Skip to content

Instantly share code, notes, and snippets.

@thinkphp
Created January 7, 2025 19:55
Show Gist options
  • Save thinkphp/3a69a9820f6976b0aba7293d47d04302 to your computer and use it in GitHub Desktop.
Save thinkphp/3a69a9820f6976b0aba7293d47d04302 to your computer and use it in GitHub Desktop.
concatenare liste
#include <stdio.h>
#include <stdlib.h>
#define EXIT_SUCCESS 0
typedef struct node {
int data;
struct node* next;
} node_t;
//functie de afisare a listei simplu inlantuite
void list_display(node_t *anchor, const char*list_name) {
if(anchor == NULL) {
printf("%s: empty list\n", list_name);
return;
}
printf("%s: ", list_name);
node_t *current = anchor;
while(current!=NULL) {
printf("%d", current->data);
if(current->next != NULL) printf(" -> ");
current = current->next;
}
printf("\n");
}
//sarcina a)alocare dinamica a memoriei
node_t* create_list_node(int data) {
//alocare dinamica a memoriei in HEAP
node_t *new_node = (node_t*)malloc(sizeof(node_t));
new_node->data = data;
new_node->next = NULL;
return new_node;
}
//sarcina b) interfata procedurala, adauga un nou element la sfarsitul listei
void list_insert_end_proc(node_t **anchor, int data) {
//alocare dinamica a memoriei
//node_t *new_node = (node_t*)malloc(sizeof(node_t));
//new_node->data = value;
//new_node->next = NULL;
node_t * new_node = create_list_node( data );
if(new_node == NULL) {
printf("Memory allocation failed!");
return;
}
//verificam daca *anchor == NULL
if(*anchor == NULL) {
*anchor = new_node;
return;
}
node_t *current = *anchor;
while(current->next != NULL) current = current->next;
current->next = new_node;
}
//varianta recursiva ->exercitiul c)
/*
lista1 = [1,2,3]
lista2 = [4,5,6]
[1,2,3]----->>[4,5,6]
list_append_rec(lista1, lista2) {
}
*/
node_t * list_append_recursive(node_t*anchor_a, node_t*anchor_b) {
if(anchor_a->next == NULL) {
anchor_a->next = anchor_b;
return anchor_a;
}
list_append_recursive( anchor_a->next, anchor_b );
return current;
}
//varianta iterativa
node_t* list_append(node_t* anchor_a, node_t* anchor_b) {
if(anchor_a == NULL) return anchor_b;
//sa gasim locul/adresa unde se termina lista 1
node_t *current = anchor_a;
while(current->next!=NULL) {
current = current->next;
}
//conectam lista1 cu lista2
current->next = anchor_b;
return anchor_a;
}
void list_free(node_t *anchor) {
node_t *current = anchor;
while(current!=NULL) {
node_t *temp = current;
current = current->next;
free(temp);
}
}
int main() {
node_t *anchor_a = NULL;
node_t *anchor_b = NULL;
list_insert_end_proc(&anchor_a, 10);
list_insert_end_proc(&anchor_a, 20);
list_insert_end_proc(&anchor_b, 30);
list_insert_end_proc(&anchor_b, 40);
list_display(anchor_a, "Lista A");
list_display(anchor_b, "Lista B");
anchor_a = list_append_recursive(anchor_a, anchor_b);
printf("\nCum arata noua lista dupa appending lista B(anchor_b) to lista A(anchor_a)\n");
list_display(anchor_a,"Combined List");
//list_free(anchor_a);
// Aufgabe d)
list_free( anchor_b );
//Singly Linked List = lista simplu inlantuita
//lista dublu inlantuita
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment