-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLinkedlist.c
More file actions
58 lines (49 loc) · 1.15 KB
/
Linkedlist.c
File metadata and controls
58 lines (49 loc) · 1.15 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
#include<stdio.h>
#include<stdlib.h>
//Global head and tail pointers
struct Node* head=NULL;
struct Node* tail = NULL;
//Linked list node representation
struct Node
{
int data;
struct Node* next;
};
//inserts nodes in the linkedlist
struct Node* addNode(struct Node* head,int val)
{
//Dynamic memory creation
struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
temp->data = val;
temp->next = NULL;
//if the list is NULL points head and tail points to the same node
if(head == NULL)
{
head = tail = temp;
}
//else add the values to the tail node and adjust
else
{
tail->next = temp;
tail = temp;
}
return head;
}
void print(struct Node* temp)
{
//Traverse the linked list nodes and print the node values
while(temp!=NULL)
{
printf("%d\t",temp->data);
temp = temp->next;
}
}
int main()
{
//Inserting element into the linkedlist
head = addNode(head,1);
head = addNode(head,2);
head = addNode(head,3);
//print the linkedlist
print(head);
}