-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprint_linked_list.c
54 lines (44 loc) · 979 Bytes
/
print_linked_list.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
#include "stdio.h"
#include "stdlib.h"
struct node{
int data;
struct node *next;
};
struct node*head=NULL,*tail=NULL;
void printList(){
struct node *p=head;
if(p==NULL){
printf("There are no elements in the linked list.");
}
printf("Elements in the linked list are : ");
while (p!=NULL){
printf("%d ",p->data);
p=p->next;
}
printf("\n");
}
void addNode(int data){
struct node *new_node=(struct node*) malloc(sizeof(struct node));
new_node->data=data;
new_node->next=NULL;
if(head==NULL){
head=new_node;
tail=new_node;
}
else{
tail->next=new_node;
tail=new_node;
}
}
int main(){
int n,temp_num;
printf("Enter the number of elements : ");
scanf("%d",&n);
printf("Enter the elements : ");
for(int i=0;i<n;i++){
scanf("%d",&temp_num);
addNode(temp_num);
}
printList();
return 0;
}