排序鏈表c語言,鏈表選擇排序c語言

本文目錄一覽:

C語言如何對鏈表的數進行排序?

同學,給你一段代碼,裡面涵蓋了鏈表的冒泡排序!

#includestdio.h

#includemalloc.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-dataq-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語言鏈表排序

請看ListSort3(L);

程序初始順序是:6 3 67 2 15 13 10 6 4

排好序的順序是:3 2 4 6 6 67 15 13 10

#includestdio.h

#include stdlib.h

#include math.h

/************************************************************************/

/* 常量定義 */

/************************************************************************/

#define ElemType int

#define Status int

#define TRUE 1

#define OK 1

#define FALSE 0

#define ERROR -1

/************************************************************************/

/* 線性表的單鏈表存儲結構*/

/************************************************************************/

typedef struct LNode

{

ElemType data;

struct LNode *next;

}LNode, *LinkList;

/************************************************************************/

/* 操作結果:構造一個空的線性表L */

/************************************************************************/

void InitList(LinkList *L)

{

*L = (LinkList)malloc(sizeof(struct LNode)); /* 產生頭結點,並使L指向此頭結點 */

if( !*L ) /* 存儲分配失敗 */

exit(OVERFLOW);

(*L)-next = NULL; /* 指針域為空 */

}

/************************************************************************/

/* 在帶頭結點的單鏈線性表L中第i個位置之前插入元素e */

/************************************************************************/

Status ListInsert(LinkList L, int i, ElemType e)

{

int j = 0;

LinkList p = L, s;

while( p j i-1) /* 尋找第i-1個結點 */

{

p = p-next;

j++;

}

if( !p|| j i-1) return ERROR;/* i小於1或者大於表長 */

s = (LinkList)malloc(sizeof(struct LNode)); /* 生成新結點 */

s-data = e; /* 插入L中 */

s-next = p-next;

p-next = s;

return OK;

}

/************************************************************************/

/* 初始條件:線性表L已存在。操作結果:依次對L的每個數據元素調用函數vi() */

/************************************************************************/

void ListTraverse(LinkList L, void(*vi)(ElemType))

{

LinkList p = L-next;

while(p)

{

vi(p-data);

p = p-next;

}

printf(“\n”);

}

void printInt(int data)

{

printf(“%d “, data);

}

void ListSort3(LinkList L)

{

LinkList first; //指向鏈表L第一個結點,除頭結點

LinkList pre; //指向first的前驅結點

LinkList last; //指向first指向排好序的最後一個結點

LinkList rest; //指向未排好序的第一個結點,即鏈表第二個結點

LinkList curr; //指向當前結點

first = L-next; //指向第一個結點

if(first == NULL) return;

pre = L ; //pre指向first的前驅結點

last = first; //last指向排好序的最後一個結點

rest = first-next; //指向剩餘的結點

first-next = NULL; //first斷鏈

while (rest) //當餘下的結點不為空

{

//保存當前結點

curr = rest;

//取下一個結點

rest = rest-next;

//當結點小於第一個結點,則鏈接到first前面

if( curr-data first-data )

{

pre-next = curr;

curr-next = first;

pre = curr;

}

//當前結點大於第一個結點,則鏈接到last後

else if(curr-data first-data)

{

curr-next = last-next;

last-next = curr;

last = curr;

}

//當前結點與第一個結點相等,則鏈接到first後面

else

{

curr-next = first-next;

first-next = curr;

}

}

}

void main()

{

LinkList L;

InitList(L);

ListInsert(L, 1, 6);

ListInsert(L, 2, 3);

ListInsert(L, 3, 67);

ListInsert(L, 4, 2);

ListInsert(L, 5, 15);

ListInsert(L, 6, 13);

ListInsert(L, 7, 10);

ListInsert(L, 8, 6);

ListInsert(L, 9, 4);

ListSort3(L);

ListTraverse(L, printInt);

}

c語言中怎麼用鏈表選擇排序????

#include

#include

typedef

struct

node

{

int

data;

struct

node

*next;

}*Linklist,Node;

Linklist

creat(int

n)

{

Linklist

head,r,p;

int

x,i;

head=(Node*)malloc(sizeof(Node));

r=head;

printf(“輸入數字:\n”);

for(i=n;i0;i–)

{

scanf(“%d”,x);

p=(Node*)malloc(sizeof(Node));

p-data=x;

r-next=p;

r=p;

}

r-next=NULL;

return

head;

}

void

output(Linklist

head)

{

Linklist

p;

p=head-next;

do

{

printf(“%8d”,p-data);

p=p-next;

}while(p);

printf(“\n”);

}

void

paixu(Linklist

head)

{

Linklist

p,q,small;

int

temp;

for(p=head-next;p-next!=NULL;p=p-next)

{

small=p;

for(q=p-next;q;q=q-next)

if(q-data

data)

small=q;

if(small!=p)

{

temp=p-data;

p-data=small-data;

small-data=temp;

}

}

printf(“輸出排序後的數字:\n”);

output(head);

}

void

main()

{

Linklist

head;

int

n;

printf(“輸入數字的個數(n):\n”);

scanf(“%d”,n);

head=creat(n);

printf(“輸出數字:\n”);

output(head);

paixu(head);

}

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;

擴展資料:

return表示把程序流程從被調函數轉向主調函數並把表達式的值帶回主調函數,實現函數值的返回,返回時可附帶一個返回值,由return後面的參數指定。

return通常是必要的,因為函數調用的時候計算結果通常是通過返回值帶出的。如果函數執行不需要返回計算結果,也經常需要返回一個狀態碼來表示函數執行的順利與否(-1和0就是最常用的狀態碼),主調函數可以通過返回值判斷被調函數的執行情況。

怎麼用C語言寫單鏈表的排序

從鍵盤輸入不定數量的數字構造鏈表,輸入0結束,然後排序輸出,代碼如下:

#include stdio.h

#include stdlib.h

#include conio.h

typedef struct _list {

int val;

struct _list* next;

} *node, list;

/* 插入節點 */

node Insert( node* head, node pos, int val )

{

node tmp;

tmp = ( node )malloc( sizeof( list ) );

tmp-val = val;

tmp-next = pos ? pos-next : *head;

if ( pos ) {

pos-next = tmp;

} else {

*head = tmp;

}

return tmp;

}

/* 構造鏈表 */

node Create( void )

{

node head, t;

int n;

head = t = NULL;

while ( scanf( “%d”, n ) n != 0 ) {

t = Insert( head, t, n );

}

return head;

}

/* 輸出 */

void Print( node head )

{

while ( head ) {

printf( “%d “, head-val );

head = head-next;

}

putchar( ‘\n’ );

}

/* 排序 */

node msort( node* head, int n )

{

int i, m;

node l, r, p, *x, *y;

if ( n 2 ) return *head;

m = n/2;

p = l = r = *head;

for ( i = m; i 0; –i )

p = r, r = r-next;

p-next = NULL;

l = msort( l, m );

r = msort( r, n – m );

x = p;

while ( l r ) {

*x = l-val r-val ? (y = l, l) : (y = r, r);

*y = (*y)-next; x = (*x)-next;

}

l = l ? l : r ? r : NULL;

*x = l; *head = p;

return p;

}

/* 包裝函數 */

void Sort( node* head )

{

int i;

node tmp = *head;

for ( i = 0; tmp; ++i, tmp = tmp-next );

msort( head, i );

}

int main( void )

{

node head = Create();

Print( head );

Sort( head );

Print( head );

getch();

return 0;

}

望採納

原創文章,作者:GZKL,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/142468.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
GZKL的頭像GZKL
上一篇 2024-10-11 11:43
下一篇 2024-10-12 09:43

相關推薦

  • 利用Python實現兩個鏈表合併為一個有序鏈表

    對於開發工程師來說,實現兩個鏈表合併為一個有序鏈表是必須掌握的技能之一。Python語言在鏈表處理上非常便利,本文將從多個方面詳細闡述如何利用Python實現兩個鏈表合併為一個有序…

    編程 2025-04-29
  • AES加密解密算法的C語言實現

    AES(Advanced Encryption Standard)是一種對稱加密算法,可用於對數據進行加密和解密。在本篇文章中,我們將介紹C語言中如何實現AES算法,並對實現過程進…

    編程 2025-04-29
  • 學習Python對學習C語言有幫助嗎?

    Python和C語言是兩種非常受歡迎的編程語言,在程序開發中都扮演着非常重要的角色。那麼,學習Python對學習C語言有幫助嗎?答案是肯定的。在本文中,我們將從多個角度探討Pyth…

    編程 2025-04-29
  • Python被稱為膠水語言

    Python作為一種跨平台的解釋性高級語言,最大的特點是被稱為”膠水語言”。 一、簡單易學 Python的語法簡單易學,更加人性化,這使得它成為了初學者的入…

    編程 2025-04-29
  • OpenJudge答案1.6的C語言實現

    本文將從多個方面詳細闡述OpenJudge答案1.6在C語言中的實現方法,幫助初學者更好地學習和理解。 一、需求概述 OpenJudge答案1.6的要求是,輸入兩個整數a和b,輸出…

    編程 2025-04-29
  • Python按位運算符和C語言

    本文將從多個方面詳細闡述Python按位運算符和C語言的相關內容,並給出相應的代碼示例。 一、概述 Python是一種動態的、面向對象的編程語言,其按位運算符是用於按位操作的運算符…

    編程 2025-04-29
  • Python語言由荷蘭人為中心的全能編程開發工程師

    Python語言是一種高級語言,很多編程開發工程師都喜歡使用Python語言進行開發。Python語言的創始人是荷蘭人Guido van Rossum,他在1989年聖誕節期間開始…

    編程 2025-04-28
  • Python語言設計基礎第2版PDF

    Python語言設計基礎第2版PDF是一本介紹Python編程語言的經典教材。本篇文章將從多個方面對該教材進行詳細的闡述和介紹。 一、基礎知識 本教材中介紹了Python編程語言的…

    編程 2025-04-28
  • Python語言實現人名最多數統計

    本文將從幾個方面詳細介紹Python語言實現人名最多數統計的方法和應用。 一、Python實現人名最多數統計的基礎 1、首先,我們需要了解Python語言的一些基礎知識,如列表、字…

    編程 2025-04-28
  • Python作為中心語言,在編程中取代C語言的優勢和挑戰

    Python一直以其簡單易懂的語法和高效的編碼環境而著名。然而,它最近的發展趨勢表明Python的使用範圍已經從腳本語言擴展到了從Web應用到機器學習等廣泛的開發領域。與此同時,C…

    編程 2025-04-28

發表回復

登錄後才能評論