You are on page 1of 7

#include<stdio.h> #include<stdlib.

h> typedef struct nodetype { int key; struct nodetype *next; } NodeT; NodeT *first=NULL, *last=NULL; void create () { char ch; do { NodeT *p; p=(NodeT *) malloc(sizeof(NodeT)); p->next=NULL; printf("\nEnter the data: "); scanf("%d",&p->key); if (last!=NULL) last->next=p; else first=p; last=p; printf("Do you want to create another? "); ch=getche(); } while (ch!='n'); } void display () { NodeT *p; printf("\nThe linked list: "); p=first; while(p!=NULL) { printf("%d--->",p->key); p=p->next; } printf("NULL"); } void access (int givenkey) { NodeT *p; p=first; while (p!=NULL) if (p->key==givenkey) { return p; } else p=p->next;

return NULL; } void append (int num) { NodeT *p; p=(NodeT *) malloc(sizeof(NodeT)); p->key=num; if (last!=NULL) { last->next=p; last=p; p->next=NULL; } } void insert_when_list_empty (int num) { NodeT *p; p=(NodeT *) malloc(sizeof(NodeT)); p->key=num; if (first == NULL) { first=p; last=p; p->next=NULL; } } void insert_bef_1node (int num) { NodeT *p; p=(NodeT *) malloc(sizeof(NodeT)); p->key=num; if (first != NULL) { p->next=first; first=p; } } void addafter (int num, int gkey) { NodeT *p, *q, *q1; p=(NodeT *) malloc(sizeof(NodeT)); q1=NULL; q=first; while (q!=NULL) { if (q->key==gkey) break; q1=q; q=q->next; } p->key=num; if (q!=NULL) { p->next=q->next; q->next=p;

if (q==last) last=p; } } void addbefore (int num, int gkey) { NodeT *p, *q, *q1; p=(NodeT *) malloc(sizeof(NodeT)); q1=NULL; q=first; while (q!=NULL) { if (q->key==gkey) break; q1=q; q=q->next; } p->key=num; if (q!=NULL) { if (q==first) { p->next=first; first=p; } else { q1->next=p; p->next=q; } } } void remove_1node () { NodeT *p; if (first != NULL) { p=first; first=first->next; free (p); if (first==NULL) last=NULL; } } void remove_last_node () { NodeT *p, *q, *q1; q1=NULL; q=first; if (q!=NULL) { while (q!=last) { q1=q; q=q->next;

} if (q==first) first=last=NULL; else { q1->next=NULL; last=q1; } free(q); } } void remove_node_key (int gkey) { NodeT *q, *q1; q1=NULL; q=first; while (q!=NULL) { if (q->key==gkey) break; q1=q; q=q->next; } if (q!=NULL) { if (q==first) { first=first->next; free(q); if (first==NULL) last=NULL; } else { q1->next=q->next; if (q==last) last=q1; free(q); } } } void delete_list () { NodeT *p; while (first != NULL) { p=first; first=first->next; free(p); } last=NULL; } int count () {

NodeT *p; int c=0; p=first; while(p!=NULL) { p=p->next; c++; } return c; } int main() { NodeT *p; int nr,i,j,t,u,k,k1; while (1) { alfa: printf("\nList Operations\n"); printf("===============\n"); printf("1.Insert\n"); printf("2.Display\n"); printf("3.Size\n"); printf("4.Delete\n"); printf("5.Create\n"); printf("6.Exit\n"); printf("Enter your choice : "); if(scanf("%d",&i)<=0) { printf("Enter only an Integer\n"); exit(0); } else { switch(i) { case 1: printf("Enter the number to insert : "); scanf("%d",&nr); if (first==NULL) insert_when_list_empty(nr); else { beta: printf("\nInsertion options \n"); printf("===============\n"); printf("1.Insert before the first node \n"); printf("2.Insert after the last node (append) \n"); printf("3.Insert with respct to a given key \n"); printf("4.Exit \n"); printf("Enter your choice : "); if (scanf("%d",&j)<=0) { printf("Enter only an Integer\n"); exit(0); } else { switch(j) { case 1:

insert_bef_1node(nr); break; case 2: append(nr); break; case 3: printf("\nEnter the key : "); scanf("%d",&k); printf("\nInsertion options with respect to the given key\n"); printf("===============\n"); printf("1.Insert after the given key\n"); printf("2.Insert before the given key\n"); printf("3.Exit\n"); printf("Enter your choice : "); if (scanf("%d",&t)<=0) { printf("Enter only an Integer\n"); exit(0); } else { switch(t) { case 1: addafter(nr,k); break; case 2: addbefore(nr,k); break; case 3: //return 0; goto beta; break; default: printf("Invalid option\n"); } } break; case 4: //return 0; goto alfa; break; default: printf("Invalid operation\n"); } } } break; case 2: if(first==NULL) { printf("List is Empty\n"); } else { display(); } break; case 3: printf("Size of the list is %d\n",count()); break; case 4: if (first==NULL)

printf("The list is empty!\n"); else { printf("\nDeleting options \n"); printf("===============\n"); printf("1.Delete the first node \n"); printf("2.Delete the last node \n"); printf("3.Delete a node specified by a given key \n"); printf("4.Delete the list \n"); printf("5.Exit\n"); printf("Enter your choice : "); if (scanf("%d",&u)<=0) { printf("Enter only an Integer\n"); exit(0); } else { switch(u) { case 1: remove_1node(); break; case 2: remove_last_node(); break; case 3: printf("Introdue the key : "); scanf("%d",&k1); remove_node_key(k1); break; case 4: delete_list(); break; case 5: //return 0; goto alfa; break; default: printf("Invalid operation\n"); } } } break; case 5: create(); break; case 6: return 0; default: printf("Invalid operation\n"); } } } return 0; } /* SERGIU NICOLAE POP A FACUT ACEST COD. ACESTA NU ESTE UN PAMFLET. TREBUIE TRAT AT CA ATARE!*/

You might also like