0% found this document useful (0 votes)
24 views

Linked List Program

The document contains code to implement a singly linked list and circular linked list in C. It includes functions to create, insert, delete and traverse nodes in the lists. The main function contains a menu to call these functions and test the implementation.

Uploaded by

atharv7740
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Linked List Program

The document contains code to implement a singly linked list and circular linked list in C. It includes functions to create, insert, delete and traverse nodes in the lists. The main function contains a menu to call these functions and test the implementation.

Uploaded by

atharv7740
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Program to implement singly Linked List

# include <stdio.h>
# include <stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head = NULL;
int menu()
{
int ch;
printf("\n 1.Create a list ");
printf("\n--------------------------");
printf("\n 2.Insert a node at beginning ");
printf("\n 3.Insert a node at end");
printf("\n 4.Insert a node at middle");
printf("\n--------------------------");
printf("\n 5.Delete a node from beginning");
printf("\n 6.Delete a node from Last");
printf("\n 7.Delete a node from Middle");
printf("\n--------------------------");
printf("\n 8.Traverse the list (Left to Right)");
printf("\n 9.Traverse the list (Right to Left)");
printf("\n--------------------------");
printf("\n 10. Count nodes ");
printf("\n 11. Exit ");
printf("\n\n Enter your choice: ");
scanf("%d",&ch);
return ch;
}

struct node* getnode()


{
struct node * newnode;
newnode = (struct node *) malloc(sizeof(struct node));
printf("\n Enter data: ");
scanf("%d", &newnode -> data);
newnode -> next = NULL;
return newnode;
}
int countnode(struct node *ptr)
{
int count=0;
while(ptr != NULL)
{ count++;
ptr = ptr -> next;
}
return (count);
}
void createlist(int n)
{
int i;
struct node *newnode;
struct node *temp;
for(i = 0; i < n; i++)
{
newnode = getnode();
if(head == NULL)
{
head = newnode;
}
else
{
temp = head;
while(temp -> next != NULL)
temp = temp -> next;
temp -> next = newnode;
}
}
}
void traverse()
{
struct node *temp;
temp = head;
printf("\n The contents of List (Left to Right): \n");
if(head == NULL)
{
printf("\n Empty List");
return;
}
else
{
while(temp != NULL)
{
printf("%d-->", temp -> data);
temp = temp -> next;
}
}
printf(" X ");
}
void rev_traverse(struct node *head)
{
if(head == NULL)
{
return;
}
else
{

rev_traverse(head -> next);


printf("%d -->", head -> data);
}
}
void insert_at_beg()
{
struct node *newnode;
newnode = getnode();
if(head == NULL)
{
head = newnode;
}
else
{
newnode -> next = head;
head = newnode;
}
}
void insert_at_end()
{
struct node *newnode, *temp;
newnode = getnode();
if(head == NULL)
{
head = newnode;
}
else
{
temp = head;
while(temp -> next != NULL)
temp = temp -> next;
temp -> next = newnode;
}
}
void insert_at_mid()
{
struct node *newnode, *temp, *prev;
int pos, nodectr, i = 1;
newnode = getnode();
printf("\n Enter the position: ");
scanf("%d", &pos);
nodectr = countnode(head);
if(pos > 1 && pos < nodectr)
{
temp = head;
while(i < pos)
{
prev = temp;
temp = temp -> next;
i++;
}
prev -> next = newnode;
newnode -> next = temp;
}
else
printf("position %d is not a middle position", pos);
}
void delete_at_beg()
{
struct node *temp;
if(head == NULL)
{
printf("\n No nodes are exist..");
return ;
}
else
{
temp = head;
head = temp -> next;
free(temp);
printf("\n Node deleted ");
}
}
void delete_at_last()
{
struct node *temp, *prev;
if(head == NULL)
{
printf("\n Empty List..");
return ;
}
else
{
temp = head;
prev = head;
while(temp -> next != NULL)
{
prev = temp;
temp = temp -> next;
}
prev -> next = NULL;
free(temp);
printf("\n Node deleted ");
}
}
void delete_at_mid()
{
int i= 1, pos, nodectr;
struct node *temp, *prev;
if(head == NULL)
{
printf("\n Empty List..");
return ;
}
else
{
printf("\n Enter position of node to delete: ");
scanf("%d", &pos);
nodectr = countnode(head);
if(pos > nodectr)
{ printf("\nThis node doesnot exist");
}
if(pos > 1 && pos < nodectr)
{
temp = head;
while(i < pos)
{
prev = temp;
temp = temp -> next;
i++;
}
prev -> next = temp -> next;
free(temp);
printf("\n Node deleted..");
}
else
{
printf("\n Invalid position..");
}
}
}
int main()
{
int ch, n;
while(1)
{
ch = menu();
switch(ch)
{
case 1:
if(head == NULL)
{
printf("\n Number of nodes you want to create: ");
scanf("%d", &n);
createlist(n);
printf("\n List created..");
break;
}
else
printf("\n List is already created..");
break;
case 2:
insert_at_beg();
break;
case 3:
insert_at_end();
break;
case 4:
insert_at_mid();
break;
case 5:
delete_at_beg();
break;
case 6:
delete_at_last();
break;
case 7:
delete_at_mid();
break;
case 8:
traverse();
break;
case 9:
printf("\n The contents of List (Right to Left): \n");
rev_traverse(head);
printf(" X ");
break;
case 10:
printf("\n No of nodes : %d ", countnode(head));
break;
case 11 :
exit(0);
}

}
}
Program to implement Circular Linked List
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head;

void createList(int n);


void beginsert ();
void lastinsert ();
void begin_delete();
void last_delete();
void display();
int main ()
{
int n;
int choice =0;
while(choice != 7)
{
printf("\n*********Main Menu*********\n");
printf("\nChoose one option from the following list ...\n");
printf("\n===============================================\n");
printf("\n1.create a linked list\n2.Insert in begining\n3.Insert at last\n4.Delete from
Beginning\n5.Delete from last\n6.display\n7.Exit\n");
printf("\nEnter your choice?\n");
scanf("\n%d",&choice);
switch(choice)
{
case 1:
printf("Enter the total number of nodes in list: ");
scanf("%d", &n);
createList(n);
break;
case 2:
beginsert();
break;
case 3:
lastinsert();
break;
case 4:
begin_delete();
break;
case 5:
last_delete();
break;
case 6:
display();
break;
case 7:
exit(0);
break;
default:
printf("Please enter valid choice..");
}
}
}

void createList(int n)
{
int i, data;
struct node *prevNode, *newNode;

if(n >= 1)
{
/*
* Creates and links the head node
*/
head = (struct node *)malloc(sizeof(struct node));

printf("Enter data of 1 node: ");


scanf("%d", &data);

head->data = data;
head->next = NULL;

prevNode = head;

/*
* Creates and links rest of the n-1 nodes
*/
for(i=2; i<=n; i++)
{
newNode = (struct node *)malloc(sizeof(struct node));
printf("Enter data of %d node: ", i);
scanf("%d", &data);

newNode->data = data;
newNode->next = NULL;

// Link the previous node with newly created node


prevNode->next = newNode;

// Move the previous node ahead


prevNode = newNode;
}

// Link the last node with first node


prevNode->next = head;

printf("\nCIRCULAR LINKED LIST CREATED SUCCESSFULLY\n");


}
}

void beginsert()
{
struct node *ptr,*temp;
int item;
ptr = (struct node *)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter the node data?");
scanf("%d",&item);
ptr -> data = item;
if(head == NULL)
{
head = ptr;
ptr -> next = head;
}
else
{
temp = head;
while(temp->next != head)
temp = temp->next;
ptr->next = head;
temp -> next = ptr;
head = ptr;
}
printf("\nnode inserted\n");
}

}
void lastinsert()
{
struct node *ptr,*temp;
int item;
ptr = (struct node *)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW\n");
}
else
{
printf("\nEnter Data?");
scanf("%d",&item);
ptr->data = item;
if(head == NULL)
{
head = ptr;
ptr -> next = head;
}
else
{
temp = head;
while(temp -> next != head)
{
temp = temp -> next;
}
temp -> next = ptr;
ptr -> next = head;
}

printf("\nnode inserted\n");
}
}

void begin_delete()
{
struct node *ptr;
if(head == NULL)
{
printf("\nUNDERFLOW");
}
else if(head->next == head)
{
head = NULL;
free(head);
printf("\nnode deleted\n");
}

else
{ ptr = head;
while(ptr -> next != head)
ptr = ptr -> next;
ptr->next = head->next;
free(head);
head = ptr->next;
printf("\nnode deleted\n");

}
}
void last_delete()
{
struct node *ptr, *preptr;
if(head==NULL)
{
printf("\nUNDERFLOW");
}
else if (head ->next == head)
{
head = NULL;
free(head);
printf("\nnode deleted\n");

}
else
{
ptr = head;
while(ptr ->next != head)
{
preptr=ptr;
ptr = ptr->next;
}
preptr->next = ptr -> next;
free(ptr);
printf("\nnode deleted\n");

}
}
void display()
{
struct node *ptr;
ptr=head;
if(head == NULL)
{
printf("\nnothing to print");
}
else
{
printf("\n printing values ..\n");

while(ptr -> next != head)


{

printf("%d-->", ptr -> data);


ptr = ptr -> next;
}
printf("%d\n", ptr -> data);
}
}

You might also like