You are on page 1of 7

/*

Singly Linked List Program using C, written by Alok Basu for CSE 3rd SEM */ #include <stdio.h> #include <conio.h> struct list { int info; struct list * next; }; struct list *create(struct list *,int); void traverse(struct list *); struct list *insert_at_begining(struct list *,int); struct list *insert_at_last(struct list *,int); int count(struct list *); void main() { struct list *head=NULL,*ptr; int num,key,item,wish,pos; clrscr(); clrscr(); while(1) { printf("\n1.Create\n2.Traverse\n3.INS at begining\n4. Insert at last\n5.Coun t nodes"); printf("\n6. Insert after a node whose address known\n7.Delete a node which is after a given node\n 8.Insert after a given(info known) node"); printf("\n9.DEL a given node\n10.INS at POSITION\n11.REVERSE THE LIST\n\n12. Delete first\n0.Exit\n\nEnter Choice "); scanf("%d",&wish); switch(wish) { case 1: printf("\n How many nodes to be created ? "); scanf("%d",&num); head=create(head,num); break; case 2: printf("\n\nInformation retrieved....Press Enter\n"); traverse(head); getch(); break; case 3: printf("\n Enter information of the newnode to be inserted at be gining "); scanf("%d",&item); head=insert_at_begining(head,item); break; case 4: printf("\n Enter the information of the newnode to be inserted at last "); scanf("%d",&item); insert_at_last(head,item); break; 7 case 0: exit(1); default: printf("\nwrong choice....Press Enter to continue.."); } // getch(); } }

struct list *create(struct list *head,int num) { struct list *temp,*newnode; int item,i; if(head!=NULL) { printf("Already created"); return(head); } for(i=1;i<=num;i++) { printf("Enter the information to be stored in a node"); scanf("%d",&item); newnode=(struct list *)malloc(sizeof(struct list)); newnode->info=item; newnode->next=NULL; if(head==NULL) head=newnode; else temp->next=newnode; temp=newnode; } return(head); } void traverse(struct list *head) { struct list *loc; loc=head; while(loc!=NULL) { printf("\naddress of node=%u info=%d next=%u ",loc,loc->info,loc->next); loc=loc->next; } } /* Insert after by address */ void insert_after_address(struct list *ptr, int item) { struct list *newnode; newnode=(struct list *) malloc(sizeof(struct list)); newnode->info=item; newnode->next=ptr->next; ptr->next=newnode; } /* Insert after by address */ void delete_after_address(struct list *ptr) { struct list *temp; temp=ptr->next; ptr->next=temp->next; temp->next=NULL; free(temp); } struct list* delete_first(struct list *head) { struct list *temp; if(head==NULL) return(head);

temp=head; head=temp->next; temp->next=NULL; free(temp); return(head); } /* Function definition for insertion after a given node and in case the given no de is not found then insert the node at the beginning of the list */ void insert_after(struct list * head,int key,int item) { struct list *loc,*newnode; loc=head; if(loc==NULL) { printf("Empty linked list"); } while(loc!=NULL && loc->info != key) loc=loc->next; newnode=(struct list *) malloc(sizeof(struct list)); newnode->info=item; if(loc==NULL) { newnode->next=head; head=newnode; } else { newnode->next=loc->next; loc->next=newnode; } } /* definition for deletion of a given node */ struct list * delete(struct list * head,int key) { struct list *loc,*locp; loc=head; if(loc==NULL) { printf("Empty list"); return(head); } while(loc!=NULL && loc->info!=key) { locp=loc; loc=loc->next; } if(loc==NULL) { printf("Node to be deleted does not exist"); return(head); } if(loc==head) head=loc->next; else locp->next=loc->next; free(loc); return(head);

} /* Definition for counting nodes in a Linked List */ int count(struct list * head) { struct list *loc; int cnt=0; loc=head; while(loc!=NULL) { cnt=cnt+1; loc=loc->next; } return(cnt); } /* Definition for Reversing a Linked List */ struct list * reverse(struct list * head) { struct list *loc,*locp,*locn; if(head==NULL) { printf("\nEmpty List"); return(head); } locn=head; loc=NULL; /* loc=head; locn=loc->next; locp=NULL; loc->next=NULL; */ while(locn!=NULL) { locp=loc; loc=locn; locn=loc->next; loc->next=locp; } head=loc; return(head); } /* Definition for sorting the List in ascending order */ /* void sort(struct list * head) { struct list *loc; int cnt=0,i,j,temp; if(head==NULL) { printf("\nEmpty List"); return; } loc=head; while(loc!=NULL) { cnt=cnt+1; loc=loc->next;

} for(i=1;i<cnt;i++) { loc=head; for(j=1;j<=cnt-i;j++) { if(loc->info > loc->next->info) { temp=loc->info; loc->info=loc->next->info; loc->next->info=temp; } loc=loc->next; } } } */ /*Definition to insert at begining */ struct list * insert_at_begining(struct list *head, int item) { struct list * newnode; newnode=(struct list *) malloc(sizeof(struct list)); newnode->info=item; newnode->next=head; head=newnode; return(head); } /*Definition to insert at last */ struct list * insert_at_last(struct list *head, int item) { struct list *locp, *loc, *newnode; loc=head; if(loc==NULL) { printf("Empty linked list"); return(head); } while(loc->next!=NULL) { // locp=loc; loc=loc->next; } /* while(loc!=NULL) { locp=loc; loc=loc->next; } */ newnode=(struct list *) malloc(sizeof(struct list)); newnode->info=item; /* newnode->next=loc; locp->next=newnode; */ newnode->next=NULL; loc->next=newnode; return(head); }

struct list * insert_in_sorted_list(struct list * head,int item) { struct list *loc,*locp,*newnode; if(head==NULL) { printf("Empty List"); return(head); } loc=head; while(loc != NULL && item > loc->info) { locp = loc; loc = loc->next; } newnode=(struct list *)malloc(sizeof(struct list)); newnode->info = item; if(head==loc) { newnode->next = head; head = newnode; } else { newnode->next = locp->next; locp->next = newnode; } return(head); } struct list * insert_at_position(struct list *head, int item,int pos) { struct list *loc,*newnode; int nodeCount,i; if(head==NULL) { printf("Empty linked list"); return(head); } loc=head; nodeCount=1; while(loc!=NULL && pos-1>nodeCount) { nodeCount++; loc=loc->next; } if(loc==NULL || pos <=0) { printf("\nPosition Given is not valid\n"); return(head); } newnode=(struct list *)malloc(sizeof(struct list)); newnode->info=item; if(pos==1) { newnode->next=head; head=newnode; } else {

newnode->next=loc->next; loc->next=newnode; } return(head); }

You might also like