-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSingly_Linked_List -Program_1.c
72 lines (65 loc) · 1.69 KB
/
Singly_Linked_List -Program_1.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
63
64
65
66
67
68
69
70
71
72
// 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;
node *position;
void insert(node *list,int e)
{
node *newnode=malloc(sizeof(node));
newnode->element=e;
if(list->next==NULL)
{
list->next=newnode;
newnode->next=NULL;
position=newnode;
}
else
{
newnode->next=NULL;
position->next=newnode;
position=newnode;
}
}
void display(node *list)
{
if(list->next!=NULL)
{
node *position=list->next;
while(position!=NULL)
{
printf("%d\n",position->element);
position=position->next;
}
}
}
int main()
{
int n,m;
node *list=malloc(sizeof(node));
list->next=NULL;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d",&m);
insert(list,m);
}
display(list);
return 0;
}