data structure circular list
DESCRIPTION
Data Structure Circular List ProgramsTRANSCRIPT
P a g e | 1 Circular List Ashok R
/* WAP1. 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 List\n");printf("1.Insert Front\n");printf("2.Delete Front\n");printf("3.Display\n");printf("4.Exit\n");printf("Enter U R Choice\n");scanf("%d", &choice);printf("\n\n");switch(choice){
case 1 : printf("Enter The Item To Insert\n");scanf("%d", &item);printf("\n\n");c_ins_first(&first, item);break;
case 2 : item=c_del_first(&first);if(item!='\0')
printf("Deleted Element Is %d\n\n", item);break;
case 3 : printf("Contents Of Stack Are\n");c_display(first);break;
default : exit(0);
P a g e | 2 Circular List Ashok R
}}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 Empty\n\n");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;
}
P a g e | 3 Circular List Ashok R
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 Empty\n\n");}else{
while(travel->next!=first){
printf("%d\n", travel->info);travel=travel->next;
}printf("%d\n\n", travel->info);
}}
P a g e | 4 Circular List Ashok R
/* Insert Node Last */
case 4 : printf("Enter The Item To Insert\n");scanf("%d", &item);printf("\n\n");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;
}}
P a g e | 5 Circular List Ashok R
Delete Last
case 5 : item=c_del_last(&first);if(item!='\0')
printf("Deleted Element Is %d\n\n", item);break;
int c_del_last(NODE **first){
int item;NODE *travel, *temp;if(*first==NULL){
printf("List Is Empty\n\n");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;
}
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 ASLAST 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 Found\n", 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 Node\n", k);
}}
P a g e | 7 Circular List Ashok R
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 Menu\n");printf("1.In First List To Insert Front\n");printf("2.In Second List To Insert Front\n");printf("3.To Display First List\n");printf("4.To Display Second List\n");printf("5.To Concate Both List & To Display\n");printf("6.Exit\n");printf("Enter U R Choice\n");scanf("%d", &choice);printf("\n\n");
switch(choice){
case 1 : printf("First List\n");printf("Enter The Item To Insert\n");scanf("%d", &item);printf("\n\n");c_ins_first(&first1, item);break;
P a g e | 8 Circular List Ashok R
case 2 : printf("Second List\n");printf("Enter The Item To Insert\n");scanf("%d", &item);printf("\n\n");c_ins_first(&first2, item);break;
case 3 : printf("Contents Of First List\n");display(first1);break;
case 4 : printf("Contents Of Second List\n");display(first2);break;
case 5 : printf("Concating List Wait....\n");concat(&first1, &first2);printf("After Concating Elements In List Is\n");
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;
}}
P a g e | 9 Circular List Ashok R
void display(NODE *first){
NODE *travel;travel=first;if(first==NULL){
printf("List Is Empty\n\n");}else{
while(travel->next!=first){
printf("%d\n", travel->info);travel=travel->next;
}printf("%d\n\n", 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;
}}