Created
September 2, 2020 15:30
-
-
Save AakashCode12/8546a65094b414209dcc55d35e692e00 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
//todo Strcuture Definition | |
struct node | |
{ | |
int data; | |
struct node *next; | |
}; | |
struct node *head, *newnode, *temp, *prev, *prev1, *temp2; | |
// 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)-tested | |
void normalCreation() | |
{ | |
int noOfElements; | |
printf("\nEnter the No of Elements you want to add in the Linked List : "); | |
scanf("%d", &noOfElements); | |
for (int i = 0; i < noOfElements; i++) | |
{ | |
newnode = (struct node *)malloc(sizeof(struct node)); | |
printf("\nEnter The Value you want to Add : "); | |
scanf("%d", &newnode->data); | |
newnode->next = NULL; | |
if (head == NULL) | |
{ | |
head = temp = newnode; | |
} | |
else | |
{ | |
temp->next = newnode; | |
temp = newnode; | |
} | |
} | |
} | |
//todo Function--Insert Beginning -2 (Done)-tested | |
void insertBeginning() | |
{ | |
newnode = (struct node *)malloc(sizeof(struct node)); | |
printf("\nEnter The Value you want to Add : "); | |
scanf("%d", &newnode->data); | |
newnode->next = NULL; | |
if (head == NULL) | |
{ | |
head = newnode; | |
} | |
else | |
{ | |
newnode->next = head; | |
head = newnode; | |
} | |
} | |
//todo Function--Insert End -3 (done)-tested | |
void insertEnd() | |
{ | |
newnode = (struct node *)malloc(sizeof(struct node)); | |
printf("\nEnter The Value you want to Add : "); | |
scanf("%d", &newnode->data); | |
newnode->next = NULL; | |
if (head == NULL) | |
{ | |
head = newnode; | |
} | |
else | |
{ | |
temp = head; | |
while (temp->next != NULL) | |
{ | |
temp = temp->next; | |
} | |
temp->next = newnode; | |
} | |
} | |
//todo Function--Insert before -4 (Done Ma'am Style)-tested | |
void insertBefore() | |
{ | |
if (head == NULL) | |
{ | |
printf("\nYou need Atleast one Element in Linked List to Use This Function\n"); | |
} | |
else | |
{ | |
int num; | |
printf("\nEnter the Data Before which you want to Insert the node : "); | |
scanf("%d", &num); | |
printf("\nEnter the value you want to add : "); | |
newnode = (struct node *)malloc(sizeof(struct node)); | |
scanf("%d", &newnode->data); | |
if (head->data == num) | |
{ | |
newnode->next = head; | |
head = newnode; | |
} | |
else | |
{ | |
temp = head; | |
while (temp->data != num) | |
{ | |
prev = temp; | |
temp = temp->next; | |
} | |
prev->next = newnode; | |
newnode->next = temp; | |
} | |
} | |
} | |
//todo Function--Insert After -5 (Done Ma'am Style)-tested (fixed a bug) | |
void insertAfter() | |
{ | |
if (head == NULL) | |
{ | |
printf("\nYou need Atleast one Element in Linked List to Use This Function\n"); | |
} | |
else | |
{ | |
int num; | |
printf("\nEnter the Data After which you want to Insert the node : "); | |
scanf("%d", &num); | |
printf("\nEnter the value you want to add : "); | |
newnode = (struct node *)malloc(sizeof(struct node)); | |
scanf("%d", &newnode->data); | |
if (head->data == num) | |
{ | |
head->next = newnode; | |
} | |
else | |
{ | |
temp = head; | |
while (temp->data != num) | |
{ | |
temp = temp->next; | |
} | |
newnode->next = temp->next; | |
temp->next = newnode; | |
} | |
} | |
} | |
//todo Function--Delete Start -6 (Done)-tested | |
void deleteStart() | |
{ | |
if (head == NULL) | |
{ | |
printf("\nYou need Atleast one Element in Linked List to Use This Function\n"); | |
} | |
else | |
{ | |
temp = head; | |
head = temp->next; | |
printf("\nThe Element removed is : %d\n", temp->data); | |
free(temp); | |
} | |
} | |
//todo Function--Delete End -7 (Done Ma'am Style)-tested | |
void deleteEnd() | |
{ | |
if (head == NULL) | |
{ | |
printf("\nYou need Atleast one Element in Linked List to Use This Function\n"); | |
} | |
else | |
{ | |
temp = head; | |
if (head->next == NULL) | |
{ | |
head = NULL; | |
printf("\nThe Element removed is : %d\n", temp->data); | |
free(temp); | |
} | |
else | |
{ | |
while (temp->next != NULL) | |
{ | |
prev = temp; | |
temp = temp->next; | |
} | |
prev->next = NULL; | |
printf("\nThe Element removed is : %d\n", temp->data); | |
free(temp); | |
} | |
} | |
} | |
//todo Function--Delete Before-8 (Done By Ma'am Style)--tested | |
void deleteBefore() | |
{ | |
if (head == NULL || head->next == NULL) | |
{ | |
printf("\nYou need Atleast Two Elements in Linked List to Use This Function\n"); | |
} | |
else | |
{ | |
int num; | |
printf("\nEnter the Data Whose Before node you want to Delete : "); | |
scanf("%d", &num); | |
temp = head; | |
temp2 = temp->next; | |
if (temp2->data == num) | |
{ | |
deleteStart(); | |
} | |
else | |
{ | |
temp=head; | |
prev=temp; | |
while (temp->data != num) | |
{ | |
prev1 = prev; | |
prev = temp; | |
temp = temp->next; | |
} | |
prev1->next = temp; | |
printf("\nThe Element removed is : %d\n", prev->data); | |
free(prev); | |
} | |
} | |
} | |
//todo Function--Delete After Node-9 (done ma'am style)--tested | |
void deleteAfter() | |
{ | |
if (head == NULL || head->next == NULL) | |
{ | |
printf("\nYou need Atleast Two Elements in Linked List to Use This Function\n"); | |
} | |
else | |
{ | |
int num; | |
printf("\nEnter the Data Whose next node you want to Delete : "); | |
scanf("%d", &num); | |
temp = head; | |
while (temp->data != num) | |
{ | |
temp = temp->next; | |
} | |
prev = temp; | |
temp = temp->next; | |
if (temp->next == NULL) | |
{ | |
prev->next = NULL; | |
} | |
prev->next = temp->next; | |
printf("\nThe Element removed is : %d\n", temp->data); | |
free(temp); | |
} | |
} | |
//todo Function--Delete Specified Node-10 (Done ma'am ke style me)--tested | |
void deleteNode() | |
{ | |
if (head == NULL) | |
{ | |
printf("\nYou need Atleast one Element in Linked List to Use This Function\n"); | |
} | |
else | |
{ | |
int num; | |
printf("\nEnter the Data which you want to Delete from the Node : "); | |
scanf("%d", &num); | |
temp = head; | |
if (head->data == num && head->next == NULL) | |
{ | |
head = NULL; | |
printf("\nThe Element removed is : %d\n", temp->data); | |
free(temp); | |
} | |
else if (head->data == num && head->next != NULL) | |
{ | |
head = head->next; | |
printf("\nThe Element removed is : %d\n", temp->data); | |
free(temp); | |
} | |
else | |
{ | |
while (temp->data != num) | |
{ | |
prev = temp; | |
temp = temp->next; | |
} | |
prev->next = temp->next; | |
printf("\nThe Element removed is : %d\n", temp->data); | |
free(temp); | |
} | |
} | |
} | |
//todo Function--Search A Node Function -11 (Done ma'am ke Style me)--tested | |
void searchNode() | |
{ | |
if (head == NULL) | |
{ | |
printf("\nThe Linked List is Empty\n"); | |
} | |
else | |
{ | |
int num, counter = 0; | |
printf("\nEnter the Data you want to search : "); | |
scanf("%d", &num); | |
temp = head; | |
while (temp->data != num) | |
{ | |
counter++; | |
temp = temp->next; | |
} | |
printf("\nThe position of the node is : %d\n", counter); | |
} | |
} | |
//todo Function--Display Function -12 (Done)--tested | |
void display() | |
{ | |
if (head == NULL) | |
{ | |
printf("\nThe Linked List is Empty\n"); | |
} | |
else | |
{ | |
temp = head; | |
printf("\nThe Linked List is : "); | |
while (temp != NULL) | |
{ | |
if (temp->next == NULL) | |
{ | |
printf("%d\n", temp->data); | |
} | |
else | |
{ | |
printf("%d -> ", temp->data); | |
} | |
temp = temp->next; | |
} | |
} | |
} | |
//todo main function--working--tested | |
int main() | |
{ | |
head = NULL; //!NULL given to head pointer | |
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