Circular Linked List
In the circular linked list the last node of the list contains the address of the first node and forms a circular chain.
Here is source code of the C Program to Implement Circular Linked List using Dynamic Memory Allocation. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
Here is source code of the C Program to Implement Circular Linked List using Dynamic Memory Allocation. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
#include<stdio.h>
struct linked_list_node{
int num;
struct linked_list_node *next;
};
typedef struct linked_list_node snode;
snode *first = NULL;
snode *last = NULL;
void insertNode(int value);
void insertAtFirst(int value);
void deleteFirst();
void deleteLast();
snode *createNode(int value);
void display();
void main()
{
int choice, nValue;
char ch='y';
while (ch == 'y')
{
printf("\n------------------------------------------------------------\n");
printf("\tCircular Linked List\n");
printf("------------------------------------------------------------\n");
printf("\n1. Insert a node at last\n");
printf("2. Insert a node at first\n");
printf("3. Delete First Node\n");
printf("4. Delete Last Node\n");
printf("5.Display contents of list\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("\nInserting a new node at last.....\n");
printf("Enter the value of node: ");
scanf("%d", &nValue);
insertNode(nValue);
break;
case 2:
printf("\nInserting a node at first.....\n");
printf("Enter the value of node: ");
scanf("%d", &nValue);
insertAtFirst(nValue);
break;
case 3:
printf("\nDeleteing first node.....\n");
deleteFirst();
break;
case 4:
printf("\nDeleteing last node.....\n");
deleteLast();
break;
case 5:
printf("\nContents of linked list are as follows\n");
display();
break;
default:
printf("\nInvalid choice!!!\n");
}
printf("\nDo you want to continue(y/n)?" );
scanf(" %c", &ch);
}
}
snode *createNode(int value)
{
snode *newNode;
newNode = (snode *)malloc(sizeof(snode));
newNode->num = value;
newNode->next = NULL;
return newNode;
}
void insertNode(int value)
{
snode *newNode;
newNode = createNode(value);
if (first == last && first == NULL)
{
newNode->next = first;
first = last = newNode;
}
else
{
last->next = newNode;
newNode->next = first;
last = newNode;
}
}
void insertAtFirst(int value)
{
snode *newNode;
newNode = createNode(value);
newNode->next = first;
last->next = newNode;
first = newNode;
}
void deleteFirst()
{
if (first == NULL)
{
printf("List is empty!!!\n");
}
else if (first == last)
{
first = last = NULL;
printf("Node successfully deleted!!!\n");
}
else
{
first = first->next;
printf("Node successfully deleted!!!\n");
}
}
void deleteLast()
{
snode *temp;
if (first == NULL)
{
printf("List is empty!!!\n");
}
else if (first == last)
{
first = last = NULL;
printf("Node successfully deleted!!!\n");
}
else
{
temp = first;
while (temp)
{
if (temp->next == last)
{
break;
}
temp = temp->next;
}
last = temp;
printf("Node successfully deleted!!!\n");
}
}
void display()
{
snode *temp = first;
if (first == NULL)
{
printf("List is empty!!!\n");
return;
}
while (temp != last)
{
printf("%d-->", temp->num);
temp = temp->next;
}
printf("%d", last->num);
}
Output:struct linked_list_node{
int num;
struct linked_list_node *next;
};
typedef struct linked_list_node snode;
snode *first = NULL;
snode *last = NULL;
void insertNode(int value);
void insertAtFirst(int value);
void deleteFirst();
void deleteLast();
snode *createNode(int value);
void display();
void main()
{
int choice, nValue;
char ch='y';
while (ch == 'y')
{
printf("\n------------------------------------------------------------\n");
printf("\tCircular Linked List\n");
printf("------------------------------------------------------------\n");
printf("\n1. Insert a node at last\n");
printf("2. Insert a node at first\n");
printf("3. Delete First Node\n");
printf("4. Delete Last Node\n");
printf("5.Display contents of list\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("\nInserting a new node at last.....\n");
printf("Enter the value of node: ");
scanf("%d", &nValue);
insertNode(nValue);
break;
case 2:
printf("\nInserting a node at first.....\n");
printf("Enter the value of node: ");
scanf("%d", &nValue);
insertAtFirst(nValue);
break;
case 3:
printf("\nDeleteing first node.....\n");
deleteFirst();
break;
case 4:
printf("\nDeleteing last node.....\n");
deleteLast();
break;
case 5:
printf("\nContents of linked list are as follows\n");
display();
break;
default:
printf("\nInvalid choice!!!\n");
}
printf("\nDo you want to continue(y/n)?" );
scanf(" %c", &ch);
}
}
snode *createNode(int value)
{
snode *newNode;
newNode = (snode *)malloc(sizeof(snode));
newNode->num = value;
newNode->next = NULL;
return newNode;
}
void insertNode(int value)
{
snode *newNode;
newNode = createNode(value);
if (first == last && first == NULL)
{
newNode->next = first;
first = last = newNode;
}
else
{
last->next = newNode;
newNode->next = first;
last = newNode;
}
}
void insertAtFirst(int value)
{
snode *newNode;
newNode = createNode(value);
newNode->next = first;
last->next = newNode;
first = newNode;
}
void deleteFirst()
{
if (first == NULL)
{
printf("List is empty!!!\n");
}
else if (first == last)
{
first = last = NULL;
printf("Node successfully deleted!!!\n");
}
else
{
first = first->next;
printf("Node successfully deleted!!!\n");
}
}
void deleteLast()
{
snode *temp;
if (first == NULL)
{
printf("List is empty!!!\n");
}
else if (first == last)
{
first = last = NULL;
printf("Node successfully deleted!!!\n");
}
else
{
temp = first;
while (temp)
{
if (temp->next == last)
{
break;
}
temp = temp->next;
}
last = temp;
printf("Node successfully deleted!!!\n");
}
}
void display()
{
snode *temp = first;
if (first == NULL)
{
printf("List is empty!!!\n");
return;
}
while (temp != last)
{
printf("%d-->", temp->num);
temp = temp->next;
}
printf("%d", last->num);
}
----------------------------------------------------------
Circular Linked List
----------------------------------------------------------
1. Insert a node at last
2. Insert a node at first
3. Delete First Node
4. Delete Last Node
5. Display contents of list
Enter your choice: 1
Inserting a new node at last.....
Enter the value of node: 5
Do you want to continue(y/n)?y
----------------------------------------------------------
Circular Linked List
----------------------------------------------------------
1. Insert a node at last
2. Insert a node at first
3. Delete First Node
4. Delete Last Node
5. Display contents of list
Enter your choice: 1
Inserting a new node at last.....
Enter the value of node: 6
Do you want to continue(y/n)?y
----------------------------------------------------------
Circular Linked List
----------------------------------------------------------
1. Insert a node at last
2. Insert a node at first
3. Delete First Node
4. Delete Last Node
5. Display contents of list
Enter your choice: 5
Contents of linked list are as follows
5-->6
Do you want to continue(y/n)?y
----------------------------------------------------------
Circular Linked List
----------------------------------------------------------
1. Insert a node at last
2. Insert a node at first
3. Delete First Node
4. Delete Last Node
5. Display contents of list
Enter your choice: 2
Inserting a node at first.....
Enter the value of node: 4
Do you want to continue(y/n)?y
----------------------------------------------------------
Circular Linked List
----------------------------------------------------------
1. Insert a node at last
2. Insert a node at first
3. Delete First Node
4. Delete Last Node
5. Display contents of list
Enter your choice: 5
Contents of linked list are as follows
4-->5-->6
Do you want to continue(y/n)?y
----------------------------------------------------------
Circular Linked List
----------------------------------------------------------
1. Insert a node at last
2. Insert a node at first
3. Delete First Node
4. Delete Last Node
5. Display contents of list
Enter your choice: 3
Deleting first node.....
Node successfully deleted!!!
Do you want to continue(y/n)?y
----------------------------------------------------------
Circular Linked List
----------------------------------------------------------
1. Insert a node at last
2. Insert a node at first
3. Delete First Node
4. Delete Last Node
5. Display contents of list
Enter your choice: 5
Contents of linked list are as follows
5-->6
Do you want to continue(y/n)?y
----------------------------------------------------------
Circular Linked List
----------------------------------------------------------
1. Insert a node at last
2. Insert a node at first
3. Delete First Node
4. Delete Last Node
5.Display contents of list
Enter your choice: 4
Deleteing last node.....
Node successfully deleted!!!
Do you want to continue(y/n)?y
----------------------------------------------------------
Circular Linked List
----------------------------------------------------------
1. Insert a node at last
2. Insert a node at first
3. Delete First Node
4. Delete Last Node
5. Display contents of list
Enter your choice: 5
Contents of linked list are as follows
5
Do you want to continue(y/n)?
Circular Linked List
----------------------------------------------------------
1. Insert a node at last
2. Insert a node at first
3. Delete First Node
4. Delete Last Node
5. Display contents of list
Enter your choice: 1
Inserting a new node at last.....
Enter the value of node: 5
Do you want to continue(y/n)?y
----------------------------------------------------------
Circular Linked List
----------------------------------------------------------
1. Insert a node at last
2. Insert a node at first
3. Delete First Node
4. Delete Last Node
5. Display contents of list
Enter your choice: 1
Inserting a new node at last.....
Enter the value of node: 6
Do you want to continue(y/n)?y
----------------------------------------------------------
Circular Linked List
----------------------------------------------------------
1. Insert a node at last
2. Insert a node at first
3. Delete First Node
4. Delete Last Node
5. Display contents of list
Enter your choice: 5
Contents of linked list are as follows
5-->6
Do you want to continue(y/n)?y
----------------------------------------------------------
Circular Linked List
----------------------------------------------------------
1. Insert a node at last
2. Insert a node at first
3. Delete First Node
4. Delete Last Node
5. Display contents of list
Enter your choice: 2
Inserting a node at first.....
Enter the value of node: 4
Do you want to continue(y/n)?y
----------------------------------------------------------
Circular Linked List
----------------------------------------------------------
1. Insert a node at last
2. Insert a node at first
3. Delete First Node
4. Delete Last Node
5. Display contents of list
Enter your choice: 5
Contents of linked list are as follows
4-->5-->6
Do you want to continue(y/n)?y
----------------------------------------------------------
Circular Linked List
----------------------------------------------------------
1. Insert a node at last
2. Insert a node at first
3. Delete First Node
4. Delete Last Node
5. Display contents of list
Enter your choice: 3
Deleting first node.....
Node successfully deleted!!!
Do you want to continue(y/n)?y
----------------------------------------------------------
Circular Linked List
----------------------------------------------------------
1. Insert a node at last
2. Insert a node at first
3. Delete First Node
4. Delete Last Node
5. Display contents of list
Enter your choice: 5
Contents of linked list are as follows
5-->6
Do you want to continue(y/n)?y
----------------------------------------------------------
Circular Linked List
----------------------------------------------------------
1. Insert a node at last
2. Insert a node at first
3. Delete First Node
4. Delete Last Node
5.Display contents of list
Enter your choice: 4
Deleteing last node.....
Node successfully deleted!!!
Do you want to continue(y/n)?y
----------------------------------------------------------
Circular Linked List
----------------------------------------------------------
1. Insert a node at last
2. Insert a node at first
3. Delete First Node
4. Delete Last Node
5. Display contents of list
Enter your choice: 5
Contents of linked list are as follows
5
Do you want to continue(y/n)?
0 comments:
Post a Comment