Upcoming SlideShare
×

# Data structure circular list

696 views

Published on

Data Structure Circular List Programs

1 Like
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total views
696
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
29
0
Likes
1
Embeds 0
No embeds

No notes for slide

### Data structure circular list

1. 1. P a g e |1 Circular List /* WAP 1. Insert A Node Into Front Of A Circular Linked List. 2. Delete A Node From The Front End. 3. Display. */ #include<stdio.h> #include<stdlib.h> struct node { int info; struct node *next; }; typedef struct node NODE; void c_ins_first(NODE**, int); int c_del_first(NODE**); void c_display(NODE*); int main() { NODE *first=NULL; int choice, item; for(;;) { printf("Circular Linked Listn"); printf("1.Insert Frontn"); printf("2.Delete Frontn"); printf("3.Displayn"); printf("4.Exitn"); printf("Enter U R Choicen"); scanf("%d", &choice); printf("nn"); switch(choice) { case 1 : printf("Enter The Item To Insertn"); scanf("%d", &item); printf("nn"); c_ins_first(&first, item); break; case 2 : item=c_del_first(&first); if(item!='0') printf("Deleted Element Is %dnn", item); break; case 3 : printf("Contents Of Stack Aren"); c_display(first); break; default : exit(0); Ashok R
2. 2. P a g e |2 } Circular List } } return 1; void c_ins_first(NODE **first, int item) { NODE *newn, *travel; newn=(NODE*)malloc(sizeof(NODE)); newn->info=item; if(*first==NULL) { newn->next=newn; *first=newn; } else { travel=*first; while(travel->next!=*first) travel=travel->next; newn->next=*first; travel->next=newn; *first=newn; } } int c_del_first(NODE **first) { int item; NODE *travel, *temp; if(*first==NULL) { printf("List Is Emptynn"); return('0'); } temp=*first; travel=*first; if(temp->next==*first) /* If There Is Only One Node Delete It */ { item=temp->info; *first=NULL; free(temp); return item; } Ashok R
3. 3. P a g e |3 } Circular List while(travel->next!=*first) /* List Contain More Than One Nodes */ travel=travel->next; item=temp->info; *first=temp->next; travel->next=temp->next; /* OR travel->next=*first */ free(temp); return item; void c_display(NODE *first) { NODE *travel; travel=first; if(first==NULL) { printf("List Is Emptynn"); } else { while(travel->next!=first) { printf("%dn", travel->info); travel=travel->next; } printf("%dnn", travel->info); } } Ashok R
4. 4. P a g e |4 Circular List /* Insert Node Last */ case 4 : printf("Enter The Item To Insertn"); scanf("%d", &item); printf("nn"); c_ins_last(&first, item); break; void c_ins_last(NODE **first, int item) { NODE *newn, *travel; newn=(NODE*)malloc(sizeof(NODE)); newn->info=item; if(*first==NULL) { newn->next=newn; *first=newn; } else { travel=*first; while(travel->next!=*first) travel=travel->next; newn->next=travel->next; travel->next=newn; } } Ashok R
5. 5. P a g e |5 Circular List Delete Last case 5 : item=c_del_last(&first); if(item!='0') printf("Deleted Element Is %dnn", item); break; int c_del_last(NODE **first) { int item; NODE *travel, *temp; if(*first==NULL) { printf("List Is Emptynn"); return('0'); } temp=*first; travel=*first; if(temp->next==*first) /* If There Is Only One Node Delete It */ { item=temp->info; *first=NULL; free(temp); return item; } while(travel->next!=*first) /* List Contain More Than One Nodes */ { temp=travel; travel=travel->next; } item=travel->info; temp->next=travel->next; free(travel); return item; } Ashok R
6. 6. P a g e |6 Circular List Ashok R CIRCULAR LIST. WRITE A PROGRAM TO SEARCH A NODE WITH A VALUE K , IF IT IS NOT FOUND THEN INSERT VALUE AS LAST NODE. void c_ins_k(NODE **first, int k) { NODE *newn, *travel, *temp; int flag=0; newn=(NODE*)malloc(sizeof(NODE)); newn->info=k; travel=*first; if(*first==NULL) { /* List Is Empty, So Element k Is Inserted As Last Node */ newn->next=newn; *first=newn; } else if(travel->info==k) /* Item Found At 1st Postion */ flag=1; else { temp=*first; /* Initially Pointing To 1st Item In A List */ travel=temp->next; /* Initially Pointing To 2nd Item In A List */ while(travel!=*first && flag==0) { if(travel->info==k) { flag=1; break; } travel=travel->next; } } } if(flag==1) printf("%d Foundn", k); else /* If Item Not Found */ { travel=*first; while(travel->next!=*first) travel=travel->next; newn->next=travel->next; travel->next=newn; printf("k=%d Not Found In The List So It Is Inserted As Last Noden", k); }
7. 7. P a g e |7 Circular List Write A C Routine That Concatenate Two Circular List. /* Concatenate two lists. */ #include<stdio.h> #include<stdlib.h> struct node { int info; struct node *next; }; typedef struct node NODE; void c_ins_first(NODE**, int); void display(NODE*); void concat(NODE**, NODE**); int main() { NODE *first1=NULL; NODE *first2=NULL; int choice, item; for(;;) { printf("Stack Menun"); printf("1.In First List To Insert Frontn"); printf("2.In Second List To Insert Frontn"); printf("3.To Display First Listn"); printf("4.To Display Second Listn"); printf("5.To Concate Both List & To Displayn"); printf("6.Exitn"); printf("Enter U R Choicen"); scanf("%d", &choice); printf("nn"); switch(choice) { case 1 : printf("First Listn"); printf("Enter The Item To Insertn"); scanf("%d", &item); printf("nn"); c_ins_first(&first1, item); break; Ashok R
8. 8. P a g e |8 Circular List case 2 : printf("Second Listn"); printf("Enter The Item To Insertn"); scanf("%d", &item); printf("nn"); c_ins_first(&first2, item); break; case 3 : printf("Contents Of First Listn"); display(first1); break; case 4 : printf("Contents Of Second Listn"); display(first2); break; case 5 : printf("Concating List Wait....n"); concat(&first1, &first2); printf("After Concating Elements In List Isn"); display(first1); break; default : exit(0); } } } return 1; void c_ins_first(NODE **first, int item) { NODE *newn, *travel; newn=(NODE*)malloc(sizeof(NODE)); newn->info=item; if(*first==NULL) { newn->next=newn; *first=newn; } else { travel=*first; while(travel->next!=*first) travel=travel->next; newn->next=*first; travel->next=newn; *first=newn; } } Ashok R
9. 9. P a g e |9 Circular List void display(NODE *first) { NODE *travel; travel=first; if(first==NULL) { printf("List Is Emptynn"); } else { while(travel->next!=first) { printf("%dn", travel->info); travel=travel->next; } printf("%dnn", travel->info); } } void concat(NODE **first1, NODE **first2) { NODE *temp, *travel; if(*first1==NULL) *first1=*first2; else { temp=*first1; while(temp->next!=*first1) temp=temp->next; /* Now temp Is Pointing To Last Node Of A First Circular List */ travel=*first2; while(travel->next!=*first2) travel=travel->next; /*Now travel Is Pointing To Last Node Of A Second Circular List */ } } temp->next=*first2; travel->next=*first1; Ashok R