| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 8308 人关注过本帖, 1 人收藏
标题:简单有序链表的创建和查询修改
只看楼主 加入收藏
凉生
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2017-4-5
收藏
得分:0 
只有第一个能运行,下面的都有错误,我用的VC++,加了头文件还是不行
2017-04-05 11:35
wp231957
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:神界
等 级:贵宾
威 望:423
帖 子:13688
专家分:53332
注 册:2012-10-18
收藏
得分:0 
我这是系列的。需要全放在一起的

DO IT YOURSELF !
2017-04-05 13:01
邹特强
Rank: 2
等 级:论坛游民
帖 子:123
专家分:85
注 册:2016-9-21
收藏
得分:0 
回复 12楼 wp231957
有一个错误而已,我把你的整理了一下

............
2017-04-05 13:05
邹特强
Rank: 2
等 级:论坛游民
帖 子:123
专家分:85
注 册:2016-9-21
收藏
得分:10 
回复 楼主 飞机火车
这个是我整理三楼后完整的代码,加了两处,在表情位置
版主是典型的面向过程思想,思路很清晰,值得学习的,代码没有错误,运行正确
#include <stdio.h>
#include <malloc.h>
#include <stdbool.h>//这个应该加上,否则bool将出现错误
#define N 6

typedef struct data
{
     int value;
     struct data* next;
}tdata,*pdata;

pdata linkcre(void)
{
    pdata head,pfirst,psecond;
    int cr[6]={21,3,15,27,11,18};
    pfirst=(pdata)malloc(sizeof(tdata));
    head=pfirst;
    head->value=0;
    psecond=pfirst;
    int i;
    for(i=0;i<N;i++)
    {
        pfirst=(pdata)malloc(sizeof(tdata));
        pfirst->value=cr[i];
        pfirst->next=NULL;
        psecond->next=pfirst;
        psecond=pfirst;
    }
    return head;
}
void prnlist(pdata head)
{
    pdata pfirst=head->next;
    while(pfirst!=NULL)
    {
        printf("%d ",pfirst->value);
        pfirst=pfirst->next;
    }
    printf("\n");
}
void searchlistp(pdata head,int pos)
{
    pdata pfirst=head->next;
    int i=0;
    while(pfirst!=NULL)
    {
        i++;
        if(i==pos) break;
        pfirst=pfirst->next;
    }
    if(i<pos) printf("无此序列号 你输入的数值大概有些大\n");else    printf("%d \n",pfirst->value);
}
void searchlistv(pdata head,int value)
{
    bool flag=false;
    pdata pfirst=head->next;
    while(pfirst!=NULL)
    {
        if(pfirst->value==value)
        {
            flag=true;
            break;
        }
        pfirst=pfirst->next;
    }
    if(!flag) printf("你输入的数值不存在于链表之中\n");else printf("%d \n",pfirst->value);
}
void inslist(pdata head,int pos,int value)
{
    pdata pfirst=head->next;
    int i=0;
    while(pfirst!=NULL && pos>0)
    {
        i++;
        if(i==pos) break;
        pfirst=pfirst->next;
    }
    pdata ptmp;
    if(pos==0) ptmp=head->next; else ptmp=pfirst->next;
    pdata pins=(pdata)malloc(sizeof(tdata));
    if(pos==0) head->next=pins;else pfirst->next=pins;
    pins->value=value;
    pins->next=ptmp;
}
void dellist(pdata head,int pos)
{
    if(pos==0)
    {
        printf("根节点不可删除\n");
        return;
    }
    pdata pfirst=head->next;
    pdata ptmp=pfirst;
    int i=0;
    while(pfirst!=NULL && pos>1)
    {
        i++;
        if(i==pos) break;
        ptmp=pfirst;
        pfirst=pfirst->next;
    }
    if(pos==1) head->next=head->next->next;
    else
    {
      ptmp->next=pfirst->next;
      free(pfirst);
   } //这里是新加的,把删除的部分的内存释放

}

int main(int argc, char* argv[])
{
    pdata  head;
    head=linkcre();
    prnlist(head);
    inslist(head,0,56); //测试头部插入
    prnlist(head);
    inslist(head,5,777);   //测试中间插入
    prnlist(head);
    inslist(head,8,99999);  //测试尾部插入
    prnlist(head);
    dellist(head,1);
    prnlist(head);
    printf("请输入待查找的序号:");
    int pos=0;
    scanf("%d",&pos);
    searchlistp(head,pos);
    printf("请输入待查找的值:");
    int value=0;
    scanf("%d",&value);
    searchlistv(head,value);
    return 0;
}

............
2017-04-05 13:07
wp231957
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:神界
等 级:贵宾
威 望:423
帖 子:13688
专家分:53332
注 册:2012-10-18
收藏
得分:0 
其实还有点小毛病。待我完善后再發

DO IT YOURSELF !
2017-04-05 13:28
快速回复:简单有序链表的创建和查询修改
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015765 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved