-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDisplayLinkedList.c
59 lines (54 loc) · 1.02 KB
/
DisplayLinkedList.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
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node* next;
} node;
node* createlinkedlist(int n);
void displaylist(node* head);
int main()
{
int n = 0;
node* HEAD = NULL;
printf("\n How many nodes: ");
scanf("%d", &n);
HEAD = createlinkedlist(n);
displaylist(HEAD);
return 0;
}
node* createlinkedlist(int n)
{
int i = 0;
node* head = NULL;
node* temp = NULL;
node* p = NULL;
for (i = 0; i < n; i++)
{
temp = (node*)malloc(sizeof(node));
printf("Enter data for node number %d: ", i + 1);
scanf("%d", &(temp->data));
temp->next = NULL;
if (head == NULL)
{
head = temp;
}
else
{
p = head;
while (p->next != NULL)
p = p->next;
p->next = temp;
}
}
return head;
}
void displaylist(node* head)
{
node* p = head;
while (p != NULL)
{
printf("\t %d->", p->data);
p = p->next;
}
}