• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to secondary sidebar

GoHired

Interview Questions asked in Google, Microsoft, Amazon

Join WeekEnd Online Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

  • Home
  • Best Java Books
  • Algorithm
  • Internship
  • Certificates
  • About Us
  • Contact Us
  • Privacy Policy
  • Array
  • Stack
  • Queue
  • LinkedList
  • DP
  • Strings
  • Tree
  • Mathametical
  • Puzzles
  • Graph

Circular Linked List

February 26, 2014 by Dhaval Dave

In the last node of a list, the link field often contains a null reference, a special value used to indicate the lack of further nodes. A less common convention is to make it point to the first node of the list; in that case the list is said to be circular or circularly linked; otherwise it is said to be open or linear.


// Program of circular linked list
#include <stdio.h>
#include <malloc.h>
 
struct node
{
  int info;
  struct node *link;
}*last;
 
main()
{
  int choice,n,m,po,i;
  last=NULL;
  while(1)
  {
    printf("1.Create Listn");
    printf("2.Add at beginingn");
    printf("3.Add after n");
    printf("4.Deleten");
    printf("5.Displayn");
    printf("6.Quitn");
    printf("Enter your choice : ");
    scanf("%d",&choice);
 
    switch(choice)
    {
     case 1:
      printf("How many nodes you want : ");
      scanf("%d",&n);
      for(i=0; i < n;i++)
      {
        printf("Enter the element : ");
        scanf("%d",&m);
        create_list(m);
      }
      break;
     case 2:
      printf("Enter the element : ");
      scanf("%d",&m);
      addatbeg(m);
      break;
     case 3:
      printf("Enter the element : ");
      scanf("%d",&m);
      printf("Enter the position after which this element is inserted : ");
      scanf("%d",&po);
      addafter(m,po);
      break;
     case 4:
      if(last == NULL)
      {
        printf("List underflown");
        continue;
      }
      printf("Enter the number for deletion : ");
      scanf("%d",&m);
      del(m);
      break;
     case 5:
      display();
      break;
     case 6:
      exit();
     default:
      printf("Wrong choicen");
    }/*End of switch*/
  }/*End of while*/
}/*End of main()*/
 
create_list(int num)
{
  struct node *q,*tmp;
  tmp= malloc(sizeof(struct node));
  tmp->info = num;
 
  if(last == NULL)
  {
    last = tmp;
    tmp->link = last;
  }
  else
  {
    tmp->link = last->link; /*added at the end of list*/
    last->link = tmp;
    last = tmp;
  }
}/*End of create_list()*/
 
addatbeg(int num)
{
  struct node *tmp;
  tmp = malloc(sizeof(struct node));
  tmp->info = num;
  tmp->link = last->link;
  last->link = tmp;
}/*End of addatbeg()*/
 
addafter(int num,int pos)
{
 
  struct node *tmp,*q;
  int i;
  q = last->link;
  for(i=0; i < pos-1; i++)
  {
    q = q->link;
    if(q == last->link)
    {
      printf("There are less than %d elementsn",pos);
      return;
    }
  }/*End of for*/
  tmp = malloc(sizeof(struct node) );
  tmp->link = q->link;
  tmp->info = num;
  q->link = tmp;
  if(q==last)    /*Element inserted at the end*/
    last=tmp;
}/*End of addafter()*/
 
del(int num)
{
  struct node *tmp,*q;
  if( last->link == last && last->info == num)  /*Only one element*/
  {
    tmp = last;
    last = NULL;
    free(tmp);
    return;
  }
  q = last->link;
  if(q->info == num)
  {
    tmp = q;
    last->link = q->link;
    free(tmp);
    return;
  }
  while(q->link != last)
  {
    if(q->link->info == num)     /*Element deleted in between*/
    {
      tmp = q->link;
      q->link = tmp->link;
      free(tmp);
      printf("%d deletedn",num);
      return;
    }
    q = q->link;
  }/*End of while*/
  if(q->link->info == num)    /*Last element deleted q->link=last*/
  {
    tmp = q->link;
    q->link = last->link;
    free(tmp);
    last = q;
    return;
  }
  printf("Element %d not foundn",num);
}/*End of del()*/
 
display()
{
  struct node *q;
  if(last == NULL)
  {
    printf("List is emptyn");
    return;
  }
  q = last->link;
  printf("List is :n");
  while(q != last)
  {
    printf("%d ", q->info);
    q = q->link;
  }
  printf("%dn",last->info);
}/*End of display()*

Similar Articles

Filed Under: problem Tagged With: Linked List

Reader Interactions

Primary Sidebar

Join WeekEnd Online/Offline Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

Join WeekEnd Online/Offline Batch from 4-April-2020

WhatsApp us

Secondary Sidebar

Custom Search

  • How I cracked AMAZON
  • LeetCode
  • Adobe
  • Amazon
  • Facebook
  • Microsoft
  • Hacker Earth
  • CSE Interview

Top Rated Questions

Python String and numbers

Find loop in Singly linked list

Given Set of words or A String find whether chain is possible from these words or not

Maximum size of square sub matrix with all 1’s in a binary matrix

VMWare SDEII Interview

‘N’ Story Building, with 1,2,3 steps how many ways can a person reach top of building.

Diagonal Traversal of Binary Tree

Print Power Set of a Set

Adobe Interview Questions 8 month Exp

robot standing at first cell of an M*N matrix. It can move only in two directions, right and down. In how many ways, it can reach to the last cell i.e. (M, N) Code it

The greedy coins game Dynamic Programming

Daughter’s Age VeryGood Puzzle

There are N nuts and N bolts, u have to find all the pairs of nuts and bolts in minimum no. of iteration

Closed Parentheses checker

Best Java Book | Top Java Programming Book for Beginners

Trapping Rain Water

Fibonacci Hashing & Fastest Hashtable

C++ OOPs Part2

Given a float number convert it into the string WITHOUT using any inbuilt Function

Given array of 0’s and 1’s. All 0’s are coming first followed by 1’s. find the position of first 1

Doubly linked list

write a c program that given a set a of n numbers and another number x determines whether or not there exist two elements in s whose sum is exactly x

Connect n ropes with minimum cost

Skiing on Mountains Matrix

How strtok() Works

Maximum of all subarrays of size k

Memory Efficient LinkedList

CodeChef Code SGARDEN

Search element in a matrix with all rows and columns in sorted order

Get K Max and Delete K Max in stream of incoming integers

Copyright © 2023 · Genesis Framework · WordPress · Log in