『壹』 c語言如何對鏈表的數進行排序
同學,給你一段代碼,裡面涵蓋了鏈表的冒泡排序!
#include<stdio.h>
#include<malloc.h>
typedef struct node
{
int data;/*data代表成績分數*/
struct node *next;
}LNode,*LinkList;
LinkList Creat(void)/*創建鏈表,結束標志為當輸入的數據為0!*/
{
LinkList H,p1,p2;
int n;
n=0;
p1=p2=(LinkList)malloc(sizeof(LNode));
printf("輸入數據:");
scanf("%d",&p1->data);
H=NULL;
while(p1->data!=0)
{
n=n+1;
if(n==1)
H=p1;
else
p2->next=p1;
p2=p1;
p1=(LinkList)malloc(sizeof(LNode));
scanf("%d",&p1->data);
}
p2->next=NULL;
return(H);
}
LinkList Sort(LinkList SL)/*遞增排序函數:入口參數:鏈表的頭指針,此為鏈表中的排序函數*/
{
LinkList p,q;
int temp;
for(p=SL;p!=NULL;p=p->next)
{
for(q=p->next;q!=NULL;q=q->next)
{
if(p->data>q->data)
{
temp=q->data;
q->data=p->data;
p->data=temp;
}
}
}
return SL;
}
int main()
{
LinkList L,S,K;
L=Creat();
printf("初始化的單鏈表數據序列為:\n");
for(S=L;S!=NULL;S=S->next)
printf("%d ",S->data);
Sort(L);
printf("\n按遞增順序排序後的序列為:\n");
for(K=L;K!=NULL;K=K->next)
printf("%d==>",K->data);
return 0;
}
『貳』 C語言的鏈表怎麼排序
其實最簡單的方法就是,重新建一個鏈表存儲有序序列,把原鏈表裡的元素一個一個地取出來,放到新鏈表裡。放進去的時候二分一下當前序列(可能要比二分數組慢一點,因為要一個一個地往前或者往後找,沒有直接按下標找方便),找到當前元素在序列里正確的位置。這種方法的時間復雜度大概在O(n*log2n)左右
基本流程:
初始狀態:
原鏈表:1--->2--->4--->3--->NULL
新鏈表:NULL
第一次處理:
原鏈表:2--->4--->3--->NULL
新鏈表:1--->NULL
第二次處理:
原鏈表:4--->3--->NULL
新鏈表:1--->2--->NULL
第三次處理:
原鏈表:3--->NULL
新鏈表:1--->2--->4--->NULL
第四次處理:
原鏈表:NULL
新鏈表:1--->2--->3--->4--->NULL
或者建一個二叉樹,類似於bst的結構(左子<根<右子),再中序遍歷一下。
或者不用那些基於比較的排序,用基數排序一類的方法,可以在O(n)的時間內解決問題C+_+C
『叄』 C語言 鏈表如何進行排序!
//排序( 參數為鏈表頭 )
void Sorting( student* pHead )
{
//參數判斷(如果是空就返回)
if ( pHead == NULL )
{
return;
}
//臨時變數..做冒泡循環用..
student* pTempA = pHead;
student* pTempB = pHead;
//這兩個while相當於冒泡的嵌套for循環
while( pTempA != NULL )
{
while ( pTempB != NULL )
{
//判斷結構體裡面int類型(學號)大小
if ( pTempA->iNum < pTempB->iNum )
{
//將結構體里int類型(學號)值進行交換
int Num = pTempA->iNum;
pTempA->iNum = pTempB->iNum;
pTempB->iNum = Num;
//將char類型(名字)做交換
char Name[ 16 ];
strcpy ( Name, pTempA->strName );
strcpy ( pTempA->strName, pTempB->strName );
strcpy ( pTempB->strName, Name );
//因為指針變數不做交換...所以不做結構體的直接交換
//除了指向下一個結點的指針變數外所有的值進行交換
}
//做鏈表遍歷(相當於循環的i++)
pTempB = pTempB->pNext;
}
//因為for每次進來i = 0; 這里while循環結束..要讓pTempB重新回到頭..
pTempB = pHead;
//做鏈表遍歷(相當於循環的i++)
pTempA = pTempA->pNext;
}
}
連接晚上回來給你寫...
『肆』 C語言鏈表排序
#include"stdafx.h"
#include<stdlib.h>
//創建一個節點,data為value,指向NULL
Node*Create(intvalue){
Node*head=(Node*)malloc(sizeof(Node));
head->data=value;
head->next=NULL;
returnhead;
}
//銷毀鏈表
boolDestroy_List(Node*head){
Node*temp;
while(head){
temp=head->next;
free(head);
head=temp;
}
head=NULL;
returntrue;
}
//表後添加一個節點,Create(value)
boolAppend(Node*head,intvalue){
Node*n=Create(value);
Node*temp=head;
while(temp->next){
temp=temp->next;
}
temp->next=n;
return0;
}
//列印鏈表
voidPrint_List(Node*head){
Node*temp=head->next;
while(temp){
printf("%d->",temp->data);
temp=temp->next;
}
printf("\n");
}
//在鏈表的第locate個節點後(頭節點為0)插入創建的節點Create(value)
boolInsert_List(Node*head,intlocate,intvalue){
Node*temp=head;
Node*p;
Node*n=Create(value);
if(locate<0)
returnfalse;
while(locate--){
if(temp->next==NULL){
temp->next=Create(value);
returntrue;
}
temp=temp->next;
}
p=temp->next;
temp->next=n;
n->next=p;
returntrue;
}
//刪除第locate個節點後(頭節點為0)的節點
boolDelete_List(Node*head,intlocate){
Node*temp=head;
Node*p;
if(locate<0)
returnfalse;
while(locate--){
if(temp==NULL){
returnfalse;
}
temp=temp->next;
}
p=temp->next->next;
free(temp->next);
temp->next=NULL;
temp->next=p;
returntrue;
}
//獲取鏈表長度(不包括頭節點)
intSize_List(Node*head){
Node*temp=head;
intsize=0;
while(temp->next){
temp=temp->next;
size++;
}
returnsize;
}
//鏈表的三種排序(選擇,插入,冒泡)
boolSort_List(Node*head){
intt=0;
intsize=Size_List(head);
//選擇排序
/*for(Node*temp=head->next;temp!=NULL;temp=temp->next){
for(Node*p=temp;p!=NULL;p=p->next){
if(temp->data>p->data){
printf("換%d和%d\n",temp->data,p->data);
t=temp->data;
temp->data=p->data;
p->data=t;
}
}
}*/
//插入排序
/*for(Node*temp=head->next->next;temp!=NULL;temp=temp->next){
for(Node*p=head;p->next!=NULL;p=p->next){
if(p->next->data>temp->data)
{
printf("換%d和%d\n",temp->data,p->next->data);
t=temp->data;
temp->data=p->next->data;
p->next->data=t;
}
}
}*/
//冒泡排序
for(Node*temp=head->next;temp->next!=NULL;temp=temp->next){
for(Node*p=head->next;p->next!=NULL;p=p->next){
if(p->data>p->next->data){
t=p->data;
p->data=p->next->data;
p->next->data=t;
}
}
}
return0;
}
(4)c語言將無序的鏈表排序擴展閱讀:
return表示把程序流程從被調函數轉向主調函數並把表達式的值帶回主調函數,實現函數值的返回,返回時可附帶一個返回值,由return後面的參數指定。
return通常是必要的,因為函數調用的時候計算結果通常是通過返回值帶出的。如果函數執行不需要返回計算結果,也經常需要返回一個狀態碼來表示函數執行的順利與否(-1和0就是最常用的狀態碼),主調函數可以通過返回值判斷被調函數的執行情況。
『伍』 C語言 鏈表怎麼排序 急求大蝦
排序!這是一個龐大的話題,有插入排序,插入排序又分直接插入排序、希爾排序等,還有交換排序,交換排序有冒泡排序、快速排序,還有選擇排序,有直接選擇排序、歸並排序等等…而且還不斷的有新的排序方法產生…不知道你要哪一種…新手一般用選擇排序和冒泡排序,方法簡單,兩重循環。
『陸』 C語言單向鏈表排序如何實現
struct student* printf_sort(struct student *head)
{
struct student *p1,*p2,*ptemp,*pfinished=NULL;
for(p1=head;p1->next!=pfinished;)//對鏈表進行從大到小排序(這里用冒泡法)
//p1使之總是指向頭結點,pfinished使之總是指向已排序好的最前面的結點
//ptemp作為中介,保存p2的上一個結點
{
for(p2=p1;p2->next!=pfinished;)
{
if(p2->num<p2->next->num)//p2的值小於p2->next的值,交換 {
if(p2==p1)//頭結點要交換
{
p1=p2->next;
p2->next=p1->next;
p1->next=p2;
ptemp=p1;
}
else
{
ptemp->next=p2->next;
ptemp=p2->next;
p2->next=ptemp->next;
ptemp->next=p2;
}
}
else//不需要交換,則p2、ptemp前進1位
{
ptemp=p2;
p2=p2->next;
}
}
pfinished=p2;
}
}
『柒』 C語言做鏈表的排序
#include"stdafx.h"
#include<stdlib.h>
//創建一個節點,data為value,指向NULL
Node*Create(intvalue){
Node*head=(Node*)malloc(sizeof(Node));
head->data=value;
head->next=NULL;
returnhead;
}
//銷毀鏈表
boolDestroy_List(Node*head){
Node*temp;
while(head){
temp=head->next;
free(head);
head=temp;
}
head=NULL;
returntrue;
}
//表後添加一個節點,Create(value)
boolAppend(Node*head,intvalue){
Node*n=Create(value);
Node*temp=head;
while(temp->next){
temp=temp->next;
}
temp->next=n;
return0;
}
//列印鏈表
voidPrint_List(Node*head){
Node*temp=head->next;
while(temp){
printf("%d->",temp->data);
temp=temp->next;
}
printf("\n");
}
//在鏈表的第locate個節點後(頭節點為0)插入創建的節點Create(value)
boolInsert_List(Node*head,intlocate,intvalue){
Node*temp=head;
Node*p;
Node*n=Create(value);
if(locate<0)
returnfalse;
while(locate--){
if(temp->next==NULL){
temp->next=Create(value);
returntrue;
}
temp=temp->next;
}
p=temp->next;
temp->next=n;
n->next=p;
returntrue;
}
//刪除第locate個節點後(頭節點為0)的節點
boolDelete_List(Node*head,intlocate){
Node*temp=head;
Node*p;
if(locate<0)
returnfalse;
while(locate--){
if(temp==NULL){
returnfalse;
}
temp=temp->next;
}
p=temp->next->next;
free(temp->next);
temp->next=NULL;
temp->next=p;
returntrue;
}
//獲取鏈表長度(不包括頭節點)
intSize_List(Node*head){
Node*temp=head;
intsize=0;
while(temp->next){
temp=temp->next;
size++;
}
returnsize;
}
//鏈表的三種排序(選擇,插入,冒泡)
boolSort_List(Node*head){
intt=0;
intsize=Size_List(head);
//選擇排序
/*for(Node*temp=head->next;temp!=NULL;temp=temp->next){
for(Node*p=temp;p!=NULL;p=p->next){
if(temp->data>p->data){
printf("換%d和%d\n",temp->data,p->data);
t=temp->data;
temp->data=p->data;
p->data=t;
}
}
}*/
//插入排序
/*for(Node*temp=head->next->next;temp!=NULL;temp=temp->next){
for(Node*p=head;p->next!=NULL;p=p->next){
if(p->next->data>temp->data)
{
printf("換%d和%d\n",temp->data,p->next->data);
t=temp->data;
temp->data=p->next->data;
p->next->data=t;
}
}
}*/
//冒泡排序
for(Node*temp=head->next;temp->next!=NULL;temp=temp->next){
for(Node*p=head->next;p->next!=NULL;p=p->next){
if(p->data>p->next->data){
t=p->data;
p->data=p->next->data;
p->next->data=t;
}
}
}
return0;
}
(7)c語言將無序的鏈表排序擴展閱讀:
return表示把程序流程從被調函數轉向主調函數並把表達式的值帶回主調函數,實現函數值的返回,返回時可附帶一個返回值,由return後面的參數指定。
return通常是必要的,因為函數調用的時候計算結果通常是通過返回值帶出的。如果函數執行不需要返回計算結果,也經常需要返回一個狀態碼來表示函數執行的順利與否(-1和0就是最常用的狀態碼),主調函數可以通過返回值判斷被調函數的執行情況。
『捌』 C語言 單向鏈表如何排序
void link_order(STU *p_head)
{
STU *pb, *pf, temp;
pf = p_head;
if(p_head == NULL) {//鏈表為空
printf("needn't order. ");
return ;
}
if(p_head->next == NULL) {//鏈表有1個節點
printf("only one print, needn't order. ");
return ;
}
while(pf->next != NULL) {//以pf指向的節點為基準節點
pb = pf->next;//pb從基準點的下一個節點開始
while(pb != NULL) {
if(pf->num > pb->num) {
temp = *pf;
*pf = *pb;
*pb = temp;
temp.next = pf->next;
pf->next = pb->next;
pb->next = temp.next;
}
pb = pb->next;
}
pf = pf->next;
}
return ;
}
(8)c語言將無序的鏈表排序擴展閱讀:
鏈表的排序有三種情況:
1、鏈表為空時:不用排序;
2、鏈表中有一個節點:不用排序;
3、鏈表中兩個及其以上節點時:排序。
return 0代表程序正常退出。return是C++預定義的語句,它提供了終止函數執行的一種方式。當return語句提供了一個值時,這個值就成為函數的返回值。
return語句用來結束循環,或返回一個函數的值。
1、return 0,說明程序正常退出,返回到主程序繼續往下執行。
2、return 1,說明程序異常退出,返回主調函數來處理,繼續往下執行。return 0或return 1對程序執行的順序沒有影響,只是大家習慣於使用return(0)退出子程序而已。
『玖』 一道C語言中關於鏈表排序的代碼的思路與步驟
/*代碼的整體思路是分為3個函數,一個建立鏈表,一個鏈表排序,最後一個是輸出鏈表,
而程序調用也是根據這個順序來調用的,詳細說明在下面*/
#include
#include
#define
LEN
sizeof(struct
number)
struct
number
{
int
num;
struct
number
*next;
};
int
n=0,sum=0;
void
main()
{
struct
number
*creat();
void
print(struct
number
*head);
struct
number
*change(struct
number
*head);
struct
number
*ahead;
ahead=creat();
sum+=n;
//sum計算結點的總數
change(ahead);
print(ahead);
}
struct
number
*creat()
//創建鏈表
{
struct
number
*p1,*p2,*head;
p1=p2=(struct
number*)malloc(LEN);
//LEN就是define定義的sizeof(struct
number)
printf("input
a
number,not
zero\n");
scanf("%d",&p1->num);
//將p1的num賦值為輸入的數
while(p1->num!=0)
//如果輸入的數不是0
{
n++;
//是記錄鏈表節點的數量
if(n==1)
//如果n是建立的第一個結點,也就是鏈表的頭
head=p1;
else
//否則p2的next指針指向p1,也就是原來鏈表最後一個結點的next指向新結點p1
p2->next=p1;
p2=p1;
//p2指向p1,每次p2都指向鏈表的結尾
p1=(struct
number*)malloc(LEN);
//申請內存,建立新結點
scanf("%d",&p1->num);
//再輸入號
}
p2->next
=NULL;
//將尾結點的next指針指向NULL
return
head;
//返回頭指針
}
struct
number
*change(struct
number
*head)
{
struct
number
*p,*h;
int
i;
for(i=1;i
next;p->next!=NULL;p=p->next,h=h->next)
//p指向頭結點,h指向p的後一個結點,每循環一次p和h都往後指一個結點
if(p->num>h->num)
//如果前面的結點比後面結點大,就交換,只交換num的值
{
int
t;
t=p->num;
p->num=h->num;
h->num=t;
}
return
head;
}
void
print(struct
number
*head)
//鏈表的便利,只便利num值
{
struct
number
*p;
p=head;
while(p!=NULL)
{
printf("%d",p->num
);
p=p->next
;
}
}
『拾』 關於C語言鏈表排序的問題
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
structnumber//鏈表節點
{
intnum;
structnumber*next;
};
intn;
structnumber*creat()//創建鏈表
{
structnumber*head;//頭節點
structnumber*p1,*p2;
n=0;
p1=p2=(structnumber*)malloc(sizeof(structnumber));
scanf("%d",&p1->num);
head=NULL;
while(p1->num!=0)//循環輸入鏈表數據,當輸入為0時結束,鏈表數據不包括0
{
n=n+1;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(structnumber*)malloc(sizeof(structnumber));
scanf("%d",&p1->num);
}
p2->next=NULL;
return(head);
}
voidprint(structnumber*head)//鏈表從小到大排序,並輸出
{
structnumber*p1,*p2,*p;
inti,j,t;
printf("這%d個數從小到大的排序為: ",n);
if(head!=NULL)
{
//冒泡排序
for(j=0;j<n-1;j++)
{
p1=head;p2=head;
for(i=0;i<n-1-j;i++)
{
p2=p1->next;
if(p1->num>=p2->num)
{
t=p1->num;
p1->num=p2->num;
p2->num=t;
}
p1=p1->next;
}
}
}
p=head;
if(head!=NULL)
{
//輸出鏈表值
do
{
printf("%3d",p->num);
p=p->next;
}while(p!=NULL);
}
}
voidmain()
{
structnumber*head;
head=creat();
print(head);
}