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

linklistall

The document contains C code implementations for various types of linked lists, including circular singly linked lists, circular doubly linked lists, doubly linked lists, and singly linked lists. Each implementation provides functions for inserting, deleting, displaying, and searching for elements within the lists. The main function in each implementation presents a menu-driven interface for users to interact with the linked list operations.

Uploaded by

pofomax827
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

linklistall

The document contains C code implementations for various types of linked lists, including circular singly linked lists, circular doubly linked lists, doubly linked lists, and singly linked lists. Each implementation provides functions for inserting, deleting, displaying, and searching for elements within the lists. The main function in each implementation presents a menu-driven interface for users to interact with the linked list operations.

Uploaded by

pofomax827
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 19

==========================================CircluarSingly.

#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head;

void beginsert();
void lastinsert();
void randominsert();
void begin_delete();
void last_delete();
void random_delete();
void display();
void search();

int main()
{
int choice = 0;
while (choice != 7)
{
printf("\n....Main Menu....\n");

printf("\nChoose one option from the following list...\n");


printf("\nl.Insert in begining\n2.Insert at last\n3.Delete from Beginning\
n4.Delete from last\n5.Search for an element\n6.Show\n7.Exit\n");

printf("\nEnter your choice: ");


scanf("%d", &choice); // choice=3
switch (choice)
{
case 1:
beginsert();
break;
case 2:
lastinsert();
break;
case 3:
begin_delete();
break;
case 4:
last_delete();
break;
case 5:
search();
break;
case 6:
display();
break;
case 7:
exit(0);
break;
default:
printf("Please enter valid choice..");
}
}
}
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
{
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
{
ptr = head;
while (ptr->next != head)
{
preptr = ptr;

ptr = ptr->next;
}
preptr->next = ptr->next;
free(ptr);
printf("\nnode deleted\n");
}
}
void search()
{
struct node *ptr;
int item, i = 0, flag;
ptr = head;
if (ptr == NULL)
{
printf("\nEmpty List\n");
}
else
{
printf("\nEnter searching item: ");
scanf("%d", &item);
while (ptr != NULL)
{
if (head->data == item)
{
printf("Item found at location %d ", i + 1);
flag = 0;
break;
}
else
{
flag = 1;
}
i++;
ptr = ptr->next;
}
if (flag == 1)
{
printf("Item not found\n");
}
}
}
void display()
{
struct node *ptr;
ptr = head;

if (head == NULL)
{
printf("\nNothing to print ");
}
else
{
printf("\nPrinting values: ");
while (ptr->next != head)
{
printf("%d\n", ptr->data);
ptr = ptr->next;
}
printf("%d\n", ptr->data);
}
}

==============================================CircularDoubly.c

#include <stdlib.h>
#include <stdio.h>
struct node
{
int data;
struct node *prev;
struct node *next;
};
struct node *head;

void insertion_beginning();
void insertion_last();
void deletion_beginning();
void deletion_last();
void display();
void search();

int main()
{
int choice = 0;
while (choice != 7)
{
printf("\n*Main Menu*****\n");

printf("\nChoose one option from the following list...\n");


printf("\nl.Insert in Beginning\n2.Insert at last\n3.Delete from Beginning\
n4. Delete from last\n5.Search\n6.Show\n7.Exit\n");

printf("\nEnter your choice: ");


scanf("\n%d", &choice);

switch (choice)
{
case 1:
insertion_beginning();
break;
case 2:
insertion_last();
break;
case 3:
deletion_beginning();
break;
case 4:
deletion_last();
break;
case 5:
search();
break;
case 6:
display();
break;
case 7:
exit(0);
break;
default:
printf("Please enter valid choice..");
}
}
}
void insertion_beginning()
{

struct node *ptr, *temp;


int item;
ptr = (struct node *)malloc(sizeof(struct node));
if (ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter Item value");
scanf("%d", &item);

ptr->data = item;
if (head == NULL)
{
head = ptr;
ptr->next = head;
ptr->prev = head;
}
else
{
temp = head;

while (temp->next != head)


{
temp = temp->next;
}
temp->next = ptr;
ptr->prev = temp;
head->prev = ptr;
ptr->next = head;
head = ptr;
}
printf("\nNode inserted\n");
}
}
void insertion_last()
{
struct node *ptr, *temp;
int item;

ptr = (struct node *)malloc(sizeof(struct node));


if (ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter value");
scanf("%d", &item);
ptr->data = item;
if (head == NULL)
{
head = ptr;
ptr->next = head;
ptr->prev = head;
}
else
{
temp = head;
while (temp->next != head)
{
temp = temp->next;
}
temp->next = ptr;
ptr->prev = temp;
head->prev = ptr;
ptr->next = head;
}
}
printf("\nNode inserted\n");
}
void deletion_beginning()
{
struct node *temp;

if (head == NULL)
{
printf("\nUNDERFLOW");
}
else if (head->next == head)
{
head = NULL;
free(head);

printf("\nNode deleted\n");
}
else
{
temp = head;
while (temp->next != head)
{
temp = temp->next;
}
temp->next = head->next;
head->next->prev = temp;
free(head);
head = temp->next;
}
}
void deletion_last()
{
struct node *ptr;

if (head == NULL)
{
printf("\nUNDERFLOW");
}
else if (head->next == head)
{
head = NULL;
free(head);
printf("Item Deleted\n ");
}
else
{
ptr = head;
if (ptr->next != head)
{
ptr = ptr->next;
}
ptr->prev->next = head;
head->prev = ptr->prev;
free(ptr);
printf("\nItem deleted\n");
}
}
void display()
{
struct node *ptr;

ptr = head;

if (head == NULL)
{
printf("\nNothing to print");
}
else
{
printf("\nPrinting values \n");
while (ptr->next != head)
{
printf("%d\n", ptr->data);
ptr = ptr->next;
}
printf("%d\n", ptr->data);
}
}
void search()
{
struct node *ptr;
int item, i = 0, flag = 1;
ptr = head;
if (ptr == NULL)
{
printf("\nEmpty List\n");
}
else
{
printf("\nEnter item which you want to search: ");
scanf("%d", &item);
if (head->data == item)
{
printf("Item found at location %d", i + 1);
flag = 0;
}
else
{
while (ptr->next != head)
{

if (ptr->data == item)
{
printf("Item found at location %d ", i + 1);
flag = 0;
break;
}
else
{
flag = 1;
}
i++;
ptr = ptr->next;
}
}
if (flag != 0)
{
printf("Item not found\n");
}
}
}

=================================================DoublyLinkedList.c

#include <stdio.h>
#include <stdlib.h>
struct node
{
struct node *prev;
struct node *next;
int data;
};
struct node *head;

void insertion_beginning();
void insertion_last();
void insertion_specified();
void deletion_beginning();
void deletion_last();
void deletion_specified();
void display();
void search();
int main()
{
int choice = 0;
while (choice != 9)
{
printf("\n*....Main Menu*****\n");
printf("\nChoose one option from the following list...\n");
printf("\nl.Insert in begining\n2.Insert at last\n3.Insert at any random
location\n4.Delete from Beginning\n 5. Delete from last\n6.Delete the node after
the given data\n7.Search\n8.Show\n9.Exit\n ");
printf("\nEnter your choice: ");
scanf("\n%d", &choice);

switch (choice)
{
case 1:
insertion_beginning();
break;
case 2:
insertion_last();
break;
case 3:
insertion_specified();
break;
case 4:
deletion_beginning();
break;
case 5:
deletion_last();
break;
case 6:
deletion_specified();
break;
case 7:
search();
break;
case 8:
display();
break;
case 9:
exit(0);
break;
default:
printf("Please enter valid choice..");
}
}
}
void insertion_beginning()
{
struct node *ptr;
int item;
ptr = (struct node *)malloc(sizeof(struct node));
if (ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter Item value");
scanf("%d", &item);
if (head == NULL)
{
ptr->next = NULL;
ptr->prev = NULL;
ptr->data = item;
head = ptr;
}
else
{
ptr->data = item;
ptr->prev = NULL;
ptr->next = head;
head->prev = ptr;
head = ptr;
}
printf("\nNode inserted\n");
}
}
void insertion_last()
{
struct node *ptr, *temp;
int item;
ptr = (struct node *)malloc(sizeof(struct node));

if (ptr == NULL)
{

printf("\nOVERFLOW");
}
else
{
printf("\nEnter value");
scanf("%d", &item);
ptr->data = item;
if (head == NULL)
{
ptr->next = NULL;
ptr->prev = NULL;
head = ptr;
}
else
{
temp = head;
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = ptr;
ptr->prev = temp;
ptr->next = NULL;
}
}
printf("innode Inserted\n");
}
void insertion_specified()
{
struct node *ptr, *temp;
int item, loc, i;
ptr = (struct node *)malloc(sizeof(struct node));

if (ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("Enter the value");
scanf("%d", &item);

ptr->data = item;
printf("Enter the location");
scanf("%d", &loc);
temp = head;
for (i = 1; i < loc; i++)
{
temp = temp->next;
if (temp == NULL)
{
printf("\n There are less than %d elements", loc);
return;
}
}
ptr->next = temp->next;
ptr->prev = temp;
temp->next = ptr;
temp->next->prev = ptr;
printf("\nnode inserted\n");
}
}
void deletion_beginning()
{
struct node *ptr;
if (head == NULL)
{
printf("\n UNDERFLOW");
}
else if (head->next == NULL)
{
head = NULL;
free(head);
printf("\nnode deleted\n");
}
else
{
ptr = head;

head = head->next;
head->prev = NULL;
free(ptr);

printf("\nnode deleted\n");
}
}
void deletion_last()
{
struct node *ptr;
if (head == NULL)
{
printf("\nUNDERFLOW");
}
else if (head->next == NULL)
{
head = NULL;
free(head);
printf("\nnode deleted\n");
}
else
{
ptr = head;
while (ptr->next != NULL)
{
ptr = ptr->next;
}
ptr->prev->next = NULL;
free(ptr);
printf("\nnode deleted\n");
}
}
void deletion_specified()
{
struct node *ptr, *temp;
int loc;
printf("\n Enter the location node is to be deleted: ");
scanf("%d", &loc);
ptr = head;
while (ptr->data != loc)
ptr = ptr->next;
if (ptr->next == NULL)
{
printf("\nCan't delete\n");
}
else if (ptr->next->next == NULL)
{
ptr->next = NULL;
}

else
{
temp = ptr->next;
ptr->next = temp->next;
temp->next->prev = ptr;
free(temp);
printf("\nnode deleted\n");
}
}
void display()
{
struct node *ptr;
ptr = head;
if (ptr == NULL)
{
printf("Nothing to print");
}
else
{
printf("\nprinting values while (ptr!=NULL) .\n");
while (ptr != NULL)
{
printf("\n%d", ptr->data);
ptr = ptr->next;
}
}
}
void search()
{
struct node *ptr;
int item, i = 0, flag;
ptr = head;
if (ptr == NULL)
{
printf("\nEmpty List\n");
}
else
{
printf("\nEnter item which you want to search?\n");
scanf("%d", &item);

while (ptr != NULL)


{
if (ptr->data == item)
{
printf("\nitem found at location %d ", i + 1);
flag = 0;
break;
}
else
{
flag = 1;
}
i++;
ptr = ptr->next;
}
if (flag == 1)
{
printf("\nItem not found\n");
}
}
}

============================================SinglyLinkedList.c

#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head;

void beginsert();
void lastinsert();
void random_insert();
void begin_delete();
void last_delete();
void random_delete();
void display();
void search();

int main()
{
int choice = 0;
while (choice != 9)
{
printf("\n...Choose One Option from the following List...\n");
printf("\nl.Insert in begining\n2.Insert at last\n3.Insert item at any
location\n4.Delete from Beginning\n5.Delete from last\n6.Delete item from any
location\n7.Search\n8.Show\n9.Exit\n");

printf("\nEnter your choice: ");


scanf("\n%d", &choice); // choice=1

switch (choice)
{
case 1:
beginsert();
break;
case 2:
lastinsert();
break;
case 3:
random_insert();
break;
case 4:
begin_delete();
break;
case 5:
last_delete();
break;
case 6:
random_delete();
break;
case 7:
search();
break;
case 8:
display();
break;
case 9:
exit(0);
break;
default:
printf("Invalid Choice..");
}
}
return 0;
}
void beginsert()
{
struct node *ptr;
int item;
ptr = (struct node *)malloc(sizeof(struct node *));
if (ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter value: ");
scanf("%d", &item);
ptr->data = item;
ptr->next = head;
head = ptr;
printf("Item inserted");
}
}
void lastinsert()
{
struct node *ptr, *temp;
int item;
ptr = (struct node *)malloc(sizeof(struct node));
if (ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter value: ");
scanf("%d", &item);
ptr->data = item;
if (head == NULL)
{
ptr->next = NULL;
head = ptr;
printf("Item inserted");
}
else
{
temp = head;
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = ptr;
ptr->next = NULL;
printf("Item inserted");
}
}
}
void random_insert()
{
int i, loc, item;
struct node *ptr, *temp;
ptr = (struct node *)malloc(sizeof(struct node));
if (ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("Enter Value: ");
scanf("%d", &item);
ptr->data = item;
printf("\nEnter the location: ");
scanf("%d", &loc);
temp = head;
for (i = 1; i < loc; i++)
{
temp = temp->next;
if (temp == NULL)
{
printf("\ncan't insert\n");
return;
}
}
ptr->next = temp->next;
temp->next = ptr;
printf("Item inserted");
}
}
void begin_delete()
{
struct node *ptr;
if (head == NULL)
{
printf("\nList is empty\n");
}
else
{
ptr = head;
head = ptr->next;
free(ptr);
printf("Item deleted from the begining ...\n");
}
}
void last_delete()
{
struct node *ptr, *ptrl;
if (head == NULL)
{
printf("\nList is empty");
}
else if (head->next == NULL)
{
head = NULL;
free(head);
printf("\nOnly node of the list deleted ..\n");
}
else
{
ptr = head;
while (ptr->next != NULL)
{
ptrl = ptr;
ptr = ptr->next;
}
ptrl->next = NULL;
free(ptr);
printf("Item deleted\n");
}
}
void random_delete()
{
struct node *ptr, *ptrl;
int loc, i;

printf("\n Enter the location: ");


scanf("%d", &loc);
ptr = head;
for (i = 0; i < loc; i++)
{
ptrl = ptr;
ptr = ptr->next;
if (ptr == NULL)
{
printf("\nCan't delete");
return;
}
}
ptrl->next = ptr->next;
free(ptr);
printf("Item deleted at loc %d", loc);
}
void search()
{
struct node *ptr;
int item, i = 0, flag;
ptr = head;
if (ptr == NULL)
{
printf("\nEmpty List\n");
}
else
{
printf("\nEnter searching item: ");
scanf("%d", &item);
while (ptr != NULL)
{
if (ptr->data == item)
{
printf("Item found at location %d ", i + 1);
flag = 0;
}
else
{
flag = 1;
}
i++;
ptr = ptr->next;
}
if (flag == 1)
{
printf("Item not found\n");
}
}
}
void display()
{
struct node *ptr;
ptr = head;
if (ptr == NULL)
{
printf("Nothing to print");
}
else
{
printf("\nPrinting values while (ptr!=NULL).\n");
while (ptr != NULL)
{
printf("%d\n", ptr->data);
ptr = ptr->next;
}
}
}

You might also like