Last active
August 11, 2020 12:03
-
-
Save AakashCode12/03ce80e4a4fd756c4409c66e2141716d to your computer and use it in GitHub Desktop.
### Till now completed - creation,display,insert at begining and end
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 structure | |
struct node{ | |
int data; | |
struct node* next; | |
}; | |
struct node *head, *newnode, *temp; | |
//todo main function | |
int main(){ | |
//? creation of linked list (start) | |
int option=1; | |
head=0; | |
while (option!=0) { | |
newnode=(struct node*)malloc(sizeof(struct node)); | |
printf("Enter the data u want to enter: "); | |
scanf("%d",&newnode->data); | |
newnode->next=0; | |
if(head==0){ | |
head=temp=newnode; | |
} | |
else{ | |
temp->next=newnode; | |
temp=newnode; | |
} | |
printf("\nTo end the process of adding elements in linked list press 0 or press any key\n"); | |
scanf("%d",&option); | |
} | |
//? creation of linked list (end) | |
//? the node insertion at begining (start) | |
newnode=(struct node*)malloc(sizeof(struct node)); | |
printf("Enter the data you want to insert at beginning: "); | |
scanf("%d", &newnode->data); | |
//! till here same because we made a new node till now | |
//! the game of pointers starts now | |
newnode->next=head; | |
head=newnode; | |
//? the node insertion at begining (end) | |
//? the node insertion at end of linked list (start) | |
newnode=(struct node*)malloc(sizeof(struct node)); | |
printf("Enter the data you want to insert at End of Linked List: "); | |
scanf("%d", &newnode->data); | |
// *till here same because we made a new node till now | |
newnode->next=0; //** to store null in the last node | |
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 ) | |
while(temp->next!=0){ | |
temp=temp->next; | |
} | |
//*here head jo tha pehle wo ab temp kam karraha hai so | |
//newnode->next=temp; // *but there is no need for this because we already gave the value of 0 to the last node | |
temp->next=newnode; | |
//? the node insertion at end of linked list (end) | |
//?insert an element after an element (Start) | |
//?insert an element after an element (End) | |
//? display the contents of linked list (start) | |
temp=head; | |
while(temp!=0){ | |
if(head==0){ | |
printf("%d ",head->data); | |
}else{ | |
printf("%d ",temp->data); | |
} | |
temp=temp->next; | |
} | |
//? display the contents of linked list (end) | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment