Announcement:

This is just the beginning of this blog, please don't copy any of my posts.

Thursday, 24 October 2013

[C Program] for Implementing Singly Circular Linked List.


//Program for implementing Singly Circular Linked List.

#include<stdio.h>
#include<conio.h>
#include<alloc.h>

//Structure definition
typedef struct cll
{
int data;
struct cll *next;
}node;

node *head,*t,*temp;

//Creating head node
node * create()
{
node *head;
head=(node *)malloc(sizeof(node));
head->data=-1;
head->next=head;
printf("\nHead node is created...\n");
return(head);
}

//Inserting nodes into circular linked list
void insert(node *head)
{
node *temp,*t;
int v;
temp=(node *)malloc(sizeof(node));
printf("\n\nEnter data for new node:");
scanf("%d",&v);
temp->data=v;
temp->next=head;
t=head;
while(t->next!=head)
{
t=t->next;
}
t->next=temp;
}

//Displaying the circular linked list
void display(node *head)
{
node *temp;
temp=head->next;
while(temp->next!=head)
{
printf("%d\t",temp->data);
temp=temp->next;
}
printf("%d\t",temp->data);
}

//Delition of node from circular linked list
void Delete(node *head)
{
int c;
node *temp,*t;
temp=head;
printf("\n\nEnter the data to be deleted from list:");
scanf("%d",&c);
while(temp->next!=head)
{
if(temp->next->data==c)
{
t=temp->next;
temp->next=t->next;
free(t);
}
else
{
temp=temp->next;
}
}
}

//Reversing the circular linked list
void reverse(node *head)
{
int a[10],i=0;
node *temp;
temp=head;
while(temp->next!=head)
{
a[i]=temp->next->data;
i++;
temp=temp->next;
}
a[i]=temp->data;
for(i=i-1;i>=0;i--)
{
printf("%d\t",a[i]);
}
}

//Reverting the circular linked list
void revert(node *head)
{
node *p,*q,*r;
p=head->next;
q=head;
while(p!=head)
{
r=q;
q=p;
p=p->next;
q->next=r;
}
head->next=q;
display(head);
}

//Main function
void main()
{
int ch;
node *head;
clrscr();
printf("*******Singly Circular Linked List*******");
do
{
printf("\n\nMenues:");
printf("\n\n1.Create 2.Insert 3.Delete 4.Reverse 5.Revert 6.Display 7.Exit");
printf("\n\nEnter Youe Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: head=create();
break;
case 2: insert(head);
break;
case 3: Delete(head);
break;
case 4: reverse(head);
break;
case 5: revert(head);
break;
case 6: display(head);
break;
case 7: exit(0);
}
}while(ch<7);
getch();
}


/* Output of Program:

*******Singly Circular Linked List*******

Menues:

1.Create 2.Insert 3.Delete 4.Reverse 5.Revert 6.Display 7.Exit

Enter Youe Choice:1

Head node is created...

Menues:

1.Create 2.Insert 3.Delete 4.Reverse 5.Revert 6.Display 7.Exit

Enter Youe Choice:2

Enter data for new node:11

Menues:

1.Create 2.Insert 3.Delete 4.Reverse 5.Revert 6.Display 7.Exit

Enter Youe Choice:2

Enter data for new node:22

Menues:

1.Create 2.Insert 3.Delete 4.Reverse 5.Revert 6.Display 7.Exit

Enter Youe Choice:2

Enter data for new node:33

Menues:

1.Create 2.Insert 3.Delete 4.Reverse 5.Revert 6.Display 7.Exit

Enter Youe Choice:6
11      22      33

Menues:

1.Create 2.Insert 3.Delete 4.Reverse 5.Revert 6.Display 7.Exit

Enter Youe Choice:2

Enter data for new node:44

Menues:

1.Create 2.Insert 3.Delete 4.Reverse 5.Revert 6.Display 7.Exit

Enter Youe Choice:2

Enter data for new node:55

Menues:

1.Create 2.Insert 3.Delete 4.Reverse 5.Revert 6.Display 7.Exit

Enter Youe Choice:2

Enter data for new node:66

Menues:

1.Create 2.Insert 3.Delete 4.Reverse 5.Revert 6.Display 7.Exit

Enter Youe Choice:6
11      22      33      44      55      66

Menues:

1.Create 2.Insert 3.Delete 4.Reverse 5.Revert 6.Display 7.Exit

Enter Youe Choice:3

Enter the data to be deleted from list:33

Menues:

1.Create 2.Insert 3.Delete 4.Reverse 5.Revert 6.Display 7.Exit

Enter Youe Choice:6
11      22      44      55      66

Menues:

1.Create 2.Insert 3.Delete 4.Reverse 5.Revert 6.Display 7.Exit

Enter Youe Choice:4
66      55      44      22      11

Menues:

1.Create 2.Insert 3.Delete 4.Reverse 5.Revert 6.Display 7.Exit

Enter Youe Choice:5
66      55      44      22      11

Menues:

1.Create 2.Insert 3.Delete 4.Reverse 5.Revert 6.Display 7.Exit

Enter Youe Choice:6
66      55      44      22      11

Menues:

1.Create 2.Insert 3.Delete 4.Reverse 5.Revert 6.Display 7.Exit

Enter Youe Choice:3

Enter the data to be deleted from list:55

Menues:

1.Create 2.Insert 3.Delete 4.Reverse 5.Revert 6.Display 7.Exit

Enter Youe Choice:6
66      44      22      11

Menues:

1.Create 2.Insert 3.Delete 4.Reverse 5.Revert 6.Display 7.Exit

Enter Youe Choice:7 */
Share it Please

Pankaj Gaikar

Pankaj Gaikar is a professional blogger from Pune, India who writes on Technology, Android, Gadgets, social media and latest tech updates at Punk Tech , Being Android & Shake The Tech . Email me HERE

0 comments :

Post a Comment

Copyright @ 2013 Pune University Bachelor of Engineering . Designed by Pankaj Gaikar | Love for The Tricks Machine