Last active
August 7, 2019 12:19
-
-
Save Rajssss/78ecc29e10a768071dd7ae031307cd92 to your computer and use it in GitHub Desktop.
Go through the code first and read the comments....
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> | |
| struct Node | |
| { | |
| int A; | |
| struct Node *next; | |
| } *first; | |
| typedef struct Node Node; | |
| void Create(); | |
| void Display(); | |
| int Insert(); | |
| int Delete(); | |
| // | |
| // NOTE-- setbuf() is for debugging purpose only! | |
| // | |
| int main(void) | |
| { | |
| int option, index, x; | |
| setbuf(stdout, 0); | |
| printf("Enter value=>"); | |
| first = (Node*)malloc(sizeof(Node)); | |
| scanf("%d", &first->A); | |
| first->next = 0; | |
| setbuf(stdout, 0); | |
| printf("Do you wanna add more?\n\t1. Yes\n\t2. No\n\t=>"); | |
| scanf("%d", &option); | |
| while(option == 1) | |
| { | |
| Create(first); | |
| setbuf(stdout, 0); | |
| printf("Do you wanna add more?\n\t1. Yes\n\t2. No\n\t=>"); | |
| scanf("%d", &option); | |
| } | |
| setbuf(stdout, 0); | |
| printf("Linked List is=> "); | |
| Display(first); | |
| /*printf("\nEnter element no to delete =>"); //Uncomment/Comment to Delete/Insert | |
| scanf("%d", &index); | |
| printf("\nDeleted value is =>%d\nLinked List is=> ",Delete(first, index)); | |
| Display(first);*/ | |
| setbuf(stdout, 0); | |
| printf("\nEnter value and position to insert resp. =>"); //Uncomment/Comment to Delete/Insert | |
| scanf("%d %d", &x, &index); | |
| Insert(first, x, index); | |
| setbuf(stdout, 0); | |
| printf("\nLinked List is=> "); | |
| Display(first); | |
| return 0; | |
| } | |
| void Create(Node* first) | |
| { | |
| Node *temp = first, *newNode; | |
| newNode = (Node*)malloc(sizeof(Node)); | |
| setbuf(stdout, 0); | |
| printf("Enter value =>"); | |
| scanf("%d", &newNode->A); | |
| newNode->next = 0; | |
| while(temp->next != 0) | |
| temp = temp->next; | |
| temp->next = newNode; | |
| } | |
| Display(Node *first) | |
| { | |
| if(first) | |
| { | |
| setbuf(stdout, 0); | |
| printf("%d ", first->A); | |
| Display(first->next); | |
| } | |
| } | |
| Delete(Node *first, int index) | |
| { | |
| Node *temp = first; | |
| int x; | |
| if(index==1) | |
| { | |
| x = temp->A; | |
| temp = temp->next; | |
| free(temp); | |
| return x; | |
| } | |
| else | |
| { | |
| for(int i=0; i<index-1; i++) | |
| { | |
| temp = first; | |
| first = first->next; | |
| } | |
| temp->next = first->next; | |
| x = first->A; | |
| free(first); | |
| return x; | |
| } | |
| } | |
| Insert(Node *head,int x, int n) | |
| { | |
| Node *temp = 0; | |
| temp = (Node*)malloc(sizeof(Node)); | |
| temp->A = x; | |
| if(n == 1) | |
| { | |
| temp->next = head; | |
| head = temp; | |
| } | |
| else | |
| { | |
| for(int i=0; i<n-2; i++) | |
| head = head->next; | |
| temp->next = head->next; | |
| head->next = temp; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment