CSLL
CSLL
A Circular linked list is one in which address part of the last node is holding address of the first node. The pictorial representation of circular linked list is shown below. 10
0205h
20
0105h
30 0115h 0105h
0605h
50
0605h
START 0505h
0205h
0115h
LAST 0605h
Comparison between SLL and CSLL SLL 1. In SLL we can traverse only if we known Address of first node. 2. Nodes which follow NODE X is reachable but Nodes that are Preceded by NODE X is not reachable 1. In SLL we have to hold address of Previous Node to know the address. CSLL In CSLL we can also traverse if we Know address of first node. Nodes which follow NODE X is reachable but also Nodes that are Preceded by NODE X is also reachable Not necessary to hold address of Previous Node to know the address.
In a Circular linked list any node can be considered as the first node and its predecessor is considered as the last node. The following two conventions can be used: 1. A pointer variable FIRST used represent the starting node of the list. This can be used to get address of the last node. 2. A Variable LAST can be used to represent the last node and node that followed by last is represented by FIRST which is the first node of the list. Operations on Singly Linked List: The basic fundamental operation that can performed on the circular singly linked list are 1. 2. 3. 4. Creating a Circular Singly Linked List Inserting a node into the Circular Singly Linked List Deleting a node from the Circular Singly Linked List Display the content of the Circular Singly Linked List
Creating a Circular singly linked list This part explains how a nodes can be linked together to form a Circular singly linked list. Creation function first create a node and copy the information to the node. Creation function adds a node one by one at a time to create a Circular singly linked list. A Circular singly linked list can be created by two method 1. By adding a node at the front of the existing list or as a first node. 2. By adding a node at end of the existing list or as a last node . By adding a node at the front of the existing list or as a first node Here Circular linked list is represented first node by variable START and last node by variable LAST. Consider a node which is created recently represented by the pointer variable NEW as follows. 10 NEW = 1010h Initially if there is no node in the existing in list the START is pointing to NULL then this NEW node is act as first node in the list pointed by variable START. 10
1010h
START =NEW = LAST =1010h Create a one more node represented by NEW 20
0505h
NEW=0505h Connect this NEW node to the existing node as follows. The following code will gives method of connecting the node in front of existing node or list. New->next = START LAST->next = NEW; START = NEW
20 1010h
10
0505h
LAST=1010h
Here Circular linked list is represented first node by variable START and last node by variable LAST. Consider a node which is created recently represented by the pointer variable NEW as follows.
10 1010h
NEW= 1010h
Initially if there is no node in the existing in list the START and LAST is pointing to NULL then this NEW node is act as first node in the list pointed by variable START. 10
1010h
START=LAST=NEW=1010h
Create a one more node represented by NEW 20 0505h NEW =0505h Connect this NEW node to the existing node as follows. The following code will gives method of connecting the node at back of existing node or list. LAST->next = NEW; NEW->next=START LAST=NEW 10 START 1010h 20 LAST 0505h
CREATE() { NODE *NEW,*TEMP; int ele; printf("Enter the element terminated by -999\n"); scanf("%d",&ele); while(ele != -999) { NEW=(NODE *)malloc(sizeof(NODE)); NEW->info=ele; NEW->next=NEW; if(START==NULL) { START=NEW; LAST=NEW; TEMP=NEW; } else { LAST->next=NEW; NEW->next=START; LAST=NEW; } scanf("%d",&ele); } }
Inserting a node to a Circular singly linked list This part explains how a insertion can be done to an existing circular singly linked list. There are different type of insertion to an existing circular singly linked list. Inserting a node as first node in an existing circular singly linked list. Inserting a node as last node in an existing circular singly linked list. Inserting a node in between list with a given position in existing circular singly linked list. Inserting a node infront of given information in an existing circular singly linked list Inserting a node at the back end of an given information in an existing circular singly linked list.
Write C function to insert a node in between the list with a given position in existing circular singly linked list.
INSERT() { NODE *NEW,*TEMP,*PREV; int ele,pos,i; printf("Enter the element and positon \n"); scanf("%d %d",&ele,&pos); NEW = (NODE *)malloc(sizeof(NODE)); NEW->info=ele; NEW->next=NEW; if(START==NULL) { START=NEW; LAST=NEW; TEMP=NEW; } else { if(pos==1) { NEW->next = START; START=NEW; LAST->next = START; } else { TEMP=START; i=1;
while((TEMP->next !=START) && (i<pos)) { PREV=TEMP; TEMP=TEMP->next; i++; } if(TEMP->next == START) { TEMP->next = NEW; NEW->next =START; LAST = NEW; } else { PREV->next = NEW; NEW->next = TEMP; } } } } Deleting a node from the Circular singly linked list The objective of this function is to delete a node from the list. The deletion means moving pointer variable to next location address logically. In a list any node can be deleted as Deleting a first node in the list Deleting last node in the list Deleting node with given position in the list Deleting a node with a given information existing in the list
Write C function to delete a node in between the list with a given position in existing circular singly linked list.
DELETE() { NODE *TEMP,*PREV; int p=0,pos,i; printf("Enter the position to be deleted\n"); scanf("%d",&pos); if(START==NULL) { printf("CLIST is empty \n"); return(p); }
else { if(pos==1) { p=START->info; START=START->next; LAST->next=START; return(p); } else { TEMP=START; i=1; while((TEMP->next !=START) && (i!=pos)) { PREV=TEMP; TEMP=TEMP->next; i++; } if((TEMP->next==START) && (i==pos)) { p=TEMP->info; PREV->next=START; LAST=PREV; } else { if(TEMP->next == START) { printf("NO POSITION TO DELETE\n"); return(0); } else { p=TEMP->info; PREV->next=TEMP->next; return(p); } } } } }
Write a C program to Circular Singly linked list by using dynamic variable and pointer and perform following operation 1. Insert Insert a node with given position in the list 2. Delete a node with given position 3. Display a content of the list #include <stdio.h> #include <malloc.h> struct node { int info; struct node *next; }; typedef struct node NODE; NODE *START=NULL, *LAST=NULL; main() { int choice=0,l; while(choice!=5) { printf("1. CREATE \n"); printf("2. INSERT \n"); printf("3. DELETE \n"); printf("4. DISPLAY \n"); printf("5. EXIT \n"); printf("Enter your choice \n"); scanf("%d",&choice); switch(choice) { case 1: CREATE(); printf("CLIST after Creation \n"); DISPLAY(); break; case 2: INSERT(); printf("CLIST after Insertion \n"); DISPLAY(); break; case 3: l=DELETE(); printf("Deleted element is %d\n",l); printf("CLIST after Deletion \n"); DISPLAY(); break; case 4: printf("Content of CLIST is \n"); DISPLAY(); break; case 5: exit(0);
} } } CREATE() { NODE *NEW,*TEMP; int ele; printf("Enter the element terminated by -999\n"); scanf("%d",&ele); while(ele != -999) { NEW=(NODE *)malloc(sizeof(NODE)); NEW->info=ele; NEW->next=NEW; if(START==NULL) { START=NEW; LAST=NEW; TEMP=NEW; } else { LAST->next=NEW; NEW->next=START; LAST=NEW; } scanf("%d",&ele); } } INSERT() { NODE *NEW,*TEMP,*PREV; int ele,pos,i; printf("Enter the element and positon \n"); scanf("%d %d",&ele,&pos); NEW = (NODE *)malloc(sizeof(NODE)); NEW->info=ele; NEW->next=NEW; if(START==NULL) { START=NEW; LAST=NEW; TEMP=NEW; } else { if(pos==1)
{ NEW->next = START; START=NEW; LAST->next = START; } else { TEMP=START; i=1; while((TEMP->next !=START) && (i<pos)) { PREV=TEMP; TEMP=TEMP->next; i++; } if(TEMP->next == START) { TEMP->next = NEW; NEW->next =START; LAST = NEW; } else { PREV->next = NEW; NEW->next = TEMP; } } } } DELETE() { NODE *TEMP,*PREV; int p=0,pos,i; printf("Enter the position to be deleted\n"); scanf("%d",&pos); if(START==NULL) { printf("CLIST is empty \n"); return(p); } else { if(pos==1) { p=START->info; START=START->next; LAST->next=START; return(p);
} else { TEMP=START; i=1; while((TEMP->next !=START) && (i!=pos)) { PREV=TEMP; TEMP=TEMP->next; i++; } if((TEMP->next==START) && (i==pos)) { p=TEMP->info; PREV->next=START; LAST=PREV; } else { if(TEMP->next == START) { printf("NO POSITION TO DELETE\n"); return(0); } else { p=TEMP->info; PREV->next=TEMP->next; return(p); } } } } } DISPLAY() { NODE *TEMP; if(START==NULL) { printf("CLIST is Empty\n"); return; } else { TEMP=START; while(TEMP->next!=START) { printf("%d -> ",TEMP->info);
Write C program to represent long integer using Circular singly linked list #include <stdio.h> #include <malloc.h> struct node { long int info; struct node *next; }; typedef struct node NODE; main() { NODE *FIRST; char ch[80]; long int i,j,x,y; long int e; FIRST=NULL; printf("Enter the first long integer \n"); scanf("%s",ch); i=strlen(ch); i--; while(i>=0) { x=0; for(j=0;j<5 && i>=0; j++,i--) { e=power(10,j); y=(int )(ch[i] - '0'); x=x+y*e; } FIRST=CREATE(FIRST,x); } printf("First long integer is \n"); DISPLAY(FIRST); }
CREATE(TEMP,x) NODE *TEMP; long int x; { NODE *NEW,*CUR,*HEAD; NEW=(NODE *)malloc(sizeof(NODE)); NEW->info=x; NEW->next=NEW; if(TEMP==NULL) { TEMP=NEW; return(TEMP); } CUR=TEMP; while(CUR->next != TEMP) CUR=CUR->next; NEW->next=CUR->next; CUR->next=NEW; TEMP=NEW; return(TEMP); } DISPLAY(TEMP) NODE *TEMP; { NODE *CUR; if(TEMP==NULL) { printf("NO LONG INTEGER \n"); return; } else { CUR=TEMP; while( CUR->next != TEMP) { printf("%05ld ->",CUR->info); CUR=CUR->next; } printf("%05ld",CUR->info); } } power(x,y) int x,y; { int res=1; int i; for(i=1;i<=y;i++)
res=res*x; return(res); } Write a C program to merge two ordered singly linked list . Store the result in the third list
#include <stdio.h> #include <malloc.h> #include <conio.h> struct node { int info; struct node *next; }; typedef struct node NODE; NODE *FIRST=NULL, *SECOND=NULL; NODE *THIRD=NULL; main() { int l; clrscr(); printf("Create a first ordered list \n"); FIRST=CREATE(FIRST); printf("The first ordered list is \n"); DISPLAY(FIRST); printf("Create a second ordered list \n"); SECOND=CREATE(SECOND); printf("The second ordered list is \n"); DISPLAY(SECOND); printf("Merge the two ordered list \n"); THIRD=MERGE(FIRST,SECOND); printf("The resultant ordered list \n"); DISPLAY(THIRD); } CREATE(START) NODE *START; { NODE *TEMP,*NEW,*PREV; int ele; printf("Enter the element terminated by -999\n"); scanf("%d",&ele);
while(ele != -999) { NEW = (NODE *)malloc(sizeof(NODE)); NEW->info=ele; NEW->next=NULL; if(START==NULL) { START=NEW; TEMP=NEW; } else { if(ele < START->info) { NEW->next=START; START=NEW; TEMP=NEW; } else { TEMP=START; while((TEMP!=NULL) && (ele > TEMP->info)) { PREV=TEMP; TEMP=TEMP->next; } if(TEMP==NULL) { PREV->next=NEW; NEW->next=TEMP; } else { PREV->next=NEW; NEW->next=TEMP; } } } scanf("%d",&ele); } return(START); } MERGE(P,Q) NODE *P,*Q; { NODE *R=NULL; while((P!=NULL)&&(Q!=NULL)) {
if(P->info <= Q->info) { R=INSERT(R,P->info); P=P->next; } else { R=INSERT(R,Q->info); Q=Q->next; } } while(P!=NULL) { R=INSERT(R,P->info); P=P->next; } while(Q!=NULL) { R=INSERT(R,Q->info); Q=Q->next; } return(R); } INSERT(X,Y) NODE *X; int Y; { NODE *T,*Z,*PREV; Z = (NODE *)malloc(sizeof(NODE)); Z->info=Y; Z->next=NULL; if(X==NULL) { X=Z; T=Z; } else { T=X; while(T!=NULL) { PREV=T; T=T->next; } if(T==NULL) PREV->next=Z; } return(X);
DISPLAY(START) NODE *START; { NODE *TEMP; if(START==NULL) { printf("List is Empty\n"); return; } else { TEMP=START; while(TEMP!=NULL) { printf("%d -> ",TEMP->info); TEMP=TEMP->next; } printf("\n\n"); } }