-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkedlist.c
62 lines (51 loc) · 1002 Bytes
/
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct _Node {
char data[20];
struct _Node* link;
} Node;
void printList(Node *list) {
Node* p=list;
while(p!=NULL) {
printf("[%s]", p->data);
if (p->link!=NULL)
printf("->");
else
printf("\n");
p = p->link;
}
}
Node *newNode(char data[]) {
Node *p = (Node*)malloc(sizeof(Node));
strcpy(p->data, data);
p->link = NULL;
return p;
}
Node *addFirstNode(Node *list, Node *p) {
p->link = list;
return p;
}
Node *addLastNode(Node *list, Node *p) {
Node* q=list;
if (q==NULL) {
return p;
}
while (q->link!=NULL)
q=q->link;
q->link=p;
return list;
}
void main() {
Node* list = NULL;
list = addFirstNode(list, newNode("Wed"));
printList(list);
list = addFirstNode(list, newNode("Tue"));
printList(list);
list = addFirstNode(list, newNode("Mon"));
printList(list);
list = addLastNode(list, newNode("Thu"));
printList(list);
list = addLastNode(list, newNode("Fri"));
printList(list);
}