请问有关单向链表排序的问题

darrencenzzz 2012-01-21 05:35:39
最近在完成一个学生管理系统的程序,用的是链表,还差排序这一块就完成了,想用的是冒泡排序,但在交换节点时遇到了困难,难以实现,所以在这里求助,谢谢,希望能得到一个代码的示范
...全文
80 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
alang512 2012-02-11
  • 打赏
  • 举报
回复
其实 就是 int类型 排序一样简单。
比如链表结点里有 学号: int 类型
那么 就是比较学号的大小进行排序。
把结点里之间的学号进行排序。
newpuple 2012-02-10
  • 打赏
  • 举报
回复
后面再添加一段对尾的处理

if (p->data > tail->data)
{
tail->next = p;
p->next = NULL;
tail = p;
}
newpuple 2012-02-10
  • 打赏
  • 举报
回复
//升序排序代码段
while (p != NULL)
{
if (p->data < currentPos->data)
{
if (currentPos == head)
{head = p; p->next = currentPos; break;}
p->next = currentPos->next;
currentPos->next = p;
break;
}
p = p->next;
}
程序员小迷 2012-02-10
  • 打赏
  • 举报
回复
这里没那么复杂啊
如果元素都是int, 用冒泡排序不是很简单么
这里是链表,用链表对应形式代替int的操作方式不就行了
尘缘udbwcso 2012-02-10
  • 打赏
  • 举报
回复

#include <stdio.h>
#include <malloc.h>

typedef int ElemType;
typedef struct node
{
ElemType data;
struct node *next;
}linkNode, *linklist;

/*
*功能:初始化链表
*返回值:链表首地址
*/
linklist initList()
{
linklist head;
head = (linklist)malloc(sizeof(linkNode));
if(head == NULL)
return NULL;
head->next = NULL;
return head;
}


/*
*功能:求链表长度(头结点不计算)
*参数:链表首地址
*返回值:链表结点数
*/
int length(linklist head)
{
int len = 0;
if(head == NULL)
return 0;
head = head->next;
while(head != NULL)
{
++len;
head = head->next;
}
return len;
}


/*
*功能:判断两个结点的数据大小
*参数:两个结点的地址
*返回值:firstNode中数据大于secondNode中数据返回1
*firstNode中数据等于secondNode中数据返回0
*firstNode中数据小于secondNode中数据返回-1
*传递参数有问题时返回-2
*/
int nodeCompare(linklist firstNode, linklist secondNode)
{
if(firstNode->data > secondNode->data)
return 1;
else if(firstNode->data == secondNode->data)
return 0;
else if(firstNode->data < secondNode->data)
return -1;
return -2;
}

/*
*功能:申请空间
*参数:结点(数据)
*返回值:指向结点的指针
*/
linklist makeNode(linkNode nodeData)
{
linklist newNode;
newNode = (linklist)malloc(sizeof(linkNode));
if(newNode == NULL)
return NULL;
newNode->data = nodeData.data;
return newNode;
}
/*
*功能:输出链表数据
*参数:链表首地址
*/
void printList(linklist head)
{
if(head == NULL || head->next == NULL)
return;
head = head->next;
printf("\nlinklist:\n");
while(head != NULL)
{
printf("%d ", head->data);
head = head->next;
}
printf("\n");

}
/*
*功能:链表排序(带头结点)
*参数:链表首地址
*
*/
void listSort(linklist head)
{
linklist pre, mid, tai;
int i, j;
int len = length(head);
if(head == NULL || head->next == NULL)
return;
for(i = 0; i < len - 1; ++i)
{
pre = head;
mid = head->next;
tai = mid->next;
for(j = 0; j < len - i - 1; ++j)
{
if(nodeCompare(mid, tai) == 1)
{
pre->next = mid->next;
mid->next = tai->next;
tai->next = mid;
}
pre = pre->next;
mid = pre->next;
tai = mid->next;
}
}
}
/*
*功能:在链表尾部插入结点
*参数:链表首地址,待插入结点地址
*/
void pushBack(linklist head, linklist insertNode)
{
if(head == NULL)
return;
while(head->next != NULL)
{
head = head->next;
}
head->next = insertNode;
insertNode->next = NULL;
}

int main()
{
linklist list, insertNode;
linkNode newNode;
int i;
list = initList();

for(i = 0; i < 10; ++i)
{
newNode.data = 10 - i;
insertNode = makeNode(newNode);
pushBack(list, insertNode);
}

printList(list);
listSort(list);
printList(list);
return 0;
}
W170532934 2012-02-10
  • 打赏
  • 举报
回复
链表用简单插入排序就可以了。
ZSP95 2012-02-10
  • 打赏
  • 举报
回复


你自己顶一个节点类型 struct ITEMPTR
{
unsigned long* pDataPtr; // 该指针指向存放于本节点的数据的地址
ITEMPTR* pPreNode; // 链表中上一个节点的位置指针
ITEMPTR* pNextNode; // 链表中下一个节点的位置指针
POSITION pPosition; // 本节点在链表中的位置
ITEMPTR()
{
pDataPtr = NULL;
pPreNode = NULL;
pNextNode = NULL;
pPosition = NULL;
}
};

然后使用二楼的code就行

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧