当前位置:首页 » 编程语言 » c语言双向链排名顺序
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

c语言双向链排名顺序

发布时间: 2023-02-05 07:07:52

① 双向链表的排序...(用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();
}