链表,折磨人的数据结构

链表,折磨人的数据结构

要有一周的时间没更博了,这段时间我暂停了计蒜课的刷题,是因为我把《征服c指针》看完了。当然这本肯定不会只读一遍,我感觉按我目前的知识水平,应该之吸收了20%不到。所以我准备在学一点数据结构再去看一遍。

    学校开设的数据结构课开始疯狂的折磨人了。我每次翻开学校的教材,看着上面乱糟糟的排版,真的有一种想撕碎它的冲动。今天特别不在状态,在上完上机课后我就感觉特别累,莫名其妙的累。当然可能是上午上了两节高数后又上了三节数据结构的原因。今天上机给的实验题目就是链表的建立插入与删除,今天写的代码是我写过的链表代码最成功的一次,一次性编译通过,没有任何bug![微笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif),这个feel太爽!下面给出代码:
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
115
116
117
118
119
120
#include<stdio.h>
#include<stdlib.h>

typedef struct Node
{
int data;
struct Node *next;
}Node;

void creatnode(Node *head)
{
Node *temp,*p;
p=head;
int n;
scanf("%d",&n);
while(n!=-1)
{
temp=malloc(sizeof(Node));
if(temp==NULL)
{
printf("No enough memory");
exit(0);
}
temp->data=n;
temp->next=NULL;
p->next=temp;
p=temp;
scanf("%d",&n);
}
}
void Print(Node *head)
{
Node *p;
p=head->next;
while(p!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
printf("\n");
}
void Insertnode(Node *head)
{
printf("Now insert the data:");
int e;
scanf("%d",&e);
Node *p,*pre,*temp;

temp=malloc(sizeof(Node));
temp->data=e;
temp->next=NULL;

pre=head;
p=pre->next;//1 2 3 5
while(p!=NULL)
{
if(e<=p->data)
{
pre->next=temp;
temp->next=p;
break;
}
pre=p;
p=p->next;
}
if(p==NULL)
{
pre->next=temp;
}

}
void Deletenode(Node *head)
{
Node *p,*pre;
pre=head;
p=pre->next;
printf("Now delete the data:");
int n;
scanf("%d",&n);
while(p!=NULL)
{
if(p->data==n)
{
pre->next=p->next;
free(p);
break;
}
pre=p;
p=p->next;
}
if(p==NULL)
printf("No found\n");
}
void FREE(Node *head)
{
Node *pre=head;
while(head!=NULL)
{
pre=head;
head=head->next;
free(pre);
}
}

int main()
{
Node *head;
head=malloc(sizeof(Node));
creatnode(head);
printf("Before insert:");
Print(head);
Insertnode(head);
printf("After insert:");
Print(head);
Deletenode(head);
printf("After delete:");
Print(head);
FREE(head);
return 0;
}
 太累了,也许是因为写了太多遍就没有写注释。这是最简单的单向链表。今天数据结构课还介绍了,对就仅仅只是介绍了双向链表,循环链表,静态链表。看着这些伪代码,我真的没有把他们翻译成c语言的冲动,哎!

 我也不知道为什么今天这么累,有可能是数据结构课上接触到的目前对我来说对我来说高难度数据结构让我感觉亚历山大,因为我一直自学在前面,一直以来上课教的东西对我来说是很轻松的,但数据结构教的东西让我很有压力,感觉心里失衡吧。

总是说今天很累很累,但是今天也有高兴的事,我以绩点第一拿到了班级奖学金,和综测第六拿到了优秀学生奖学金二等奖。话说中学一直在名校读,从来都还没有拿到过奖学金呢。

我感觉我还是太心急了,太迫切的想完全掌握c语言,导致我对出现的让我暂时不能明白的知识有点压力。以后要调整这种心态,不能太急功近利。