C: List: stack and queue - Applications resolved
1) Write a type to represent a linked list structure through which will hold text values to display a message. The list will contain three items.
#include<stdio.h> #include<conio.h> #include<stdlib.h> //write a structure to represent a node (element) of the list struct node_list{ char* inf; node_list* next; }; //build a proper type to represent a node list based on previously defined structure typedef struct node_list node; //main function to run void main() { node* a; //first element of the list a=(node*)malloc(sizeof(node)); a->inf="Hello"; //contents of the first element node* b; //the second element of the list b=(node*)malloc(sizeof(node)); a->next=b; //next element of the first element b->inf="C/C++"; node* c; //the third element of the list c=(node*)malloc(sizeof(node)); b->next=c; c->inf="language!"; c->next=NULL; //or c->next=0; while(a) //a!=NULL or a!=0 { printf("%s ",a->inf); a=a->next; } getch(); }
2) Write a type to represent a linked list structure through which will hold text values to display a message. All references to list be done with a single element.
#include<stdio.h> #include<conio.h> #include<stdlib.h> //write a structure to represent a nodee (element) of the list struct node_list{ char* inf; node_list* next; }; //build a proper type to represent a nodee list based on previously defined structure typedef struct node_list node; //main function to run void main() { node* a; //first element of the list a=(node*)malloc(sizeof(node)); a->inf="Hello"; //contents of the first element a->next=(node*)malloc(sizeof(node)); a->next->inf="C/C++"; //next element of the first element a->next->next=(node*)malloc(sizeof(node)); a->next->next->inf="language!"; a->next->next->next=NULL; while(a) //a!=NULL or a!=0 { printf("%s ",a->inf); a=a->next; } getch(); }
3) Will write a program to determine the main operations on a linked list (asymmetric) containing integers. Will develop the following:
- Generation (initializing) a list
- Length (number of elements) from a list
- Adding an item in a list: the front list, behind the list, after a given element
- Change the value of a particular item in the list
- Deleting an item from the list
- Deleting first item
- Deleting last item
- Delete a specific item
- Display list
- Destruction (deallocation) list.
- Generation (initializing) a list
- Length (number of elements) from a list
- Adding an item in a list: the front list, behind the list, after a given element
- Change the value of a particular item in the list
- Deleting an item from the list
- Deleting first item
- Deleting last item
- Delete a specific item
- Display list
- Destruction (deallocation) list.
#include<stdio.h> #include<conio.h> #include<stdlib.h> //defining a structure for representing a nodee type of the list typedef struct node{ int val; node *next; }node; //initialization (generation) a list node* init(node *cap); //length - no. the elements of the list int lung(node *cap); //add an element through front of the list node* adFata(node *cap, int inf); //adding an element behind of the list node* adSpate(node *cap, int inf); //adding an element to a specific position in the list node* adInterior(node *cap,int valpoz, int valnou); /*modification of a value from the list (if the value of the desired the change occurs more often will change first meeting)*/ node* modify(node *cap, int valvechi, int valnou); //delete an item through front of the list node* stergeFata(node *cap); //delete an item from list node* stergeVal(node *cap, int inf); //delete an item through back of the list node* stergeSpate(node *cap); //components of the list display void afisList(node *cap); //destruction (deallocation) of the list void destroy(node *cap); //initialization (generation) a list node* init(node *cap) { cap=NULL; return cap; } //length - number of elements in list int lung(node *cap) { node *aux; //element of the scroll of the list int nr=0; aux=cap; while(aux!=NULL) { nr++; aux=aux->next; } return nr; } //adding an element through front of the list node* adFata(node *cap, int inf) //inf - desired numerical value will be introduced to use { node *temp; //auxiliary element that will be initially adding temp=(node*)malloc(sizeof(node)); temp->val=inf; //value to populate item in the listand remains to the populate and liaison //testing is done if there are items in the list if (cap==NULL) //case the list is empty { temp->next=NULL; cap=temp; } else //situation in which there are elements in list { temp->next=cap; cap=temp; } return cap; } //adding an element behind of the list node* adSpate(node *cap, int inf) { node *temp, *aux; //aux - used to scroll through the list temp=(node*)malloc(sizeof(node)); temp->val=inf; //testing is done if there are items in the list if (cap==NULL) //case the list is empty { temp->next=NULL; cap=temp; } else //situation in which there are elements in list { aux=cap; while(aux->next!=NULL) aux=aux->next; aux->next=temp; temp->next=NULL; } return cap; } //Adding an item to a specific position in the list node* adInterior(node *cap, int valpoz, int valnou) { node *temp, *aux; temp=(node*)malloc(sizeof(node)); temp->val=valnou; if (cap==NULL) { temp->next=NULL; cap=temp; } else { aux=cap; while((aux->next!=NULL)&&(aux->val!=valpoz)) aux=aux->next; if(aux->val==valpoz) { temp->next=aux->next; aux->next=temp; } else printf("\nValoarea %d nu se afla in lista\n", valpoz); } return cap; } /*modification of a value from the list (if the value of the desired the change occurs more often will change first meeting)*/ node* modify(node *cap, int valvechi, int valnou) { node *aux; aux=cap; while((aux!=NULL)&&(aux->val!=valvechi)) aux=aux->next; if(aux!=NULL) aux->val=valnou; return cap; } //deleting list element in front node* stergeFata(node *cap) { node *temp; if (cap==NULL) printf("\nLista este vida\n"); else { temp=cap; cap=cap->next; free(temp); } return cap; } //deleting the list element behind node* stergeSpate(node *cap) { node *temp,*aux; if (cap==NULL) printf("\nLista este vida\n"); else if (cap->next==NULL) { free(cap); } else { temp=cap; aux=temp->next; while(aux->next!=NULL) { aux=aux->next; temp=temp->next; } temp->next=NULL; free(aux); } return cap; } //delete a specific element from the list node* stergeVal(node *cap, int inf) { node *temp, *aux; if (cap==NULL) printf("\nLista este vida\n"); else if(cap->val==inf) { temp=cap; cap=cap->next; free(temp); } else if(cap->next==NULL) printf("\nDeleted value is not in the list\n"); else { temp=cap; aux=temp->next; while((aux!=NULL)&&(aux->val!=inf)) { aux=aux->next; temp=temp->next; } if(aux!=NULL) { temp->next=aux->next; free(aux); } } return cap; } //display components listed void afisList(node *cap) { node *aux; aux=cap; printf("List elements are: "); while (aux!=NULL) { printf("%d ", aux->val); aux=aux->next; } } //destruction (deallocation) of the list void destroy(node *cap) { node *temp; printf("\nIt destroys list\n"); while(cap!=NULL) { temp=cap; cap=cap->next; free(temp); } } //main function to run void main() { node *cap=NULL; int inf,poz; cap=init(cap); //initialization of the list //introduction of elements in list printf("Insert nodees in the list. Finish with zero: "); scanf("%d",&inf); while(inf!=0) //introduction ends with "0" element { cap=adSpate(cap,inf); //I chose to add through back scanf("%d",&inf); } afisList(cap); //display components of the list //Adding items using the option to add in front printf("\nEnter an element for it to add the first position: "); scanf("%d",&inf); cap=adFata(cap,inf); afisList(cap); //display components of the list //Added through interior exemplifying printf("\nEnter the value by which you want to insert: "); scanf("%d",&poz); printf("Enter an element for the value it adds after%d: ",poz); scanf("%d",&inf); cap=adInterior(cap,poz,inf); afisList(cap); //display components of the list //examples of change in an option from a list printf("\nEnter the value you want to modify: "); scanf("%d",&poz); printf("Enter new value: "); scanf("%d",&inf); cap=modify(cap,poz,inf); afisList(cap); //display components of the list //delete the first element of the list printf("\n\nWe will delete the first element of the list - "); cap=stergeFata(cap); afisList(cap); //display components of the list //stergerea prin spatele listei printf("\nWe will delete the last element in the list - "); cap=stergeSpate(cap); afisList(cap); //delete a specific value from the list printf("\nEnter the value you wish to delete: "); scanf("%d",&inf); cap=stergeVal(cap,inf); afisList(cap); //destruction (deallocation) of the list printf("\n\nWe destroy list"); destroy(cap); getch(); }