forked from Vencenter/C_Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
线性表_双向链表_.cpp
113 lines (85 loc) · 1.53 KB
/
线性表_双向链表_.cpp
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
#include<stdio.h>
#include<stdlib.h>
typedef struct datanum
{
int num;
struct datanum *right;
struct datanum *left;
}dat,*dap;
struct datanum* create()
{
int i,j,n;
dat *head,*s,*p;
printf("请输入次数:\n");
scanf("%d",&n);
head=(datanum*)malloc(sizeof(datanum));
p=head;
head->right=NULL;
head->left=NULL;
for(i=n;i>0;i--)
{
printf("请输入第%d个数字:\n",n-i+1);
s=(datanum*)malloc(sizeof(datanum));
scanf("%d",&s->num);
p->right=s;
s->left=p;
p=s;
}
s->right=NULL;
printf("链表创建完成!\n");
return head;
}
int print1(struct datanum *p)
{
printf("链表信息显示:\n");
while(p->right!=NULL)
{
printf("%d<--->",p->right->num);
p=p->right;
}
printf("over!\n");
}
struct datanum* w_insert(struct datanum *p)
{
int n;
dat *t,*head;
printf("请输入插入的数字:\n");
t=(datanum*)malloc(sizeof(datanum));
scanf("%d",&t->num);
head=p;
while(p->right!=NULL)
{
//printf("%d<--->\n",p->right->num);
p=p->right;
}
p->right=t;
t->right=NULL;
t->left=p;
return head;
}
struct datanum* t_insert(struct datanum *p)
{
int n;
dat *t,*head;
printf("请输入插入的数字:\n");
t=(datanum*)malloc(sizeof(datanum));
scanf("%d",&t->num);
head=p;
t->right=p->right;
p->right->left=t;
p->right=t;
t->left=p;
//t->left=p;
return head;
}
int main()
{
dat *p,*q;
p=create();
print1(p);
q=w_insert(p);
print1(q);
q=t_insert(p);
print1(q);
return 0;
}