鏈表,折磨人的數據結構
要有一週的時間沒更博了,這段時間我暫停了計蒜課的刷題,是因爲我把《征服c指針》看完了。當然這本肯定不會只讀一遍,我感覺按我目前的知識水平,應該之吸收了20%不到。所以我準備在學一點數據結構再去看一遍。
學校開設的數據結構課開始瘋狂的折磨人了。我每次翻開學校的教材,看着上面亂糟糟的排版,真的有一種想撕碎它的衝動。今天特別不在狀態,在上完上機課後我就感覺特別累,莫名其妙的累。當然可能是上午上了兩節高數後又上了三節數據結構的原因。今天上機給的實驗題目就是鏈表的建立插入與刪除,今天寫的代碼是我寫過的鏈表代碼最成功的一次,一次性編譯通過,沒有任何bug,這個feel太爽!下面給出代碼:
#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語言,導致我對出現的讓我暫時不能明白的知識有點壓力。以後要調整這種心態,不能太急功近利。