Circular linked list in Data Structures

Ratings:
(4)
Views:787
Banner-Img
  • Share this blog:

Circular linked list

A linked list in which the last node points to the first node instead of containing null pointer will be termed as circularly linked list or simply circular list   36   Circular linked list in data structures have an advantages over singly linked list. It is concerned with the accessibility of a node. Ina circular list every node is accessible from a given node that is, from this given node all nodes can be reached by changing through the list.  

Insertion operation:

  1. Insertion as a first node:

cur7 1= Head; Head=cur; ptr 7 1=Head;   37    

(ii) Insertion after a given node:

cur 7 link = ptr à link; ptr1 7link =cur;   38    

Deletion Operation

  • Deleting first node

Head =ptr1 7 link; ptr 7 link =Head; free(ptr); 39

  • Deleting a specific node

temp=ptrl 7 link; ptr1 7 link = ptr1 7 link à link free(temp); 40

Circular linked list Program in Data Structures

Implementaton of circular linked list # include <studio.h> #include <conio.h> # define NULL #include<stdlib.h> struct Node { int data; struct Node*link; }; struct node*Head; void create() { int a; struct Node * cur,*ptr; Head=NULL; printf(“Enter Data For The Node(-1):”); scanf(“%d%,&a); while(a!=-1) { cur =(struct Node *) malloc (size of (struct Node)); cur7data =a; cur 7link =NULL; if(Head== NULL) Head==cur; else ptr= cur; printf(“Enter Data For Node (-1):”); scanf (“%d”,&a); } ptr 7 link =Head; } void insert() { int ele; char ch; struct Node*ftr, *ptr,*ptr1,*cur; ptr=Head; ptr1=Head; cur=(struct Node *) malloc (size of (struct Nodes); printf(“Enter data:”); scanf(“%d”,&cur àdata); cur 7 link =NULL; printf(“Do you want to insert first(y\n):”); flush(stdin); scanf(“%c”,&ch); if((ch==’y’) || (ch==’y’)) { while(ptr 7link!=Head) { ptr=ptr 7 link; } cur 7 link =Head; Head= cur; ptr à link =Head; } else { printf(“After which element you want to insert:”); scanf(“%d”,&ele); do { if( ptr1 7data == ele) { cur 7 link =ptr1 7link; ptr1 7link =cur; } ptr1 = ptr1 7link; } while(ptr1!=Head); } } void deletion() { int ele; char ch; struct Node *temp,*ptr,*ptr1; ptr=ptr1=Head; fflush(stdin); printf(“Do you want to delete first(y\n):”); scanf(“%c”,&ch); if((ch==’y’)|| (ch==’y’)) { while(ptr 7 link!=Head) { ptr= ptr 7link; } cur 7link =Head; Head= cur; ptr 7 link =Head; } else { printf(“After which element you want to insert:”); scanf(“%d”,&ele); do { if(ptr1 7 data ==ele) { cur 7link=ptr1 7 link; ptr1 7link =cur; } ptr1 =ptr1 àlink; } while(ptr1!=Head); } } void deletion() { int ele; char ch; struct Node *temp,*ptr,*ptr1; ptr=ptr1=Head; fflush(stdin); printf(“Do you want to delete first (y/n):”); scanf(“%c”,&ch); if((ch==’y’) || (ch==’y’)) { while(ptr 7link!=Head) { ptr = ptr 7 link; } Head =ptr1 7 link; ptr 7 link =Head; free(ptr1); } printf(“Which element you want to delete:”); scanf(“%d”,&ele); do { if(ptr1 7 link 7 data == ele) { temp=ptr1 7link; ptr1 7 link = ptr1 7 link 7link; free(temp); break; } else { } while(ptr1!=Head) } } void display() { struct Node * temp; temp=Head; do { printf(“%d” 7,temp 7 data); temp=temp 7link; } while(temp!= Head); } } void main() { int op; clrscr(); create(); printf(“1.INSERTION \n”); printf(“2.DELETION  \n”); printf(“3.DISPLAY  \n”); printf(“4.EXIT \n”); do { printf(“Enter your option:\n”); scanf(“%d”,&op); switch (op) { { case1: insertion (); break; case2: deletion (); break; case3: Display(); break; case4: exit(0); } } while (op<=4); }

You liked the article?

Like : 0

Vote for difficulty

Current difficulty (Avg): Medium

Recommended Courses

1/15

About Author
Authorlogo
Name
TekSlate
Author Bio

TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.


Stay Updated


Get stories of change makers and innovators from the startup ecosystem in your inbox