-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Linkedlist.c
98 lines (65 loc) · 2.03 KB
/
Linkedlist.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
int key;
struct node *next;
};
struct node *head = NULL;
struct node *current = NULL;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void insertFirst(int key, int data){ // Insert a node in the beginning
struct node *link = (struct node*) malloc(sizeof(struct node));
link->key = key;
link->data = data;
link->next = head;
head = link;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void insertLast(int key, int data){ // Insert a node to the end
current = head;
while(current->next != NULL){
current = current->next;
}
struct node *link = (struct node*) malloc(sizeof(struct node));
link->key = key;
link->data = data;
link->next = NULL;
current->next = link;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void printList(){ // Printing the linked list
struct node *temp = head;
printf("Printing the list [ ");
while(temp != NULL){
printf("(%d, %d)", temp->key, temp->data);
temp = temp->next;
}
printf(" ]");
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void deleteNode(int key){ //Delete a sertain Node with a key
struct node *temp = head;
while(temp->key != key){
current = temp;
temp = temp->next;
}
current->next = temp->next;
temp->next = NULL;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Main method
int main()
{
insertFirst(1,10);
insertFirst(2,20);
insertFirst(3,30);
insertFirst(4,40);
insertFirst(5,50);
insertLast(6,32);
printList();
deleteNode(3);
printf("\n");
printList();
return 0;
}