本文目錄一覽:
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-hk/n/142468.html