• USA : +1 973 910 5725
  • INDIA: +91 905 291 3388
  • info@tekslate.com
  • Login

Circular linked list in Data Structures

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);

}

“At TekSlate, we are trying to create high quality tutorials and articles, if you think any information is incorrect or want to add anything to the article, please feel free to get in touch with us at info@tekslate.com, we will update the article in 24 hours.”

0 Responses on Circular linked list in Data Structures"

    Leave a Message

    Your email address will not be published. Required fields are marked *

    Site Disclaimer, Copyright © 2016 - All Rights Reserved.

    Support


    Please leave a message and we'll get back to you soon.

    I agree to be contacted via e-mail.