Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Write a program in C to insert new node at the beginning of a Singly Linked List.
- #include<stdio.h>
- #include<stdlib.h>
- typedef struct Node
- {
- int x;
- struct Node *ptr;
- }node;
- node *head = NULL;
- node *temp = NULL;
- int main()
- {
- int i,n;
- printf("Input the number of nodes:",n);
- scanf("%d",&n);
- for(i=1; i<=n; i++)
- {
- node *q = (node*)malloc(sizeof (node));
- printf("\nInput data for node %d : ",i);
- scanf("%d", &q -> x);
- q -> ptr = NULL;
- if(head == NULL)
- {
- head = q;
- }
- else
- {
- q -> ptr = head;
- head = q;
- }
- }
- node *temp = head;
- printf("\nData entered in the list : \n");
- while(temp!= NULL)
- {
- printf("\nData = %d\n", temp->x);
- temp = temp->ptr;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement