① 雙向鏈表的排序...(用c語言編寫程序)
#include
#include
struct node
{
int data;
struct node* next;
struct node* prev;
};
struct node* create_list(int n)
{
struct node *head = null;
struct node *p = null, *q = null;
int i = 0, data = 0;
for (i = 0; i < n; i++)
{
printf("請輸入結點%d的值:", i+1);
scanf("%d", &data);
p = (struct node*)malloc(sizeof(struct node));
p->next = null;
p->data = data;
if (i == 0)
{
head = p;
p->prev = null;
}
else
{
p->prev = q;
q->next = p;
}
q = p;
}
return head;
}
void free_list(struct node* head)
{
struct node* p = head;
struct node* q = null;
while (p != null)
{
q = p;
p = p->next;
delete q;
}
}
void display_list(struct node* head)
{
struct node* p = head;
while (p != null)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
struct node* get_max_node(struct node* node)
{
struct node* max_node = node;
while (node != null)
{
if (node->data > max_node->data)
{
max_node = node;
}
node = node->next;
}
return max_node;
}
void swap_node(struct node* node1, struct node* node2)
{
int temp;
if (node1 == node2)
{
return;
}
temp = node1->data;
node1->data = node2->data;
node2->data = temp;
}
void sort_list(struct node* head)
{
struct node* max_node = null;
struct node* current = null;
current = head;
while (current != null)
{
max_node = get_max_node(current);
swap_node(current, max_node);
current = current->next;
}
}
int main()
{
struct node *head = null;
int n = 0;
printf("請輸入雙向鏈表的結點個數:");
scanf("%d", &n);
head = create_list(n);
display_list(head);
sort_list(head);
display_list(head);
free_list(head);
return 0;
}
② 雙向鏈表排序c語言程序設計
/************************************************************************/
/*
文件名 doublelnk.h
作用 定義必要的結構體,並對雙向鏈表的操作函數做聲明
*/
/************************************************************************/
#ifndefDList_H
#defineDList_H
typedefintItem;
typedefstructNode*PNode;
typedefPNodePosition;
/*定義節點類型*/
typedefstructNode
{
Itemid; /*編號*/
Itemdata; /*數據域*/
PNodeprevious;/*指向前驅*/
PNodenext; /*指向後繼*/
}Node;
/*定義鏈表類型*/
typedefstruct
{
PNodehead; /*指向頭節點*/
PNodetail; /*指向尾節點*/
intsize;
}DList;
enumenumSortType
{
ID,
DATA
};
/*分配值為i的節點,並返回節點地址*/
PositionMakeNode(Itemid,Itemdata);
/*釋放p所指的節點*/
voidFreeNode(PNodep);
/*構造一個空的雙向鏈表*/
DList*InitList();
/*摧毀一個雙向鏈表*/
voidDestroyList(DList*plist);
/*將一個鏈表置為空表,釋放原鏈表節點空間*/
voidClearList(DList*plist);
/*返回頭節點地址*/
PositionGetHead(DList*plist);
/*返回尾節點地址*/
PositionGetTail(DList*plist);
/*返回鏈表大小*/
intGetSize(DList*plist);
/*返回p的直接後繼位置*/
PositionGetNext(Positionp);
/*返回p的直接前驅位置*/
PositionGetPrevious(Positionp);
/*將pnode所指節點插入第一個節點之前*/
PNodeInsFirst(DList*plist,PNodepnode);
/*將鏈表第一個節點刪除並返回其地址*/
PNodeDelFirst(DList*plist);
/*獲得節點的數據項*/
ItemGetItem(Positionp);
/*設置節點的數據項*/
voidSetItem(Positionp,Itemi);
/*刪除鏈表中的尾節點並返回其地址,改變鏈表的尾指針指向新的尾節點*/
PNodeRemove(DList*plist);
/*在鏈表中p位置之前插入新節點S*/
PNodeInsBefore(DList*plist,Positionp,PNodes);
/*在鏈表中p位置之後插入新節點s*/
PNodeInsAfter(DList*plist,Positionp,PNodes);
/*返回在鏈表中第i個節點的位置*/
PNodeLocatePos(DList*plist,inti);
voidListTraverse(DList*plist,void(*visit)(Node));
/*對雙向鏈表按照執行的排序方式進行排序*/
voidSortDLnk(DList*plist,enumSortTypesortType);
voidSortDLnkbyID(DList*plist);
voidSortDLnkbyData(DList*plist);
voidswapnode(PNodeanode,PNodebnode);
#endif
/************************************************************************/
/*
文件名 doublelnk.cpp
作用 對雙向鏈表的操作函數進行具體實現
*/
/************************************************************************/
#include"doublelnk.h"
#include<malloc.h>
#include<stdlib.h>
/*分配值為i的節點,並返回節點地址*/
PositionMakeNode(Itemid,Itemdata)
{
PNodep=NULL;
p=(PNode)malloc(sizeof(Node));
if(p!=NULL)
{
p->id=id;
p->data=data;
p->previous=NULL;
p->next=NULL;
}
returnp;
}
/*釋放p所指的節點*/
voidFreeNode(PNodep)
{
free(p);
}
/*構造一個空的雙向鏈表*/
DList*InitList()
{
DList*plist=(DList*)malloc(sizeof(DList));
PNodehead=MakeNode(0,0);
if(plist!=NULL)
{
if(head!=NULL)
{
plist->head=head;
plist->tail=head;
plist->size=0;
}
else
returnNULL;
}
returnplist;
}
/*摧毀一個雙向鏈表*/
voidDestroyList(DList*plist)
{
ClearList(plist);
free(GetHead(plist));
free(plist);
}
/*判斷鏈表是否為空表*/
intIsEmpty(DList*plist)
{
if(GetSize(plist)==0&&GetTail(plist)==GetHead(plist))
return1;
else
return0;
}
/*將一個鏈表置為空表,釋放原鏈表節點空間*/
voidClearList(DList*plist)
{
PNodetemp,p;
p=GetTail(plist);
while(!IsEmpty(plist))
{
temp=GetPrevious(p);
FreeNode(p);
p=temp;
plist->tail=temp;
plist->size--;
}
}
/*返回頭節點地址*/
PositionGetHead(DList*plist)
{
returnplist->head;
}
/*返回尾節點地址*/
PositionGetTail(DList*plist)
{
returnplist->tail;
}
/*返回鏈表大小*/
intGetSize(DList*plist)
{
returnplist->size;
}
/*返回p的直接後繼位置*/
PositionGetNext(Positionp)
{
returnp->next;
}
/*返回p的直接前驅位置*/
PositionGetPrevious(Positionp)
{
returnp->previous;
}
/*將pnode所指節點插入第一個節點之前*/
PNodeInsFirst(DList*plist,PNodepnode)
{
Positionhead=GetHead(plist);
if(IsEmpty(plist))
plist->tail=pnode;
plist->size++;
pnode->next=head->next;
pnode->previous=head;
if(head->next!=NULL)
head->next->previous=pnode;
head->next=pnode;
returnpnode;
}
/*將鏈表第一個節點刪除,返回該節點的地址*/
PNodeDelFirst(DList*plist)
{
Positionhead=GetHead(plist);
Positionp=head->next;
if(p!=NULL)
{
if(p==GetTail(plist))
plist->tail=p->previous;
head->next=p->next;
head->next->previous=head;
plist->size--;
}
returnp;
}
/*獲得節點的數據項*/
ItemGetItem(Positionp)
{
returnp->data;
}
/*設置節點的數據項*/
voidSetItem(Positionp,Itemi)
{
p->data=i;
}
/*刪除鏈表中的尾節點並返回地址,改變鏈表的尾指針指向新的尾節點*/
PNodeRemove(DList*plist)
{
Positionp=NULL;
if(IsEmpty(plist))
returnNULL;
else
{
p=GetTail(plist);
p->previous->next=p->next;
plist->tail=p->previous;
plist->size--;
returnp;
}
}
/*在鏈表中p位置之前插入新節點s*/
PNodeInsBefore(DList*plist,Positionp,PNodes)
{
s->previous=p->previous;
s->next=p;
p->previous->next=s;
p->previous=s;
plist->size++;
returns;
}
/*在鏈表中p位置之後插入新節點s*/
PNodeInsAfter(DList*plist,Positionp,PNodes)
{
s->next=p->next;
s->previous=p;
if(p->next!=NULL)
p->next->previous=s;
p->next=s;
if(p=GetTail(plist))
plist->tail=s;
plist->size++;
returns;
}
/*返回在鏈表中第i個節點的位置*/
PNodeLocatePos(DList*plist,inti)
{
intcnt=0;
Positionp=GetHead(plist);
if(i>GetSize(plist)||i<1)
returnNULL;
while(++cnt<=i)
{
p=p->next;
}
returnp;
}
/*依次對鏈表中每個元素調用函數visit()*/
voidListTraverse(DList*plist,void(*visit)(Node))
{
Positionp=GetHead(plist);
if(IsEmpty(plist))
exit(0);
else
{
while(p->next!=NULL)
{
p=p->next;
visit(*p);
}
}
}
voidSortDLnk(DList*plist,enumSortTypesortType)
{
switch(sortType)
{
caseID:
SortDLnkbyID(plist);
break;
caseDATA:
SortDLnkbyData(plist);
break;
}
}
voidSortDLnkbyID(DList*plist)
{
PNodehead=plist->head;
Nodetmpnode;
Positioncurrnode=head;
Positionbianlinode;
while((currnode=currnode->next)!=NULL)
{
bianlinode=currnode;
while((bianlinode=bianlinode->next)!=NULL)
{
if(currnode->id>bianlinode->id)
{
swapnode(currnode,bianlinode);
}
}
}
}
voidSortDLnkbyData(DList*plist)
{
PNodehead=plist->head;
Nodetmpnode;
Positioncurrnode=head;
Positionbianlinode;
while((currnode=currnode->next)!=NULL)
{
bianlinode=currnode;
while((bianlinode=bianlinode->next)!=NULL)
{
if(currnode->data>bianlinode->data)
{
swapnode(currnode,bianlinode);
}
}
}
}
voidswapnode(PNodeanode,PNodebnode)
{//這裡面要特別注意防止前驅節點是頭結點和後驅節點是Null的情況
Nodetmpnode;
tmpnode.id=anode->id;
tmpnode.data=anode->data;
anode->id=bnode->id;
anode->data=bnode->data;
bnode->id=tmpnode.id;
bnode->data=tmpnode.data;
}/************************************************************************/
/*
文件名 program.cpp
作用 對雙向鏈表的操作函數進行測試
*/
/************************************************************************/
#include"doublelnk.h"
#include<stdio.h>
voidprint(Nodenode)
{
printf("數據項:id=%d,data=%d ",node.id,node.data);
}
intmain()
{
DList*plist=NULL;
PNodep=NULL;
plist=InitList();
p=InsFirst(plist,MakeNode(12,23));
InsBefore(plist,p,MakeNode(2,54));
InsAfter(plist,p,MakeNode(3,34));
printf("p前驅位置的值為%d ",GetItem(GetPrevious(p)));
printf("p位置的值為%d ",GetItem(p));
printf("p後繼位置的值為%d ",GetItem(GetNext(p)));
printf("遍歷輸出各節點數據項: ");
ListTraverse(plist,print);
printf("按照ID排序後遍歷輸出: ");
SortDLnk(plist,ID);
ListTraverse(plist,print);
printf("按照Data排序後遍歷輸出: ");
SortDLnk(plist,DATA);
ListTraverse(plist,print);
printf("除了頭節點該鏈表共有%d個節點 ",GetSize(plist));
FreeNode(DelFirst(plist));
printf("刪除第一個節點後重新遍歷輸出為: ");
ListTraverse(plist,print);
printf("除了頭節點該鏈表共有%d個節點 ",GetSize(plist));
DestroyList(plist);
printf("鏈表已被銷毀 ");
return0;
}
程序總共分三部分, 分別是雙向鏈表的頭文件、源文件和測試程序
建立工程後,分別建立相應的文件並添加相應代碼應該就可以
下面的圖片是我的運行效果(聲明:程序中很多代碼參考了以為前輩的代碼http://blog.csdn.net/hopeyouknow/article/details/6716177)
③ 幫忙用c語言設計一個雙向鏈表的排序
#include <malloc.h>
#include <stdio.h>
struct Node
{
int data;
struct Node* next;
struct Node* prev;
};
struct Node* create_list(int N)
{
struct Node *head = NULL;
struct Node *p = NULL, *q = NULL;
int i = 0, data = 0;
for (i = 0; i < N; i++)
{
printf("請輸入結點%d的值:", i+1);
scanf("%d", &data);
p = (struct Node*)malloc(sizeof(struct Node));
p->next = NULL;
p->data = data;
if (i == 0)
{
head = p;
p->prev = NULL;
}
else
{
p->prev = q;
q->next = p;
}
q = p;
}
return head;
}
void free_list(struct Node* head)
{
struct Node* p = head;
struct Node* q = NULL;
while (p != NULL)
{
q = p;
p = p->next;
delete q;
}
}
void display_list(struct Node* head)
{
struct Node* p = head;
while (p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
struct Node* get_max_node(struct Node* node)
{
struct Node* max_node = node;
while (node != NULL)
{
if (node->data > max_node->data)
{
max_node = node;
}
node = node->next;
}
return max_node;
}
void swap_node(struct Node* node1, struct Node* node2)
{
int temp;
if (node1 == node2)
{
return;
}
temp = node1->data;
node1->data = node2->data;
node2->data = temp;
}
void sort_list(struct Node* head)
{
struct Node* max_node = NULL;
struct Node* current = NULL;
current = head;
while (current != NULL)
{
max_node = get_max_node(current);
swap_node(current, max_node);
current = current->next;
}
}
int main()
{
struct Node *head = NULL;
int N = 0;
printf("請輸入雙向鏈表的結點個數:");
scanf("%d", &N);
head = create_list(N);
display_list(head);
sort_list(head);
display_list(head);
free_list(head);
return 0;
}
④ c語言數據結構(雙向鏈表排序)
#include<stdio.h>
#include<malloc.h>
#define ElemType int
int count=0;
typedef struct DulNode
{
ElemType data;
DulNode *prior;
DulNode *next;
}DulNode,*DulLinkList;
//初始化鏈表,結束後產生一個頭結點指針
void InitDLList(DulLinkList *L)
{
(*L)=(DulLinkList)malloc(sizeof(DulNode));
(*L)->next=*L;
(*L)->prior=(*L)->next;
}
//對鏈表進行插入操作
void ListInsert(DulLinkList *L)
{
int i=0,n;
ElemType temp;
DulNode *s,*p;
p=(*L)->next;
printf("請輸入插入元素數量:\n");
scanf("%d",&n);
count=n;
printf("請輸入%d個自然數\n",n);
while(i<n)
{
scanf("%d",&temp);
s=(DulNode*)malloc(sizeof(DulNode));
s->data=temp;
while((p!=(*L))&&(p->data<temp))//查找所要插入的位置
{
p=p->next;
}
s->prior=p->prior;//新節點的插入
s->next=p;
p->prior->next=s;
p->prior=s;
p=(*L)->next;//將指針回指到鏈表第一個非空節點,主要是為了下次查找插入位置
i++;
}
}
void Display(DulLinkList L)
{
DulNode *p;
p=L->next;
printf("雙向鏈表中的數據為:\n");
while(p!=L)
{
printf("%d ",p->data);
p=p->next;
}
printf("\n");
}
void Sort(DulLinkList *L)
{
ElemType temp;
DulNode *p,*q;
p=(*L)->next;
q=(*L)->prior;
if(count%2!=0)
q=q->prior;
p=p->next;
while(p!=q)
{
temp=p->data;
p->data=q->data;
q->data=temp;
p=p->next;
if(p!=q) //第二題只需交換節點數據
q=q->prior;//這幾個if else語句需要仔細
else
break;
if(p!=q)
p=p->next;
else
break;
if(p!=q)
q=q->prior;
else
break;
}
}
void main()
{
DulLinkList L;
InitDLList(&L);//初始化鏈表
ListInsert(&L);//順序插入數據
Display(L);//顯示結果
Sort(&L);//第二題操作
Display(L);//第二題輸出結果
}
⑤ C語言雙向鏈表排序
既然是選擇排序,在交換最小節點與當前節點,也就是調用 reverse() 之後,當前節點應該後移一個,所以將 p = i 去掉即可,因為外層 for 循環已經有 p = p->pnext
⑥ C語言雙向鏈表排序
你這個演算法根本就沒有改變節點的相對位置嘛
struct
num
{
double
number;
struct
num*
pnext;
struct
num*
pfront;
};
struct
num*
paixu(struct
num
*head)
{
struct
num
*p,*q,*i,*t;
double
min
tmp;
for(p=head;p->pnext!=head;p=p->pnext)
{
min=p->number;
i=p;
for(q=p->pnext;q!=head;q=q->pnext)
{
if(q->number<=min)
{
min=q->number;
tmp=i->number:
i->number=min;
q->number=tmp;
}
}
if(p!=i)
{
head=reverse(p,i,head);
p=i;
}
}
return
head;
}
我隨便看了看,改了改,你先看看
⑦ C語言 雙向鏈表 快速排序
我說一下想法你看行不行
直接在鏈表中排序太麻煩,不如把關鍵字和指針單獨抽取出來放到一個結構體數組中
然後對這個數組進行排序,排好後按相應指針順序輸出鏈表元素即可
在輸入規模不大的情況下增加了一些空間代價,但是卻可使代碼簡化不少,應該是可行的。
當然直接交換雙向鏈表中的兩個元素也非難事。
⑧ 幫忙用c語言設計一個雙向鏈表的排序
這里給你寫個雙向循環鏈表的各種操作
有什麼其他問題歡迎交流&
#include<stdio.h>
#include<stdlib.h>
//建立雙向循環鏈表
struct d_list{
int data;
struct d_list *prior;
struct d_list *next;
};
void create_d_list(struct d_list **headp,int *p)
{
struct d_list *head=NULL,*tail;
if(p[0]==0)
*headp=NULL;
else{
head=(struct d_list *)malloc(sizeof(struct d_list));
head->data=*p++;
tail=head;
while(*p){
tail->next=(struct d_list *)malloc(sizeof(struct d_list));
tail->next->prior=tail;
tail=tail->next;
tail->data=*p++;
}
tail->next=head;
head->prior=tail;
}
*headp=head;
}
//遍歷雙向循環鏈表
void disp_d_list(struct d_list *p1)
{
struct d_list *p=p1;
while(p!=p1->prior){
printf("%d\t",p->data);
p=p->next;
}
printf("%d\n",p->data);
}
//雙向循環鏈表排序
//升序排序
void sort_d_list(struct d_list **headp1)
{
struct d_list *p,*q;
int i,j,k,n=0;
p=*headp1;
while(p!=(*headp1)->prior){
n++;
p=p->next;
}
for(i=0,p=*headp1;i<n;p=p->next,i++)
for(j=i,q=p->next;j<n;q=q->next,j++)
if((p->data)>(q->data)){
k=q->data;
q->data=p->data;
p->data=k;
}
}
//刪除操作
//只能刪除第一次遇到的x
int delete_d_list(struct d_list **headp,int x)
{
struct d_list *p;
//查找被刪除的結點
p=*headp;
while((p->data)!=x&&(p->next)!=*headp)
p=p->next;
if(p->data==x){//找到被刪除的結點
if(p==*headp){//被刪除的是頭結點
*headp=p->next;
(*headp)->prior=p->prior;
}
else{//被刪除的不是頭結點
p->prior->next=p->next;
p->next->prior=p->prior;
}
free(p);
return 1;
}
else //沒有找到要刪除的結點
return 0;
}
//插入結點
//插入在找到的結點之後
struct d_list *insert_node_next(struct d_list **headp2,int x0)
{
struct d_list *p,*new1;
new1=(struct d_list *)malloc(sizeof(struct d_list));
printf("input the new number\n");
scanf("%d",&new1->data);
getchar();
//查找要插入的結點的位置
p=*headp2;
while(p->data!=x0&&p!=(*headp2)->prior)
p=p->next;
if(p->data==x0){
new1->prior=p;
new1->next=p->next;
p->next->prior=new1;
p->next=new1;
return new1;
}
else
return NULL;
}
//插入在找到的結點之前
struct d_list *insert_node_prior(struct d_list **headp3,int x1)
{
struct d_list *p,*new2;
new2=(struct d_list *)malloc(sizeof(struct d_list));
printf("input the new number\n");
scanf("%d",&new2->data);
getchar();
//查找要插入的結點的位置
p=*headp3;
while(p->data!=x1&&p!=(*headp3)->prior)
p=p->next;
if(p->data==x1){
new2->prior=p->prior;
new2->next=p->prior->next;
p->prior->next=new2;
p->prior=new2;
return new2;
}
else
return NULL;
}
int main(void)
{
struct d_list *head,*p;
int a[]={1,5,3,4,2,7,6,9,8,0},flag;
create_d_list(&head,a);
disp_d_list(head);
printf("Now delete 3....\n");
flag=delete_d_list(&head,3);
if(flag==1)
printf("Delete OK !\n");
else
printf("Can't delete !\n");
printf("after delete 3:\n");
disp_d_list(head);
printf("Now insert after 5....\n");
p=insert_node_next(&head,5);
if(p=NULL)
printf("Insert error !\n");
else
printf("Insert OK !\n");
printf("after insert a new number after 5:\n");
disp_d_list(head);
printf("Now insert before 7....\n");
p=insert_node_prior(&head,7);
if(p=NULL)
printf("Insert error !\n");
else
printf("Insert OK !\n");
printf("after insert a new number before 7:\n");
disp_d_list(head);
printf("Now sort ....\n");
sort_d_list(&head);
printf("Sort OK!\n");
printf("after sort :\n");
disp_d_list(head);
getchar();
getchar();
}