首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

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

2012-07-19 
请问有关单向链表排序的问题最近在完成一个学生管理系统的程序,用的是链表,还差排序这一块就完成了,想用的

请问有关单向链表排序的问题
最近在完成一个学生管理系统的程序,用的是链表,还差排序这一块就完成了,想用的是冒泡排序,但在交换节点时遇到了困难,难以实现,所以在这里求助,谢谢,希望能得到一个代码的示范

[解决办法]
//升序排序代码段
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;
}
[解决办法]
 

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

然后使用二楼的code就行

[解决办法]

C/C++ code
#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;} 

热点排行