一个比较(非常)简单的双向链表...

如题,给初学者们的一个例子…

声明:
·自己编写的例子,纯原创
·200行,其实不算长
·版权没有(开玩笑的),欢迎下载自己修改

作为自己(一个蒻驹)两个小时的成果,我只能说自己还是……比较膨胀的,这点东西权当参考吧……


部分函数

dele()

删除单个节点

exi()

删除所有节点

insert()

插入单个节点

input_data()

输入所有数据

sortBTS()

冒泡排序法,降序

watch (int ret)

查看当前链表的所有元素,并且,ret作为下一步是否返回菜单的标志,决定暂停功能的实现


特别提醒

·······system(“pause”) ———–意为使用DOS命令行”pause”———–作用为暂停
·······system(“cls”) —————意为使用DOS命令行”cls”—————作用为清除屏幕
·······使用了goto语句,好坏不作评论。


效果图(部分)

主菜单如下

insert界面如下


实现代码

#include<stdio.h>
#include<stdlib.h>
//全局变量 
struct array {
        struct array *pre,*next;
        long dat; 
        }; 
struct array *head,*tail,*now;
long num;

int main()
{
    int getch();
    int watch(),insert(),dele(),sortBTS(),exi();
    int make_head(),make_array(),input_data();
re: system("cls");
    printf("输入数据个数:\n"); 
    scanf("%ld",&num);
    while (num<1) {
        printf("输入错误!重新输入:\n");
        scanf("%ld",&num);
    }
    make_head();        //建立数组头 
    make_array();       //开辟数组单元 
    input_data();       //输入数据 
    while (1) {         //控制总界面 
        system("cls");
        printf("按键操作:\n"); 
        printf("1.查看数组元素\n2.在选定元素后方插入元素\n3.删除选定元素\n");
        printf("4.对数组从大到小排序\n5.清除并重建数组\n按ESC清除数组并退出...\n"); 
        switch (getch()) {
            case '1':watch(1);break;                    //查看数组元素 
            case '2':insert();break;                    //在选定元素后方插入新元素 
            case '3':dele();break;                      //删除选定元素 
            case '4':sortBTS();break;                   //对数组由大到小排序 
            case '5':exi();goto re;break;               //清除并重建数组               
            case '\x1b':exi();return 0;                 //清除数组并退出程序 
        }   
    }
    return 0;
}

int make_head() 
{   
    int exi();
    tail=NULL;
    if ((head=malloc(sizeof(struct array)))==NULL) {  
        printf("建立首元素错误!需关闭程序...\n");
        system("pause");
        exi();
        exit(0); 
    }
    head->next=NULL;
    head->pre=NULL;
    return 0;
}

int make_array()
{   
    long i;
    int exi();
    tail=head;  
    for (i=1;i<num;i++) {
        if ((tail->next=malloc(sizeof(struct array)))==NULL) {  
            printf("建立数组错误!需关闭程序...\n");
            system("pause");
            exi();
            exit(0); 
        }
        tail->next->pre=tail;
        tail=tail->next;
        tail->next=NULL;
    }
    return 0;
}

int input_data()
{
    printf("输入数据(long int型):\n"); 
    now=head;  
    while (now->next!=NULL) { 
        scanf("%ld",&now->dat);
        now=now->next;
    }
    if (num!=0) scanf("%ld",&now->dat);
    now=head; 
    return 0;
}

int watch(int ret)  //ret=0 -不返回菜单;ret=1 -返回菜单 
{
    struct array *k;
    int i=0;
    system("cls");
    printf("共%ld个数据...\n",num); 
    k=head;
    while (k->next!=NULL) { 
        i++;
        printf("%ld\t\t",k->dat);
        if (!(i%5)) putchar('\n'); 
        k=k->next;
    }
    if (num!=0) printf("%ld\t\t",k->dat);
    if (ret) {
        printf("\n按任意键返回菜单...");
        getch();
    }
    return 0;
}

int insert()
{
    long k,i;
    int watch(),exi();
    struct array *s;
re: watch(0);   
    printf("\n选择待插入元素的前一个元素的位置(0到%ld):\n",num);
    scanf("%ld",&k);
    while ((k>num)||(k<0)) {   //报错 
        printf("输入错误!重新输入:\n"); 
        scanf("%ld",&k);
    }
    now=head;
    for (i=2;i<=k;i++) now=now->next;
    if ((s=malloc(sizeof(struct array)))==NULL) 
        printf("新建元素错误!按1重试,按其他键退出...\n");
    if  (s==NULL) 
        if (getchar()=='1') goto  re;
        else {
                exi();
                exit(0);
             }
    if (k==0) {
        s->next=head;
        s->pre=NULL;
        head->pre=s;
        head=head->pre;
    }
    else if (now==tail) {
            tail->next=s;
            s->pre=tail;
            tail=s;
            tail->next=NULL;
        }
        else {
            s->next=now->next;
            s->next->pre=s;
            now->next=s;
            s->pre=now;
        }
    printf("输入插入的元素数据:\n");
    scanf("%ld",&s->dat);
    num++;
    watch(1);
    return 0;
}

int dele()
{
    long k,i;
    int watch();
    watch(0);
    now=head; 
    printf("\n输入待删除元素的位置:(1到%ld)\n",num);
    scanf("%d",&k);
    while ((k>num)||(k<1)) {   //报错 
        printf("输入错误!重新输入:\n"); 
        scanf("%d",&k);
    }
    for (i=2;i<=k;i++) now=now->next;
    if (now==head) {
        head=head->next;
        free(head->pre);
        head->pre=NULL; 
    }
    else if (now==tail) {
            tail=tail->pre;
            free(tail->next);
            tail->next=NULL;
         }
         else {
             now->next->pre=now->pre;
             now->pre->next=now->next;
             free(now);
         }
    num--;
    now=head;
    watch(1);
    return 0;
}

int sortBTS()
{
    struct array *i,*j;
    long k; 
    for (i=head;i!=tail;i=i->next)
        for (j=i->next;j!=NULL;j=j->next)
            if (j->dat>i->dat) {
                k=i->dat;
                i->dat=j->dat;
                j->dat=k;
            }
    watch(1);
    return 0;
}

int exi()
{
    while (head->next!=NULL) {
        tail=tail->pre;
        free(tail->next);
        tail->next=NULL;
    }
    free(tail);
    tail=head=NULL;
    return 0;
}

—————————————————————

The blog was edited
by 陈阳
—————————————————————UNAA,2017,12,2

转载请注明出处

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值