链表排序-冒泡排序

本文介绍了一种通过链表实现动态数组的方法,解决了C语言中数组大小固定的问题。提供了链表的基本操作如添加、查找、删除节点及排序等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

动态数组的存储和排序

 需求: C不支持动态数组,申明的时候,一定要指明动态数组的大小,不能将数组的大小设置为未知数,但是很多情况 下,数据量的大小是未知数,或者数据量的大小会随着问题的变化而变化。
   解决:使用链表结构来存储数据

// DymicArray.cpp : Defines the entry point for the console application.  
//  
  
#include "stdafx.h"  
// MyList.cpp : Defines the entry point for the console application.  
//  
  
#include "stdafx.h"  
  
#include <iostream>  
#include <cstdio>  
#include <cstdlib>  
#include <cstring>  
  
using namespace std;  
  
  
// data  
  
typedef struct  
{  
    char key[10];  
    char name[20];  
    int age;  
}Data;  
  
typedef struct Node  
{  
    Data nodeData;  
    struct Node *nextNode;  
}CLType;  
  
// AddTail  
CLType *CLAddEnd(CLType *head, Data nodeData)  
{  
    CLType *node,*htemp;  
  
    if(!(node = (CLType*)malloc(sizeof(CLType))))  
    {  
        printf("malloc false!\n");  
        return NULL;  
    }  
    else  
    {  
        node->nodeData = nodeData;  
        node->nextNode = NULL;  
  
        if(head == NULL)  
        {  
            head = node;  
            return head;  
        }  
        htemp = head;  
        while(htemp->nextNode != NULL)  
        {  
            htemp=htemp->nextNode;  
        }  
        htemp->nextNode = node;  
        return head;  
    }  
}  
  
// InsertFirst  
CLType *CLAddFirst(CLType *head, Data nodeData)  
{  
    CLType *node = NULL;   // init  
  
    if(!(node == (CLType *)malloc(sizeof(CLType))))  
    {  
        printf("AddFirst malloc false\n");  
        return NULL;  
    }  
    else  
    {  
        node->nodeData = nodeData;  
        node->nextNode = head;  
        head = node;  
        return head;  
    }  
}  
  
// Find Key  
CLType *CLFindNode(CLType *head, char *key)  
{  
    CLType *htemp;  
    htemp = head;  
    while(htemp)  
    {  
        if(strcmp(htemp->nodeData.key, key) == 0)  
        {  
            return htemp;  
        }  
        htemp = htemp->nextNode;  
    }  
    return NULL;  
}  
  
// Insert Node  
CLType *CLInsertNode(CLType *head, char *findkey, Data nodeData)  
{  
    CLType *node = NULL, *nodetemp = NULL;  
  
     if(!(node = (CLType*)malloc(sizeof(CLType))))  
    {  
        printf("malloc false!\n");  
        return NULL;  
    }  
    node->nodeData = nodeData;  
    nodetemp = CLFindNode(head, findkey);  
  
    if(nodetemp)  
    {  
        node->nextNode = nodetemp->nextNode;  
        nodetemp->nextNode = node;  
    }  
    else  
    {  
        printf("%s Not Found\n", findkey);  
        free(node);  
    }  
    return head;  
}  
  
// DelNode  
int CLDeleteNode(CLType *head, char *key)  
{  
    CLType *node, *htemp;  
  
    htemp = head;  
    node = head;  
  
    while(htemp)  
    {  
        if(strcmp(htemp->nodeData.key, key) == 0)  
        {  
            node->nextNode = htemp->nextNode;  
            free(htemp);  
            return 1;  
        }  
        else  
        {  
            node = htemp;  
            htemp = htemp->nextNode;  
        }  
    }  
    return 0;  
}  
  
// CL length  
int CLLength(CLType *head)  
{  
    CLType *htemp = NULL;  
    int Len = 0;  
    htemp = head;  
  
    while(htemp)  
    {  
        Len++;  
        htemp = htemp->nextNode;  
    }  
    return Len;  
}  
  
// Show ALL  
void CLAllNode(CLType *head)  
{  
    CLType *htemp;  
    Data nodeData;  
    htemp = head;  
    printf("having %d node show All \n", CLLength(head));  
  
    while(htemp)  
    {  
        nodeData = htemp->nodeData;  
        printf("Node(%s,%s,%d)\n", nodeData.key, nodeData.name, nodeData.age);  
        htemp = htemp->nextNode;  
    }  
}  
  
  
// 排序 冒泡排序方法  
void DynamicSort(CLType *head)  
{  
    int i,j;  
    CLType *htemp = NULL;  
    int Len = 0;  
    htemp = head;  
    Data nodedata;  
      
    // 获取链表的长度  
    while(htemp){  
        Len++;  
        htemp = htemp->nextNode;  
    }  
    // 链表表头置为链表头  
    htemp = head;  
  
    // 冒泡排序方法:   
    //             ↓(i)  
    //   ------------------- (Len)  
    //   ↑(j)---->|(Len-i-1)  
    for (i = 0; i < Len-1; i++)  
    {  
        for (j = 0; j < Len - i - 1; j++)  
        {  
            // 按照年龄大小进行比较  
            if (htemp->nodeData.age < htemp->nextNode->nodeData.age)  
            {  
                nodedata.age = htemp->nodeData.age;  
                strcpy(nodedata.key,htemp->nodeData.key);  
                strcpy(nodedata.name, htemp->nodeData.name);  
  
                htemp->nodeData.age = htemp->nextNode->nodeData.age;  
                strcpy(htemp->nodeData.key, htemp->nextNode->nodeData.key);  
                strcpy(htemp->nodeData.name, htemp->nextNode->nodeData.name);  
                  
                htemp->nextNode->nodeData.age = nodedata.age;  
                strcpy(htemp->nextNode->nodeData.name, nodedata.name);  
                strcpy(htemp->nextNode->nodeData.key, nodedata.key);  
            }  
            htemp = htemp->nextNode;  
        }  
        htemp = head;  // 每次比较之后 将临时链表指针移到链表头  
    }  
}  
  
  
int main()  
{  
    CLType *node, *head = NULL;  
    Data nodeData;  
    char key[10], findkey[10];  
  
    cout<<"test CList enter list init data format like: key name age"<<endl;  
  
    do  
    {  
        fflush(stdin);  
        scanf("%s", nodeData.key);  
  
        if(strcmp(nodeData.key, "0") == 0)  
        {  
            break;  
        }  
        else  
        {  
            scanf("%s%d", nodeData.name, &nodeData.age);  
            head = CLAddEnd(head, nodeData);  
        }  
    }while(1);  
  
    CLAllNode(head);  
  
    printf("inset Data, enter insert find key\n");  
    fflush(stdin);  
    //scanf("%s", findkey);  
    cin>>findkey;  
    cout<<"findkey"<<findkey<<endl;  
    cout<<"enter insert data format like: key name age"<<endl;  
    scanf("%s%s%d", nodeData.key, nodeData.name, &nodeData.age);  
    //cin>>nodeData.key>>nodeData.name>>nodeData.age;  
    cout<<nodeData.key<<nodeData.name<<nodeData.age<<endl;  
    head = CLInsertNode(head, findkey, nodeData);  
    CLAllNode(head);  
  
    cout<<"排序: 冒泡方式------"<<endl;  
    DynamicSort(head);  
    CLAllNode(head);  
  
    cout<<"delete enter you want delete key: "<<endl;  
    scanf("%s", key);  
    //cin>>key;  
    CLDeleteNode(head, key);  
    CLAllNode(head);  
  
    cout<<"find enter you want find key:"<<endl;  
    scanf("%s", key);  
    //cin>>key;  
    node = CLFindNode(head, key);  
    if(node)  
    {  
        nodeData = node->nodeData;  
        cout<<"key"<<nodeData.key<<"name"<<nodeData.name<<"age"<<nodeData.age<<endl;  
    }  
    else  
    {  
        cout<<"CList not found"<<endl;  
    }  
  
    return 0;  
}  
  
  
/* 
 
  test CList enter list init data format like: key name age 
  10 tom 11 
  2 lily 5 
  4 lucy 13 
  6 dim 6 
  0 
  having 4 node show All 
  Node(10,tom,11) 
  Node(2,lily,5) 
  Node(4,lucy,13) 
  Node(6,dim,6) 
  inset Data, enter insert find key 
  2 
  findkey2 
  enter insert data format like: key name age 
  7 devid 10 
  7devid10 
  having 5 node show All 
  Node(10,tom,11) 
  Node(2,lily,5) 
  Node(7,devid,10) 
  Node(4,lucy,13) 
  Node(6,dim,6) 
  排序: 冒泡方式------ 
  having 5 node show All 
  Node(4,lucy,13) 
  Node(10,tom,11) 
  Node(7,devid,10) 
  Node(6,dim,6) 
  Node(2,lily,5) 
  delete enter you want delete key: 
  2 
  having 4 node show All 
  Node(4,lucy,13) 
  Node(10,tom,11) 
  Node(7,devid,10) 
  Node(6,dim,6) 
  find enter you want find key: 
  3 
  CList not found 
Press any key to continue 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值