Performing all operations in linked list data structure using C Language.
#include<stdio.h>#include<stdlib.h>using namespace std;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();int main(){ int choice=0; printf("\n-------------Main menu-----------------"); printf("\n\n1.Insert in begining\n2.Insert at last\n3.Insert at any random location\n4.Delete from Beginning\n"); printf("5.Delete from last\n6.Delete node at specified location\n7.Show all Elements\n8.Exit\n"); while(choice!= 8) { printf("\n----------------------------------------"); printf("\nEnter your choice From the main menu: "); scanf("%d",&choice); switch(choice) { case 1: beginsert(); break; case 2: lastinsert(); break; case 3: randominsert(); break; case 4: begin_delete(); break; case 5: last_delete(); break; case 6: random_delete(); break; case 7: display(); break; case 8: exit(0); break; default: printf("Ooops...!!\nSomething went wrong..."); } }}void beginsert(){ struct node *ptr; int item; ptr = (struct node*) malloc(sizeof(struct node*)); if(ptr == NULL) { printf("OVERFLOW..."); } else { printf("Enter value to be pushed : "); scanf("%d",&item); ptr->data = item; ptr->next = head; head = ptr; printf("Node inserted..."); }}void lastinsert(){ struct node *ptr,*temp; int item; ptr = (struct node*)malloc(sizeof(struct node*)); if(ptr == NULL) { printf("OVERFLOW..."); } else { printf("Enter value to be pushed : "); scanf("%d",&item); ptr->data = item; if(head == NULL) { ptr -> next = NULL; head = ptr; printf("Node inserted..."); } else { temp = head; while (temp -> next!= NULL) { temp = temp -> next; } temp->next = ptr; ptr->next = NULL; printf("Node inserted..."); } }}void randominsert()
{
int i,loc,item;
struct node *ptr, *temp;
ptr = (struct node *) malloc (sizeof(struct node*));
if(ptr == NULL)
{
printf("OVERFLOW...");
}
else
{
printf("Enter value to be pushed : ");
scanf("%d",&item);
ptr->data = item;
printf("Enter the location after which you want to insert : ");
scanf("%d",&loc);
temp=head;
for(i=0;i<loc;i++)
{
temp = temp->next;
if(temp == NULL)
{
printf("Can't insert...");
return;
}
}
ptr ->next = temp ->next;
temp ->next = ptr;
printf("Node inserted...");
}
}
void begin_delete()
{
struct node *ptr;
if(head == NULL)
{
printf("Empty List...");
}
else
{
ptr = head;
head = ptr->next;
free(ptr);
printf("Node deleted at the begining...");
}
}
void last_delete()
{
struct node *ptr,*ptr1;
if(head == NULL)
{
printf("Empty List...");
}
else if(head -> next == NULL)
{
head = NULL;
free(head);
printf("Node deleted at last...");
}
else
{
ptr = head;
while(ptr->next != NULL)
{
ptr1 = ptr;
ptr = ptr ->next;
}
ptr1->next = NULL;
free(ptr);
printf("Node deleted at last...");
}
}
void random_delete()
{
struct node *ptr,*ptr1;
int loc,i;
printf("Enter the location of the node which you want to delete : ");
scanf("%d",&loc);
ptr=head;
for(i=0;i<loc;i++)
{
ptr1 = ptr;
ptr = ptr->next;
if(ptr == NULL)
{
printf("Can't delete...");
return;
}
}
ptr1 ->next = ptr ->next;
free(ptr);
printf("Deleted node %d ",loc+1);}
void display()
{
struct node *ptr;
ptr = head;
if(ptr == NULL)
{
printf("Nothing to print...");
}
else
{
printf("Printing values....");
while (ptr!=NULL)
{
printf("\n%d",ptr->data);
ptr = ptr -> next;
}
}
}
You mast run the above code using Dev C++ compiler to get the exact output given below -
Output -
-------------Main menu-----------------
1.Insert in beginning2.Insert at last3.Insert at any random location4.Delete from Beginning5.Delete from last6.Delete node at specified location7.Show all Elements8.Exit
---------------------------------------
Enter your choice From the main menu : 1Enter value to be pushed : 11Node inserted...---------------------------------------
Enter your choice From the main menu : 2Enter value to be pushed : 22Node inserted...---------------------------------------
Enter your choice From the main menu : 3Enter value to be pushed : 33Enter the location after which you want to insert : 1Node inserted...---------------------------------------
Enter your choice From the main menu : 7Printing values....112233---------------------------------------
Enter your choice From the main menu : 4Node deleted at the beginning...---------------------------------------
Enter your choice From the main menu : 5Node deleted at last...---------------------------------------
Enter your choice From the main menu : 7Printing values....22---------------------------------------
Enter your choice From the main menu : 8
--------------------------------Process exited after 83.38 seconds with return value 0Press any key to continue . .
Also check out : Stack Data-Structure Program in C++ Language - Learn With AVRK