// Basic operations on Linked List
//
// Author : Shubham Kumar @realshubham1007
// Date : 11/05/21
// Time : 02:38 GMT
//
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head;
int count;
void create()
{
struct node *newnode, *temp;
head = 0;
int choice;
choice = count = 1;
while (choice)
{
newnode = (struct node *)malloc(sizeof(struct node));
printf("Enter data in %d node\n", count);
scanf("%d", &newnode->data);
newnode->next = 0;
if (head == 0)
{
head = temp = newnode;
}
else
{
temp->next = newnode;
temp = newnode;
}
printf("Do you want to insert more node(0,1)?\n");
scanf("%d", &choice);
count++;
}
}
void insertAtBeg()
{
struct node *newnode;
newnode = (struct node *)malloc(sizeof(struct node));
printf("Enter data you want to insert :\n");
scanf("%d", &newnode->data);
newnode->next = head;
head = newnode;
count++;
}
insertAtEnd()
{
struct node *newnode, *temp;
newnode = (struct node *)malloc(sizeof(struct node));
printf("Enter data you want to insert :\n");
scanf("%d", &newnode->data);
newnode->next = 0;
temp = head;
while (temp->next != 0)
{
temp = temp->next;
}
temp->next = newnode;
count++;
}
insertAtPos()
{
int pos, i = 1;
struct node *newnode, *temp;
newnode = (struct node *)malloc(sizeof(struct node));
printf("Enter the position :\n");
scanf("%d", &pos);
if (pos > count)
{
printf("Invalid Position");
}
else if (pos == 1)
{
insertAtBeg();
}
else
{
temp = head;
while (i < pos - 1)
{
temp = temp->next;
i++;
}
printf("Enter data you want to insert :\n");
scanf("%d", &newnode->data);
newnode->next = temp->next;
temp->next = newnode;
count++;
}
}
DeleteFromBeg()
{
struct node *temp;
temp = head;
head = head->next;
free(temp);
count--;
}
DeleteFromEnd()
{
struct node *temp, *prevnode;
temp = head;
while (temp->next != 0)
{
prevnode = temp;
temp = temp->next;
}
if (temp == head)
{
head = 0;
}
else
{
prevnode->next = 0;
}
free(temp);
count--;
}
DeleteFromPos()
{
struct node *temp, *nextnode;
int pos, i = 1;
temp = head;
printf("Enter Position : \n");
scanf("%d", &pos);
if (pos == 1)
{
DeleteFromBeg();
}
else
{
while (i < pos - 1)
{
temp = temp->next;
i++;
}
nextnode = temp->next;
temp->next = nextnode->next;
free(nextnode);
count--;
}
}
void display()
{
printf("Data in list are :\n");
struct node *temp = head;
while (temp != 0)
{
printf("%d ", temp->data);
temp = temp->next;
}
}
void reverse()
{
struct node *prevnode, *currentnode, *nextnode;
prevnode = 0;
currentnode = nextnode = head;
while (nextnode != 0)
{
nextnode = nextnode->next;
currentnode->next = prevnode;
prevnode = currentnode;
currentnode = nextnode;
}
head = prevnode;
}
void option()
{
int choice;
printf("\nWhat operation do you want to perform?\n");
printf("1 ----> Insert at beginning\n");
printf("2 ----> Insert at end\n");
printf("3 ----> Insert at specifird position\n");
printf("4 ----> Delete at beginning\n");
printf("5 ----> Delete at end\n");
printf("6 ----> Delete at specified position\n");
printf("7 ----> Reverse the linked list\n");
printf("8 ----> Display the data in linked list\n");
printf("Press any key then enter to exit.\n");
scanf("%d",&choice);
switch (choice)
{
case 1:
insertAtBeg();
option();
case 2:
insertAtEnd();
option();
case 3:
insertAtPos();
option();
case 4:
DeleteFromBeg();
option();
case 5:
DeleteFromEnd();
option();
case 6:
DeleteFromPos();
option();
case 7:
reverse();
option();
case 8:
display();
option();
default:
break;
}
}
void main()
{
create();
option();
}
0 Comments
Tell us your queries or more topics which you want