栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

c语言链表操作示例分享

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

c语言链表操作示例分享

复制代码 代码如下:
#include
#include
#include

typedef struct chaink{
    char c;
    struct chaink * next;
    }ck;
ck * chain(ck *,int);
int print(ck *,int);

int main(void){
    printf("这是一个线性链表试验程序。n");
    ck * head=NULL;
    int k;
    k=sizeof(ck);
    do{
        head=chain(head,k);
        printf("是否要结束程序?若结束,请按y/Y;按其他键继续录入。n");
        if(getch()=='y' && getch()=='Y'){
            printf("程序结束。n");
            getch();
            break;
            }       
        }while(1);
    return 0;
    }

ck * chain(ck * head,int k){
    ck * next=NULL;
    ck * temp=NULL;
    int i=0;
    if(head==NULL){
        head=(ck*)malloc(k);
        if(head==NULL){
            printf("malloc内存错误!");
            getch();
            exit(1);
            }
        printf("头结点已成功创建,其地址为%p。n",head);
        head->next=NULL;
        head->c='0';
        }   
    do{
        printf("是否要录入新数据?若录入,请按y/Y;按其他键结束录入。n");
        if(getch()!='y' && getch()!='Y'){
            printf("录入结束。n");
            getch();
            break;
            }            
        temp=head->next;
        head->next=(ck*)malloc(k);
        if(head->next==NULL){
            printf("malloc内存错误!");
            getch();
            exit(1);
            }
        next=head->next;
        next->next=temp;
        printf("请录入新数据……n");
        next->c=getch();
        if(next->c==-1){
            printf("系统录入端错误!");
            getch();
            exit(1);
            }
        printf("新数据录入成功。录入的新数据为%c,其数据节点的地址为%p。n",next->c,next);
    }while(1);
    if(head->next==NULL){
        printf("数据链表里现在没有数据。n");
        getch();   
    }
    else{
        printf("是否要显示链表中所有的数据及其地址?若要显示,请按y/Y;按其他键跳过。n");
        if(getch()!='y' && getch()!='Y'){
            printf("跳过。n");
            getch();
            return head;
            }
        printf("现在输出链表的内容……n 序号 数据 指针n");
        for(next=head->next;next!=NULL;next=next->next){
            i=print(next,i);
            }
    }
    return head;   
    }

int print(ck * next,int i){
    printf("   %d    %c    %pn",i,next->c,next);
    i++;
    return i;
    }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/65830.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号