c語言鏈表的排序
Ⅰ 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語言 鏈表如何進行排序!
//排序( 參數為鏈表頭 )
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語言做鏈表的排序
主要修改了sort函數,採用冒泡排序演算法進行排序的。
你其他的兩個函數寫的不錯,就sort函數寫的有問題,已經很不錯了。
注意:程序結束,最好對鏈表進行銷毀,否則,內存永遠也不會釋放,導致內存泄漏了。
修改如下:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#define N 5
typedef struct node
{
char name[20];
int score;
struct node *link;
}stud;
stud *sort(stud *head) /*排序函數*/
{
stud *temp=NULL; //默認為NULL,也就是鏈表的結尾
stud *ptr1=head;
stud *ptr2=head->link;
while(ptr1->link!=temp)//(ptr1!=NULL)
{
//ptr2=ptr1->link; //放在循環體下面了
while(ptr2->link!=temp)//(ptr2!=NULL)
{
if(ptr1->link->score > ptr2->link->score) //(ptr1->score > ptr2->score)
{//交換 ptr1->link和ptr2->link,而不是ptr1和ptr2,否則無法交換
ptr1->link=ptr2->link;
ptr2->link=ptr2->link->link;//temp->link=ptr2;
ptr1->link->link=ptr2;//ptr2->link=ptr1;
}
ptr1=ptr1->link;//ptr2=ptr2->link;
ptr2=ptr1->link;//從上面移動下來的
}
temp=ptr2;//新加的
ptr1=head;//ptr1=ptr1->link;
ptr2=ptr1->link;//從上面移動下來的
}
return (head);
}
stud * creat(int n)
{
stud *p,*h,*s;
int i;
if((h=(stud *)malloc(sizeof(stud)))==NULL)
{
printf("不能分配內存空間!");
exit(0);
}
h->name[0]='\0';
h->link=NULL;
p=h;
for(i=0;i<n;i++)
{
if((s= (stud *) malloc(sizeof(stud)))==NULL)
{
printf("不能分配內存空間!");
exit(0);
}
s->link=NULL;//p->link=s; //跟下句對調了一下,為了把相關的代碼放在一起
printf("請輸入第%d個人的姓名",i+1);
scanf("%s",s->name);
printf("請輸入第%d個人的分數",i+1);
scanf("%d",&s->score);
p->link=s; //s->link=NULL;//跟上句對調了一下,為了把相關的代碼放在一起
p=s;
}
return(h);
}
void print(stud *h)
{
stud *p;
p=h->link;
printf("數據信息為:\n");
while(p!=NULL)
{
printf("%s ",&*(p->name));
printf("的分數為%d\n",p->score);
p=p->link;
}
}
void main()
{
stud *head;
head=creat(N);
head=sort(head);
print(head);
getchar();
}
Ⅳ c語言數據結構(雙向鏈表排序)
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
float data;
struct node *pre;
struct node *next;
}*DoubleNode;
void sort_DoubleNode(DoubleNode &head, float data)
{
DoubleNode node = (DoubleNode)malloc(sizeof(struct node));
DoubleNode p,q;
node->data = data;
if(head == NULL)
{
head = node;
head->next = NULL;
head->pre = NULL;
return;
}
p = head;
while(p != NULL && p->data < data)
{
q = p;
p = p->next;
}
if(p == head)
{
node->next = head;
node->pre = NULL;
head ->pre = node;
head = node;
}
else{
q->next = node;
node->pre = q;
node->next = p;
if(p != NULL) p->pre = node;
}
}
void out_DoubleNode(DoubleNode &head)
{
DoubleNode p = head;
while(p != NULL)
{
printf("%.4f ", p->data);
p = p->next;
}
printf("\n");
}
void main()
{
int n;
float data;
DoubleNode head = NULL;
scanf("%d", &n);
for(int i=0; i<n; ++i)
{
scanf("%f", &data);
sort_DoubleNode(head, data);
}
out_DoubleNode(head);
}
/*
15
23.4 4.5 56 67 7.5 90 8.97 90.6 25.6 145
856 10.2 0.36 54.6 56.1
*/
第二題: 測試數據同第一題
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
float data;
struct node *pre;
struct node *next;
}*DoubleNode;
bool bigger(float a, float b)
{
return (a > b);
}
bool smaller(float a, float b)
{
return (a < b);
}
void sort_DoubleNode(DoubleNode &head, float data, bool (*comp)(float a, float b))
{
DoubleNode node = (DoubleNode)malloc(sizeof(struct node));
DoubleNode p,q;
node->data = data;
if(head == NULL)
{
head = node;
head->next = NULL;
head->pre = NULL;
return;
}
p = head;
while(p != NULL && comp(p->data, data))
{
q = p;
p = p->next;
}
if(p == head)
{
node->next = head;
node->pre = NULL;
head ->pre = node;
head = node;
}
else{
q->next = node;
node->pre = q;
node->next = p;
if(p != NULL) p->pre = node;
}
}
void merge_DoubleNode(DoubleNode &head, DoubleNode &head1, DoubleNode &head2)
{
DoubleNode p = head2;
head = head1;
if(head1 == NULL || head2 == NULL) return;
while((head1->next != NULL) && (head2 != NULL))
{
p = head2->next;
head1->next->pre = head2;
head2->next = head1->next;
head2->pre = head1;
head1->next = head2;
head1 = head2->next;
head2 = p;
}
if((head1->next == NULL) && (head2 != NULL))
{
head1->next = head2;
head2->pre = head1;
}
}
void out_DoubleNode(DoubleNode &head)
{
DoubleNode p = head;
while(p != NULL)
{
printf("%.4f ", p->data);
p = p->next;
}
printf("\n");
}
void main()
{
int n;
float data;
DoubleNode head = NULL;
DoubleNode head1 = NULL;
DoubleNode head2 = NULL;
scanf("%d", &n);
for(int i=0; i<n; ++i)
{
scanf("%f", &data);
if((i+1)%2 == 0) sort_DoubleNode(head2, data, &bigger);
else sort_DoubleNode(head1, data, &smaller);
}
merge_DoubleNode(head, head1, head2);
out_DoubleNode(head);
}
/*
15
23.4 4.5 56 67 7.5 90 8.97 90.6 25.6 145
856 10.2 0.36 54.6 56.1
*/
Ⅳ C語言鏈表冒泡排序
我這個效率要高一些,呵呵。
#include <stdio.h>
typedef struct listnode
{
int f;
struct listnode *next;
} ListNode;
ListNode *sort(ListNode *head)
{
ListNode *p,*p1,*p2,*p3;
ListNode h, t;
if (head == NULL) return NULL;
h.next=head;
p=&h;
while (p->next!=NULL)
{
p=p->next;
}
p=p->next=&t;
while (p!=h.next)
{
p3=&h;
p1=p3->next;
p2=p1->next;
while (p2!=p)
{
if ((p1->f)>(p2->f))
{
p1->next=p2->next;
p2->next=p1;
p3->next=p2;
p3=p2;
p2=p1->next;
} else {
p3=p1;
p1=p2;
p2=p2->next;
}
}
p=p1;
}
while (p->next!=&t)
{
p=p->next;
}
p->next=NULL;
return h.next;
}
int main() {
ListNode h,j,k,l;
h.next=&j;
h.f=3;
j.next=&k;
j.f=5;
k.next=&l;
k.f=1;
l.next=NULL;
l.f=7;
ListNode* p = sort(&h);
while (p != NULL) {
printf("%d ", p->f);
p=p->next;
}
printf("\n");
return 0;
}
Ⅵ C語言,鏈表怎麼從大到小排序
//創建data型結構,為生成鏈表做准備
struct data
{
int value;
data *next;
};
//對鏈表進行排列的函數
void line(data *p,int n)
{ int temp,i,j;
data *tp;
for(i=0;i<n-1;i++)
for(j=0,tp=p;j<n-i-1;j++,tp=tp->next)
{ if(tp->value>tp->next->value)
{temp=tp->next->value;
tp->next->value=tp->value;
tp->value=temp;
}
}
}
以上是冒泡法對鏈表排專序的例子;
//輸出答案的屬函數
void answer(data *p)
{ while(p!=NULL) {
cout<<p->value<<endl;
p=p->next;
}
}
以上是遍歷鏈表的函數p是鏈表的頭指針
Ⅶ 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語言中,創建的鏈表怎麼排序!
先創建一個結構體,內容包括編號,姓名,
然後,再對姓名進行排序
,排序方法有選擇法,冒泡法,等都可以,
最後再輸出
Ⅸ C語言鏈表中如何實現對一組數據進行排序
#include<stdio.h>
#include<stdlib.h>
#define NULL 0
struct student * creat();
struct student * link(struct student * head_a,struct student * head_b);
void print(struct student * head);
struct student{
int num;
float score[2];
struct student *next;
}stu;
int main(void)
{
struct student *head_a;
struct student *head_b,*head_c;
printf("請輸入a鏈表學生的數據:0 0 0結束輸入\n");
head_a=creat();
print(head_a);
printf("請輸入b鏈表學生的數據:0 0 0結束輸入\n");
head_b=creat();
print(head_b);
head_c=link(head_a,head_b);
printf("列印經過排序之後的學生數據,a,b鏈數據的結合\n");
print(head_c);
return 0;
}
struct student * creat()
{
int n=0;
struct student * head,*p1,*p2;
p1=p2=(struct student *)malloc(sizeof(struct student ));
scanf("%d%f%f",&p1->num,&p1->score[0],&p1->score[1]);
head=NULL;
while(p1->num!=0)
{
n=n+1;
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student *)malloc(sizeof(struct student));
scanf("%d%f%f",&p1->num,&p1->score[0],&p1->score[1]);
}
p2->next=NULL;
return head;
}
void print(struct student * head)
{
struct student *p;
p=head;
while(p!=NULL)
{
printf("%-10d%-10.1f%-10.1f\n",p->num,p->score[0],p->score[1]);
p=p->next;
}
return ;
}
struct student * link(struct student * head_a,struct student * head_b)
{
int n,m;
m=n=0;
struct student * head,*p1,*p2,*p3,*q;//q是在冒泡排序是(共需N-1趟排序)每趟的最後一次指針p1的位置,開始時q為Null
p1=head_a;
p2=head_b;
head=head_a;
while(p1->next!=NULL)
{p1=p1->next;n++;}
p1->next=p2;
p1=head_a;
while(p1!=NULL)
{
p1=p1->next;
n++; //n是計算鏈表的節點數,以備後面的排序用
}
q=NULL;
p1=head_a;
p2=p1->next ;
while(m<n)
{
m++;
//以下是採用冒泡法進行排序
while(p2!=q)
{
if(p1->num>p2->num)
{
if(head==p1) head=p2;
else p3->next=p2;
p1->next=p2->next;p2->next=p1;
//以下是按照 p3 p1 p2排序
p3=p2;p2=p1->next;
}
else
{
p3=p1;p1=p1->next;p2=p2->next;
}
}
q=p1;p1=head;p2=p1->next;
}
return (head);
}