circular linked list in c

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>

struct node {
   int data;
   int key;
	
   struct node *next;
};

struct node *head = NULL;
struct node *current = NULL;

bool isEmpty() {
   return head == NULL;
}

int length() {
   int length = 0;

   //if list is empty
   if(head == NULL) {
      return 0;
   }

   current = head->next;

   while(current != head) {
      length++;
      current = current->next;   
   }
	
   return length;
}

//insert link at the first location
void insertFirst(int key, int data) {

   //create a link
   struct node *link = (struct node*) malloc(sizeof(struct node));
   link->key = key;
   link->data = data;
	
   if (isEmpty()) {
      head = link;
      head->next = head;
   } else {
      //point it to old first node
      link->next = head;
		
      //point first to new first node
      head = link;
   }    
}

//delete first item
struct node * deleteFirst() {

   //save reference to first link
   struct node *tempLink = head;
	
   if(head->next == head) {  
      head = NULL;
      return tempLink;
   }     

   //mark next to first link as first 
   head = head->next;
	
   //return the deleted link
   return tempLink;
}

//display the list
void printList() {

   struct node *ptr = head;
   printf("\n[ ");
	
   //start from the beginning
   if(head != NULL) {
	
      while(ptr->next != ptr) {     
         printf("(%d,%d) ",ptr->key,ptr->data);
         ptr = ptr->next;
      }
   }
	
   printf(" ]");
}

void main() {
   insertFirst(1,10);
   insertFirst(2,20);
   insertFirst(3,30);
   insertFirst(4,1);
   insertFirst(5,40);
   insertFirst(6,56); 

   printf("Original List: "); 
	
   //print list
   printList();

   while(!isEmpty()) {            
      struct node *temp = deleteFirst();
      printf("\nDeleted value:");  
      printf("(%d,%d) ",temp->key,temp->data);
   }   
	
   printf("\nList after deleting all items: ");
   printList();   
}

4
2
Mukhujje 75 points

                                    #include &lt;iostream&gt;
using namespace std;

#define  NULL  0


struct  node
{
  int  data ;
  struct  node  *next ;
} ;

struct  node  *first=NULL ;
struct  node  *last=NULL ;

void  create()
{
  int  i , n ;
  struct  node  *pnode , *p ;

  printf(&quot;Enter the number of nodes required:\n&quot;) ;
  scanf(&quot;%d&quot;,&amp;n) ;

  printf(&quot;Enter the data value of each node:\n&quot;) ;
  for(i=1 ; i&lt;=n ; i++)
  {
    pnode=(struct node*)malloc(sizeof(struct node)) ;
    if(pnode==NULL)
    {
      printf(&quot;Memory overflow. Unable to create.\n&quot;) ;
      return ;
    }

    scanf(&quot;%d&quot;,&amp;pnode-&gt;data) ;

    if(first==NULL)
	 first=last=pnode ;
    else
    {
	  last-&gt;next=pnode ;
	  last=pnode ;    /* last keeps track of last node */
    }

    last-&gt;next=first ;
  }
}

/* This function will delete a node with value k from the Linked List if such a node exists */
void  deletenode(int  k)
{
  struct  node  *p , *follow ;

  /* searching the required node */
  p=first ;
  follow=NULL ;
  while(follow!=last)
  {
    if(p-&gt;data==k)
	  break ;
    follow=p ;
    p=p-&gt;next ;
  }

  if(follow==last)
    printf(&quot;Required node not found.\n&quot;) ;
  else
  {
    if(p==first&amp;&amp;p==last)  /* deleting the one and the only node */
	  first=last=NULL ;
    else if(p==first)       /* deleting the first node */
    {
      first=first-&gt;next ;
      last-&gt;next=first ;
    }
    else if(p==last)      /* deleting the last node */
    {
      last=follow ;
      last-&gt;next=first ;
    }
    else		/* deleting any other node */
      follow-&gt;next=p-&gt;next ;

    free(p) ;
  }
}

/* This function will go through all the nodes of Linked List exactly once and will display data value of each node */
void  traverse()
{
  struct  node  *p , *follow ;
  if(first==NULL)
    printf(&quot;Circularly Linked List Empty&quot;) ;
  else
  {
    printf(&quot;Circularly Linked List is as shown: \n&quot;) ;

    p=first ;
    follow = NULL ;
    while(follow!=last)
    {
      printf(&quot;%d &quot; , p-&gt;data) ;
      follow=p ;
      p=p-&gt;next ;
    }

    printf(&quot;\n&quot;) ;
  }
}

int main()
{
  int  x , k , ch ;
 
  do
  {
    printf(&quot;\n Menu: \n&quot;) ;
    printf(&quot;1:Create Linked List \n&quot;) ;
    printf(&quot;2:Delete Node \n&quot;) ;
    printf(&quot;3:Traverse \n&quot;) ;
    printf(&quot;4:Exit \n&quot;) ;

    printf(&quot;\nEnter your choice: &quot;) ;
    scanf(&quot;%d&quot;,&amp;ch) ;

    switch(ch)
    {
      case 1:
      create() ;
      break ;

      case 2:
      printf(&quot;Enter the data value of the node to be deleted: &quot;) ;
      scanf(&quot;%d&quot;,&amp;k) ;
      deletenode(k) ;
      break ;

      case 3:
      traverse() ;
      break ;

      case 4:
      break ;
    }
 }
 while(ch!=4) ;

 return 0;
}

4 (2 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
circular linked list insert program in c circular linked list display program in c how to make a linked list circular how to create a circular linked list circular linked list in c singly circular linked list in c circular linked list in c with before and after c program to implement circular linked list circular doubly linked list program in c with all operations implementing circular linked list cpp circular linked list circular linked list program in c language circular linked list program in c short and simple circular singly linked list code in c++ doubly circular linked list c++ guide doubly circular linked list in c++ circular linked list c++ guide how to make a circular linked list implement doubly circular linked list c++ implement doubly circular linked list c ++ how to add elements to a sorted circular linked list c++ circular linked list all operations in c circular double linked list in c linked list and circular linked list insert function in a circular linked list c programming explain circular linked list circular linked list with example Write a c program for implementing circular linked list. for implementing circular linked list. in c++ circular linked list c++ implementation circular linked list structure circular linked list in c++ insertion time circular linked list in c definition circular doubly linked list code in c linked list circular c++ c circular linked list circular linked list using array circular singly linked list in c++ circular linked list program circular singly linked list c circular sinlgy linked list c circular doubly linked list in c implementation implementation of circular linked list c++ doubly circular linked list all operations c++ circular linked list gfg define circular linked list how to create circular linked list in c circular linked list in c jenny circular linke list c++ circular linked list code circular linked list example circular linked list simple program in c display circular doubly linked list c++ code write a c program to create circular singly linked list and display it circular linked list operations insert in circular linked list in c display of circular linked list c++ in circular linked list circularly linked list in c+= stl circular linked list using stl in c++ circular linked list code C circular linked list stl create circular linked list in c what is circular linked list circular linked list in c example real life circular linked list in c example how to implement circular linked list Write a c code to create a circular linked list. circular linked list insertion in c circular linked list c++ code circular linked list in c++ pdf circular linked list examples In a circular linked list * circular linked list c++ traversal circular linked list implementation in c circular linked list c++ pinch circular linked list function c++ Circular linked list implementation creation of circular linked list in c++ circular linked list algorithm circular doubly linked list c++ circular linked list c program display circular linked list in c circular linked list in c insertion circular linked list class c++ combining circular linked list c++ c++ circular linked list circular linked list c++ java circular linked list geeksforgeeks circular linked list insert at beginning and display in c circular linked list insert at beginning in c circular linked list c++ using class where is circular linked list used In a circular linked list circular linked list advantages how to make a circularly doubly linked list C++ how to make a circularly linked list C++ how to make a circular linked list c++ implementation of the circular linked list circular linked list code in cpp hlinkes list circal implementation of circular stack using linked list in c++ circular singly linked list operations create circle linked list cpp a single pointer p is used to access the circular linkeed list to which node circular linked list java code insertion in circular linked list doubly circular linked list doubly circular linked list program in c mycodeschool circularly linked list c program circular queue in linked list circular linked list in data structure syntax Code to update Circular Linked List. Code to delete element from a Circular Linked list. C++ Code to Create Circular Linked List. Code to Create circular Linked list. C++ sudo code for circular linked list In a circular linked list * time complexity of singly circular linked list Circular header linked list stores the address of the header node in the next field of the last node time complexity of adding a node at front end in Circular List time complexity of circular linked list insertion before a given element in circular linked list c circular doubly linked list in c In circular linked lists, the tail node is also the head node. circular linked list insert after a node in c c++ doubly circular linked list circular linked list insertion circular singly linked list circular singly linked list in c Circular Linked List in c++ To study and implement the basic addition, deletion and traversal operations on singly circular linked list. singly circular linked list in data structure circular linked list in c library singly circular linked list single circular linked list circular doubly linked list program in c c program for circular linked list singly linked list vs circular linked list representation circular linked list c pseudocode for singly circular linked list operations in c circular linked list using array in c circular linked list insertion and deletion IN C circular linked list insertion and deletion circular list c circular list in c Insertion , Deletion operations with Circular linked lists Write a C program to implement insertion and deletion on circular linked list. code for circular linked list circular linked list circular linked list code in c circular linked list program in c c circular list In a circular singly linked list circular linked list implimentation A circular linked list contains four nodes { A, B, C, D}. Which node pointers should be updated if a new node E is to be inserted at end of the list? Implement Circular Linked List program. circular linked list insertion and deletion program in c circular linked list in c
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source