-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinked_list1.c
More file actions
64 lines (57 loc) · 1.54 KB
/
linked_list1.c
File metadata and controls
64 lines (57 loc) · 1.54 KB
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
63
64
// A Teacher gave randomly some marbles to students starting from first bench till the last bench but didn't note the count of marbles given to a student. The class representative was told to note down the marbles with students starting from first bench till the last bench. Given a linked list, print each node's data element, one per line. If the head pointer is null (indicating the list is empty), there is nothing to print.
// Input Format
// The first line of input contains n, the number of elements in the linked list.
// The next n lines contain one element each, the data values for each node.
// Sample Input
// 2
// 16
// 13
// Sample Output
// 16
// 13
// Explanation
// There are two elements in the linked list. They are represented as 16 -> 13 -> NULL. So print 16 and 13 each on a new line.
#include <stdio.h>
#include <stdlib.h>
struct node
{
int element;
struct node *next;
};
typedef struct node node;
void insert(node *list, int x)
{
node *newnode = malloc(sizeof(node));
newnode->element = x;
newnode->next = NULL;
node *pos = list;
while (pos->next != NULL)
{
pos = pos->next;
}
pos->next = newnode;
}
void traverse(node *list)
{
node *pos = list;
while (pos->next != NULL)
{
pos = pos->next;
printf("%d\n", pos->element);
}
}
int main()
{
int n;
scanf("%d", &n);
node *list = malloc(sizeof(node));
list->next = NULL;
while (n--)
{
int x;
scanf("%d", &x);
insert(list, x);
};
traverse(list);
return 0;
}