1101 填空題 鍊錶的排序 SCAU

2021-09-30 00:17:22 字數 2427 閱讀 4222

題目:先建立乙個鍊錶(鍊錶中各結點未按學號由小到大排序),然後呼叫sort函式,將鍊錶中各結點按學號由小到大排序

法一(超時)

這個題我一開始的思路是利用冒泡的思想,當當前結點比next結點大的時候,交換兩者next所指向內容。

比如鍊錶1-5-4-6 , 當指標p指向5所在結點的時候,發現p->next的num小於當前的num,這時候我要令p1=p->next(4所在結點), 讓(5)p->next指向 (6)p1->next, 再讓 (4)p1->next指向(5)p,最後再讓(1)pre接上(4)p1,這樣,鍊錶的結點順序就變成了 1-4-5-6。(注意如果是第一位的話就不用使用pre指標)

#include

"stdio.h"

#include

"malloc.h"

#define len sizeof(struct student)

struct student

;struct student *

create

(int n)

return

(head);}

void

print

(struct student *head)

}struct student *

insert

(struct student *head,

struct student *stud)

else

if( p0->num < p1->num )

else

}return

(head);}

struct student *

del(

struct student *head,

long num)

p2=p1;

p1=p1->next;

}return

(head);}

struct student *

sort

(struct student *head)

// printf("%d\n",count);

for(i=

1;i<=count;i++

)else

// print(head);

// printf("%d %d %d\n",p1->num,p2->num,p3->num);

}else

// printf("1\n");}}

return head;

}main()

發現可以

發現可以達到預期效果但是,這個方法的迴圈次數太多,哪怕優化後,對內層加上了小於i就continue,也還是超時。

法二

既然改變鍊錶結點順序會超時,那麼就退一步,我只要保持冒泡,將滿足判斷語句(後比前小)結構體裡面的成員替換就好了。

比如1-5-4-6

當p指向5時,發現下乙個的num更小,那麼我就把下乙個結點的num和我當前的num換一下,結點還是原來的結點,無需變動,只需改變成員變數就行了。

#include

"stdio.h"

#include

"malloc.h"

#define len sizeof(struct student)

struct student

;struct student *

create

(int n)

return

(head);}

void

print

(struct student *head)

}struct student *

insert

(struct student *head,

struct student *stud)

else

if( p0->num < p1->num )

else

}return

(head);}

struct student *

del(

struct student *head,

long num)

p2=p1;

p1=p1->next;

}return

(head);}

struct student *

sort

(struct student *head)}}

return head;

}main()

這樣就快了很多。至於還有沒有更好的方法,歡迎指出。

GRE填空題的命題規律

很多考生在gre考試的備戰中對新gre填空的命題規律感興趣,他們認為gre填空題的準備只有多看多做,沒什麼技巧規律可言。這是複習思想上的誤區。每一門考試都有自己的命題規律和解題技巧。下面大家一起看看gre考試填空的命題規律。gre填空題出題規律,首先是難度方面,通過ets的出題思路來判斷,gre題目...

一道簡單的填空題

在慕課python基礎課看到一題 十位數迴圈從1至9,個位數迴圈從0至9。系統給的提示 如下 課件給的 for x in for y in 剛開始沒看懂課件給的提示啥意思,於是按照自己的思維寫了 1 fornum in range 10,100 num str num ifnum 0 num 1 c...

藍橋杯四6三部排序填空題

一般的排序有許多經典演算法,如快速排序 希爾排序等。但實際應用時,經常會或多或少有一些特殊的要求。我們沒必要套用那些經典演算法,可以根據實際情況建立更好的解法。比如,對乙個整型陣列中的數字進行分類排序 使得負數都靠左端,正數都靠右端,0在中部。注意問題的特點是 負數區域和正數區域內並不要求有序。可以...