-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLinked_list_int_tarverse.c
114 lines (103 loc) · 2.21 KB
/
Linked_list_int_tarverse.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <stdio.h>
#include <stdlib.h>
struct c_node{
struct c_node *prev;
int id;
int val;
struct c_node *next;
};
struct s_node{
int id;
int val;
struct s_node *next;
};
struct c_node *head = NULL;
struct c_node *current = NULL;
struct s_node *shead = NULL;
struct s_node *sheadstart = NULL;
void double_list_init(void)
{
struct c_node *cache;
for(int i=0;i<5;i++)
{
cache = (struct c_node *)malloc(sizeof(struct c_node));
cache->id =i;
cache->val =i;
cache->next = NULL;
cache->prev = NULL;
if(current==NULL)
{
head = cache;
current = cache;
}
else
{
cache->prev = current;
current->next = cache;
current =cache;
}
}
}
void single_list_rev_init(void)
{
for(int i=0; i<5; i++)
{
struct s_node *cache;
cache = (struct s_node*)malloc(sizeof(struct s_node));
cache->id =i;
cache->val =i;
cache->next= shead;
shead =cache;
}
}
void single_list_forward_init(void)
{
for(int i=0; i<5; i++)
{
struct s_node *cache;
cache = (struct s_node*)malloc(sizeof(struct s_node));
cache->id =i;
cache->val =i;
cache->next=NULL;
if(shead==NULL)
{
shead =cache;
sheadstart =cache;
}
else
{
shead->next= cache;
shead=cache;
}
}
}
void main(void)
{
single_list_forward_init();
printf("Single Forward\n");
while(sheadstart != NULL)
{
printf("%d\t%d\n",sheadstart->id, sheadstart->val);
sheadstart = sheadstart->next;
}
single_list_rev_init();
printf("Single Reverse\n");
while(shead != NULL)
{
printf("%d\t%d\n",shead->id, shead->val);
shead = shead->next;
}
double_list_init();
printf("Double Forward\n");
while(head != NULL)
{
printf("%d\t%d\n",head->id, head->val);
head = head->next;
}
printf("Double Reverse\n");
while(current != NULL)
{
printf("%d\t%d\n",current->id, current->val);
current = current->prev;
}
}