Skip to content

Instantly share code, notes, and snippets.

@AakashCode12
Created August 19, 2020 07:54
Show Gist options
  • Save AakashCode12/77e87b433f9eb62d0abb11aee4968ae2 to your computer and use it in GitHub Desktop.
Save AakashCode12/77e87b433f9eb62d0abb11aee4968ae2 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
//todo structure & pointers declaration
struct node
{
int data;
struct node *next;
};
struct node *head, *newnode, *temp, *backtemp;
int flagforstart = 0;
//?-------------------------------------------------------------------------
// todo function prototyping
void normalCreation(); //?---1
void insertBeginning(); //*---2
void insertEnd(); //?---3
void insertBefore(); //*---4
void insertAfter(); //?---5
void deleteStart(); //*---6
void deleteEnd(); //?---7
void deleteBefore(); //*---8
void deleteAfter(); //?---9
void deleteNode(); //*---10
void searchNode(); //?---11
void display(); //*---12
//?-------------------------------------------------------------------------
//todo function -- Normal creation 1 (done)
void normalCreation()
{
printf("\nHow many elements You want to add initially\n");
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
if (flagforstart == 0)
{
head = 0;
newnode = (struct node *)malloc(sizeof(struct node));
printf("\nEnter The Value you want to Add : ");
scanf("%d", &newnode->data);
newnode->next = 0;
head = temp = newnode;
flagforstart++; //!indicates one linked list is added
}
else
{
newnode = (struct node *)malloc(sizeof(struct node));
printf("\nEnter The Value you want to Add : ");
scanf("%d", &newnode->data);
newnode->next = 0;
temp->next = newnode;
temp = newnode;
flagforstart++; //!indicates one linked list is added
}
}
printf("\nThe initial Linked List is created\n");
}
//todo function -- Insert Beginning 2 (done)
void insertBeginning()
{
if (flagforstart == 0)
{ //!if no element is present in Linked List
newnode = (struct node *)malloc(sizeof(struct node));
printf("\nEnter the value you want to add : ");
scanf("%d", &newnode->data);
newnode->next = 0; //!ab ye end hai to isko bhi zero denge neto display func me masti hoga
head = newnode;
}
else
{
newnode = (struct node *)malloc(sizeof(struct node));
printf("\nEnter the value you want to add : ");
scanf("%d", &newnode->data);
newnode->next = head;
head = newnode;
}
flagforstart++;
}
//todo function -- Insert End 3 (done)
void insertEnd()
{
if (flagforstart == 0)
{ //!if no element is present in Linked List
newnode = (struct node *)malloc(sizeof(struct node));
printf("\nEnter the value you want to add : ");
scanf("%d", &newnode->data);
newnode->next = 0; //!ab ye end hai to isko bhi zero denge neto display func me masti hoga
head = newnode;
}
else
{
newnode = (struct node *)malloc(sizeof(struct node));
printf("\nEnter the data you want to insert at End of Linked List: ");
scanf("%d", &newnode->data);
newnode->next = 0;
temp = head; //*temporary pointer now points to head that is start of linked list
//*now lets traverse til we get 0 in next pointer (next is the pointer which stores the value of end node )
while (temp->next != 0)
{
temp = temp->next;
}
temp->next = newnode;
}
flagforstart++;
}
//todo function -- Insert Before 4 (done)
void insertBefore()
{
if (flagforstart == 0)
{
printf("\nThe Linked List is Empty. Add Elements to the Linked List use this function\n");
}
else if (flagforstart == 1)
{
printf("\nEnter the Data Before which you want to add the Node : ");
int num;
scanf("%d", &num);
if (head->data == num)
{
newnode = (struct node *)malloc(sizeof(struct node));
printf("\nEnter the value you want to add : ");
scanf("%d", &newnode->data);
newnode->next = head;
head = newnode;
flagforstart++;
}
else
{
printf("\nNo Element As Such in The Linked List\n");
}
}
else
{
printf("\nEnter the Data Before which you want to add the Node : ");
int num;
scanf("%d", &num);
temp = head;
while (1)
{
if (temp->next->data == num)
{
newnode = (struct node *)malloc(sizeof(struct node));
printf("\nEnter the value you want to add : ");
scanf("%d", &newnode->data);
newnode->next = temp->next;
temp->next = newnode;
flagforstart++;
break;
}
else if (temp->next->next == 0 && temp->next->data != num)
{
printf("\nNo Element as Such in the Linked List\n");
break;
}
else
{
temp = temp->next;
}
}
}
}
//todo function -- Insert After 5 (done)
void insertAfter()
{
if (flagforstart == 0)
{
printf("\nThe Linked List is Empty. Add Elements to the Linked List use this function\n");
}
else
{
printf("\nEnter the data After which you want to add the Node : ");
int num;
scanf("%d", &num);
temp = head;
while (1)
{
if (temp->data == num)
{
newnode = (struct node *)malloc(sizeof(struct node));
printf("\nEnter The Value you want to Add : ");
scanf("%d", &newnode->data);
if (temp->next == 0)
{ //*if we have last node
newnode->next = 0;
temp->next = newnode;
}
else
{
newnode->next = temp->next;
temp->next = newnode;
}
break;
}
else if (temp->data != num && temp->next == 0)
{
printf("\nNo element as such\n");
flagforstart--; //!no element is added here so decrement as function end pe we have ++
break;
}
else
{
temp = temp->next;
}
}
flagforstart++;
}
}
//todo function -- Delete Start 6 (done)
void deleteStart()
{
if (flagforstart == 0)
{
printf("\nThe Linked List is Empty\n");
}
else
{
printf("\nThe element you deleted is : %d\n", head->data);
temp = head;
head = head->next;
free(temp);
flagforstart--;
}
}
//todo function -- Delete End 7 (done)
void deleteEnd()
{
if (flagforstart == 0)
{
printf("\nThe Linked List is Empty\n");
}
else if (flagforstart == 1)
{
printf("\nThe element you deleted is : %d\n", head->data);
temp = head;
free(temp);
flagforstart--;
}
else
{
temp = head;
while (temp->next->next != 0)
{
temp = temp->next;
}
printf("\nThe element you deleted is : %d\n", temp->next->data);
temp->next = 0;
temp = temp->next;
free(temp);
flagforstart--;
}
}
//todo function -- Delete Before 8 (done)
void deleteBefore()
{
if (flagforstart == 0 && flagforstart == 1)
{
printf("\nTo Use This Function You must have at least 2 Elements in the Linked List\n");
}
else if (flagforstart == 2)
{
printf("\nEnter the data Before which you want to Delete the Node : ");
int num;
scanf("%d", &num);
if (head->next->data == num)
{
temp = head;
head = head->next;
printf("\nElement You Deleted is : %d\n", temp->data);
free(temp);
flagforstart--;
}
else
{
printf("\nNo element as such :-| \n");
}
}
else
{
printf("\nEnter the data Before which you want to Delete the Node : ");
int num;
scanf("%d", &num);
temp = head;
while (1)
{
if (temp->next->next->data == num)
{
backtemp = temp->next; //not used back temp its just wrong name
temp->next = temp->next->next;
printf("\nElement You Deleted is : %d\n", backtemp->data);
free(backtemp);
flagforstart--;
break;
}
else if (temp->next->next == 0 && temp->next->data != num)
{
printf("\nNo element as such\n");
break;
}
else
{
temp = temp->next;
}
}
}
}
//todo function -- Delete After 9 (done)
void deleteAfter()
{
if (flagforstart == 0 && flagforstart == 1)
{
printf("\nTo Use This Function You must have at least 2 Elements in the Linked List\n");
}
else
{
printf("\nEnter the data After which you want to Delete the Node : ");
int num;
scanf("%d", &num);
temp = head;
while (1)
{
if (temp->data == num && temp->next != 0)
{
if (temp->next->next == 0)
{
//!when next wala is the last node
backtemp = temp->next;
temp->next = 0;
}
else
{ //*When next to the node we have more nodes
backtemp = temp->next;
temp->next = temp->next->next;
}
printf("\nElement You Deleted is : %d\n", backtemp->data);
free(backtemp);
flagforstart--;
break;
}
else if (temp->data != num && temp->next == 0)
{
printf("\nNo element as such\n");
break;
}
else if (temp->data == num && temp->next == 0)
{
printf("\nThere is no element after this node bro ..Cmon man be cool\n");
break;
}
else
{
temp = temp->next;
}
}
}
}
//todo function -- Delete Specified Node 10 (done)
void deleteNode()
{
if (flagforstart == 0)
{
printf("\nThere are no Elements Present in the Linked List\n");
}
else if (flagforstart == 1)
{
printf("\nEnter the data of the node you want to delete : ");
int num;
scanf("%d", &num);
if (head->data == num)
{
temp = head;
printf("\nThe Element you Removed is : %d \n", temp->data);
free(temp);
flagforstart--;
}
else
{
printf("\nThe Data is not present :-| \n");
}
}
else
{
printf("\nEnter the data of the node you want to delete : ");
int num;
scanf("%d", &num);
temp = head;
while (1)
{
if (temp->next->data == num)
{
backtemp = temp->next;
temp->next = temp->next->next;
printf("\nThe Value you Deleted is : %d\n", backtemp->data);
free(backtemp);
break;
}
else if (temp->next->next == 0)
{
printf("\nNo element as Such \n");
break;
}
else
{
temp = temp->next;
}
}
flagforstart--;
}
}
//todo function -- Search Node 11 (done)
void searchNode()
{
if (flagforstart == 0)
{
printf("\nThe Linked List is Empty\n");
}
else
{
printf("\nEnter the Data You want to Search : ");
int num;
scanf("%d", &num);
temp = head;
int nodeno = 1;
while (1) //?To Start An Infinite loop
{
if (temp->data == num)
{
printf("\nThe Element is at position %d\n", nodeno);
break;
}
else if (temp->next == 0)
{
printf("\nNo element as Such \n");
break;
}
else
{
temp = temp->next;
nodeno++;
}
}
}
}
//todo function -- Display 12 (done)
void display()
{
temp = head;
if (flagforstart == 0)
{
printf("\nThe Linked List is Empty\n");
}
else
{
printf("\nThe Linked list is : \n");
while (temp != 0)
{
if (head == 0)
{
printf("%d\n", head->data);
}
else
{
printf("%d\n", temp->data);
}
temp = temp->next;
}
}
}
//?-------------------------------------------------------------------------
//todo main function
int main()
{
int option = 0;
printf("\n***--------Main Menu--------***");
printf("\nPress the following keys for following functions");
printf("\n1) Initial Creation\n2) Insert Begining\n3) Insert End\n4) Insert Before\n5) Insert After\n6) Delete Start\n7) Delete End\n8) Delete Before\n9) Delete After\n10) Delete Specified Node\n11) Search Node\n12) Display\n13) Exit\n");
do
{
printf("\nYou want to Perform Function No : ");
scanf("%d", &option);
switch (option)
{
case 1:
normalCreation();
break;
case 2:
insertBeginning();
break;
case 3:
insertEnd();
break;
case 4:
insertBefore();
break;
case 5:
insertAfter();
break;
case 6:
deleteStart();
break;
case 7:
deleteEnd();
break;
case 8:
deleteBefore();
break;
case 9:
deleteAfter();
break;
case 10:
deleteNode();
break;
case 11:
searchNode();
break;
case 12:
display();
break;
case 13:
//! writing so that it does not come in default when we select option 12
break;
default:
printf("\nInvalid Option key is pressed\n");
break;
}
} while (option != 13);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment