forked from daction/Basic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LL_insert_at_beginning.c
62 lines (45 loc) · 1.3 KB
/
LL_insert_at_beginning.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//Linked List: Inserting a node at beginning
#include <stdio.h>
#include <stdlib.h>
struct Node{
int data;
struct Node *next; // Address of next node
// In C++, node *next;
};
struct Node *head;// Global variable
void Insert(int x)
{
//struct Node* temp = (Node*) malloc(sizeof(struct Node));
struct Node *temp = (struct Node*)malloc(sizeof(struct Node));
//In C, every time is struct Node
temp->data = x; //(*temp).data = x;
// temp->next = NULL; // Dereferencing and then modify
// if(head != NULL) temp->next = head; // Insert a node at beginning of the list
temp->next = head; //covering when head is empty
head = temp; // Head point to temp, and value in head now will be address 100, check Note for graph
}
void Print()
{
struct Node *temp = head;// In C need: struct Node *
printf("List is: ");
//Loop for Traversing
while(temp != NULL)
{
printf(" %d", temp->data);
temp = temp->next; // Traverse the list
}
printf("\n");
}
int main()
{
head = NULL; // Empty list;
printf("How many numbers?\n");
int n,i,x;
scanf("%d", &n);
for(i = 0; i<n; i++){
printf("Enter the number \n");
scanf("%d", &x);
Insert(x); //
Print();
}
}