Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- struct Node
- {
- int Value;
- struct Node *Next;
- };
- struct Node *Head, *LL, *Temp, *Temp2;
- void Create_A_Node(int V)
- {
- Temp = (struct Node *)malloc(sizeof(struct Node));
- if(!Temp)
- {
- printf("Something Wrong with Node Creation\n");
- exit(0);
- }
- else
- {
- Temp->Value = V;
- Temp->Next = NULL;
- }
- }
- void Free_A_Node(struct Node *A_Node)
- {
- free(A_Node);
- }
- void Menu()
- {
- printf("\n 1. Create a Linked List");
- printf("\n 2. Insert a value into a Linked List (at the end)");
- printf("\n 3. Insert a value into a Linked List (after a specific value)");
- printf("\n 4. Insert a value into a Linked List (before a specific value)");
- printf("\n 5. Update a value with another value");
- printf("\n 6. Delete a value from the Linked List");
- printf("\n 7. Delete a value which is before a specific value");
- printf("\n 8. Delete a value which is after a specific value");
- printf("\n 9. Print the Linked List");
- printf("\n10. Print the Linked List in Reverse Order (For Bonus Marks)");
- printf("\nEnter your Choice : (1-10, 0 to exit):");
- }
- void Option1()
- {
- int V;
- printf("You have selection Option1\n");
- if(Head==NULL) // That is, the Linked list is yet to create
- {
- printf("Enter an integer to create the Linked List ; ");
- scanf("%d",&V);
- Create_A_Node(V); // Calling this function with V and the new Node will be created as Temp;
- Head = Temp; // Now the Head will Point to the Temp;
- printf("The Linked List is Created\n");
- }
- else
- {
- printf("The Linked List is already created\n");
- }
- }
- void Option2()
- {
- int V;
- printf("You have selection Option2 (Insert a value into a Linked List (at the end))\n");
- LL = Head;
- printf("Enter an integer to insert into the Linked List ; ");
- scanf("%d",&V);
- if(LL==NULL)
- {
- printf("There is No Linked List. So, the Linked list will be created First\n");
- Create_A_Node(V); // Calling this function with V and the new Node will be created as Temp;
- Head = Temp; // Now the Head will Point to the Temp;
- }
- else
- {
- while(LL->Next) // LL will traverse till the end of the Linked List or the Node that has Null in it's next
- {
- LL = LL->Next;
- }
- Create_A_Node(V); // Calling this function with V and the new Node will be created as Temp;
- LL->Next = Temp; // That newly created node will be added to the end of the linked list (as the LL is now at the last node after traversing Linked List
- printf("Data Inserted at the end of the Linked List\n");
- }
- }
- void Option3()
- {
- printf("You have selection Option3\n");
- printf("(Insert a value into a Linked List (after a specific value))");
- printf("Where do you want to insert the integer. After : ");
- int u, v;
- scanf("%d", &u);
- printf("Enter the integer: ");
- scanf("%d", &v);
- LL = Head;
- if(LL==NULL)
- {
- printf("There is No Linked List. So, the Linked list will be created First\n");
- Create_A_Node(v);
- Head = Temp;
- }
- else {
- while(LL->Value != u){
- LL = LL->Next;
- if (LL->Next == NULL) break;
- }
- if (LL->Value == u) {
- Temp2 = LL->Next;
- Create_A_Node(v);
- LL->Next = Temp;
- Temp->Next = Temp2;
- printf("\n\n%d %d %d\n\n", LL->Value, Temp->Value , Temp2->Value);
- printf("Data Inserted successfully!\n");
- }
- else {
- printf("Not found.\n");
- }
- }
- }
- void Option4()
- {
- printf("You have selection Option4\n");
- printf("\n 4. Insert a value into a Linked List (before a specific value)");
- printf("Where do you want to insert the integer. After : ");
- int u, v;
- scanf("%d", &u);
- printf("Enter the integer: ");
- scanf("%d", &v);
- LL = Head;
- if(LL==NULL)
- {
- printf("There is No Linked List. So, the Linked list will be created First\n");
- Create_A_Node(v); // Calling this function with V and the new Node will be created as Temp;
- Head = Temp; // Now the Head will Point to the Temp;
- }
- else {
- if (LL->Value == u) {
- Create_A_Node(v);
- Head = Temp;
- Temp->Next = LL;
- }
- else {
- while(LL->Next->Value != u){
- LL = LL->Next;
- if (LL->Next->Next == NULL) break;
- }
- if (LL->Next->Value == u) {
- Create_A_Node(v);
- Temp->Next = LL->Next;
- LL->Next = Temp;
- printf("Added successfully!\n");
- }
- else printf("Not found!\n");
- }
- }
- }
- void Option5()
- {
- printf("You have selection Option5\n");
- printf("\n 5. Update a value with another value");
- printf("Which value do you want to update?");
- int u,v;
- scanf("%d", &u);
- printf("Enter the integer: ");
- scanf("%d", &v);
- LL = Head;
- if(LL==NULL)
- {
- printf("There is No Linked List. So, the Linked list will be created First\n");
- Create_A_Node(v); // Calling this function with V and the new Node will be created as Temp;
- Head = Temp; // Now the Head will Point to the Temp;
- }
- else {
- while (LL->Value != u) {
- LL = LL->Next;
- if (LL->Next == NULL) break;
- }
- if (LL->Value == u) LL->Value = v;
- else printf("Not found!");
- }
- }
- void Option6()
- {
- printf("You have selection Option6\n");
- printf("\n 6. Delete a value from the Linked List");
- printf("Which value do you want to delete?");
- int u;
- int found = 0;
- scanf("%d", &u);
- LL = Head;
- if(LL==NULL)
- {
- printf("Sorry, there is No Linked List.\n");
- }
- else {
- if (LL->Value == u) {
- Head = LL->Next;
- free(LL);
- printf("Deleted!\n");
- }
- else {
- while (LL->Next->Value != u) {
- LL = LL->Next;
- if (LL->Next->Next == NULL) break;
- }
- if (LL->Next->Value == u) found = 1;
- if (found) {
- Temp = LL->Next;
- LL->Next = LL->Next->Next;
- free(Temp);
- printf("Deleted!\n");
- }
- else printf("Not found!\n");
- }
- }
- }
- void Option7()
- {
- printf("You have selection Option7\n");
- printf("\n 7. Delete a value which is before a specific value");
- printf("Enter the value: ");
- int u;
- scanf("%d", &u);
- LL = Head;
- if (LL == NULL) printf("Sorry, there is no linked list.\n");
- else if (LL->Value == u) printf("This is the first element!\n");
- else if (LL->Next->Value == u) {
- Head = LL->Next;
- free(LL);
- printf("Deleted!\n");
- }
- else {
- int found = 0;
- while (LL->Next->Next->Value != u) {
- LL = LL->Next;
- if (LL->Next->Next->Next == NULL) break;
- }
- if (LL->Next->Next->Value == u) found = 1;
- if (found) {
- Temp = LL->Next;
- LL->Next = LL->Next->Next;
- free(Temp);
- printf("Deleted!\n");
- }
- else printf("Not found!\n");
- }
- }
- void Option8()
- {
- printf("You have selection Option8\n");
- printf("\n 8. Delete a value which is after a specific value");
- printf("Enter the integer: ");
- int u;
- scanf("%d", &u);
- LL = Head;
- if (!LL) printf("Sorry, there is no linked list!\n");
- else {
- while (LL->Value!=u) {
- LL = LL->Next;
- if (LL->Next == NULL) break;
- }
- if (!LL->Next && LL->Value == u) printf("This is the last element!\n");
- else if (LL->Value != u) printf("The element was not found!\n");
- else {
- Temp2 = LL->Next;
- LL->Next = LL->Next->Next;
- free(Temp2);
- printf("Deleted!\n");
- }
- }
- }
- void Option9()
- {
- printf("\nYou have selection Option9\n");
- /*
- printf("\n 9. Print the Linked List");
- */
- LL = Head;
- while(LL!=NULL) // Or you can write while(LL)
- {
- printf("%d\n",LL->Value);
- LL= LL->Next;
- }
- }
- void printReverese (struct Node *head) {
- if (head == NULL) return;
- printReverese(head->Next);
- printf("%d\n", head->Value);
- return;
- }
- void Option10()
- {
- printf("You have selection Option10\n");
- printf("\n10. Print the Linked List in Reverse Order (For Bonus Marks)\n");
- printReverese(Head);
- }
- void SelectOptions(int Ch)
- {
- if(Ch==1) Option1();
- else if(Ch==2) Option2();
- else if(Ch==3) Option3();
- else if(Ch==4) Option4();
- else if(Ch==5) Option5();
- else if(Ch==6) Option6();
- else if(Ch==7) Option7();
- else if(Ch==8) Option8();
- else if(Ch==9) Option9();
- else if(Ch==10) Option10();
- }
- void main()
- {
- freopen("in.txt", "r", stdin);
- int Choice;
- while(1)
- {
- Menu();
- scanf("%d", &Choice);
- if(Choice==0)
- break;
- else if(Choice<0 || Choice>10)
- printf("Wrong input\n");
- else
- SelectOptions(Choice);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment