Single Linked List: - Simply a list is a sequence of data, and the linked list is a sequence of data linked with each other.
The formal definition of a single linked list is as follows.
The single linked list is a sequence of elements in which every element has link to its next element in the sequence.
In any single linked list, the individual element is called as "Node". Every "Node" contains two fields, data and next. The data field is used to store the actual value of that node and the next field is used to store the address of the next node in the sequence.
- #include <stdio.h>
- #include <stdlib.h>
- #include <malloc.h>
- struct node
- {
- int data;
- struct node *next;
- };
- struct node *start;
- void creat_node(int);
- void creat_nodee(int);
- void print();
- int main()
- {
- int data,i,n,neww;
- printf("Plese Enter the Length of the Linked List:\n");
- scanf("%d",&n);
- for(i=0;i<n;i++)
- {
- printf("Please Enter A Value:\n");
- scanf("%d",&data);
- creat_node(data);
- }
- print();
- printf("Please Enter A Value to be Insert at the End of the Linked LIST:\n");
- scanf("%d",&neww);
- creat_nodee(neww);
- print();
- }
- void creat_node(int x)
- {
- struct node *t,*t1;
- t=(struct node*)malloc(sizeof(struct node));
- t->data=x;
- t->next=NULL;
- if(start==NULL)
- {
- start=t;
- }
- else
- {
- t1=start;
- while(t1->next!=NULL)
- {
- t1=t1->next;
- }
- t1->next=t;
- }
- }
- void creat_nodee(int x)
- {
- struct node *t,*t1;
- t=(struct node*)malloc(sizeof(struct node));
- t->data=x;
- t->next=NULL;
- t1=start;
- while(t1->next!=NULL)
- {
- t1=t1->next;
- }
- t1->next=t;
- }
- void print()
- {
- struct node *t;
- t=start;
- while(t->next!=NULL)
- {
- printf("Your Entered DATA was: %d \n",t->data);
- t=t->next;
- }
- printf("Your Entered DATA was (END): %d \n",t->data);
- }