zoukankan      html  css  js  c++  java
  • 有序链表和顺序表

      通过捕捉键盘动作来进行交互,头文件为conio.h,是console input output的缩写,捕捉用户键盘按键的函数为getch(),通过键值码可以找到对应的按键。

    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    #include<conio.h>
    #include<windows.h>
    #define MAX 5000
    struct List {
        int num;
        List* next;
    };
    struct Squence {
        int data[MAX];
        int length;
    };
    void OnGUI() {
        printf("|************链表和顺序表[1504020311]********************|
    ");
        printf("|                                                        |
    ");
        printf("|------------链表操作请按   “1” -----------------------|
    ");
        printf("|------------顺序表操作请按 “2” -----------------------|
    ");
        printf("|------------退出程序请按   “ESC”----------------------|
    ");
        printf("|                                                        |
    ");
        printf("|********************************************************|
    ");
    }
    void List_GUI() {
        printf("|**********************链 表******************************|
    ");
        printf("|                                                         |
    ");
        printf("|------------新建链表请按   “1” ------------------------|
    ");
        printf("|------------插入元素请按   “2” ------------------------|
    ");
        printf("|------------删除元素请按   “3” ------------------------|
    ");
        printf("|------------查找元素请按   “4” ------------------------|
    ");
        printf("|------------合并链表请按   “5” ------------------------|
    ");
        printf("|------------显示链表请按   “6” ------------------------|
    ");
        printf("|------------销毁链表请按   “7” ------------------------|
    ");
        printf("|------------返回上一级按   “ESC” ----------------------|
    ");
        printf("|                                                         |
    ");
        printf("|*********************************************************|
    ");
    }
    void BackList_GUI() {
        printf("按“ESC”返回上一层
    ");
        while(1) {
            if(getch() == 27) {
                system("cls");
                List_GUI();
                return;
            }
        }
    }
    void Insert_List(List* &L,int num) { ///在链表中插入元素,形成有序链表
        List *tmp,*Front,*p;
        tmp = (List*)malloc(sizeof(List));
        tmp->num = num;
        tmp->next = NULL;
        if(L == NULL) {
            L = tmp;
        } else {
            Front = NULL;
            for(p = L; p != NULL; p = p->next) {
                if(p->num >= num) {
                    tmp->next = p;
                    if(Front == NULL) L = tmp;
                    else Front->next = tmp;
                    break;
                }
                Front = p;
            }
            if(p == NULL) Front->next = tmp;
        }
    }
    int Delete_Num(List* &L,int num) { ///删除链表中的某个元素
        int flag = 0;
        List*Front=NULL,*Back;
        for(List* p = L; p != NULL; p = Back) {
            Back = p->next;
            if(p->num == num) {
                flag = 1;
                if(Front == NULL) L = Back;
                else {
                    Front->next = Back;
                }
                free(p);
            } else Front = p;
        }
        return flag;
    }
    int Find_elem(List* &L,int num) { ///查找某个元素是否存在于链表中
        int id = 0;
        for(List*p=L; p!=NULL; p = p->next) {
            id++;
            if(p->num == num) {
                return id;
            }
        }
        return id;
    }
    void Show_List(List* L) { ///展示链表
        if(L == NULL) {
            printf("当前链表为空
    ");
            return;
        }
        printf("链表元素如下:
    ");
        for(List*p = L; p != NULL; p = p->next) {
            printf("%d ",p->num);
        }
        printf("
    ");
    }
    void Destroy(List* &L) { ///销毁一个链表
        if(L == NULL) {
            printf("当前无链表
    ");
            return;
        }
        List* Back;
        for(List* p =L; p!=NULL; p=Back) {
            Back = p->next;
            free(p);
        }
        L = NULL;
        printf("销毁操作完成
    ");
    }
    void Union(List* &L1,List* L2) { ///合并链表
        for(List*p = L2; p != NULL; p = p->next) {
            Insert_List(L1,p->num);
        }
        printf("合并操作完成
    ");
    }
    void Init_List(List* &L) { ///新建链表
        int n,num;
        L = NULL;
        printf("请输入链表的初始长度
    ");
        scanf("%d",&n);
        printf("请输入这 %d 个元素
    ",n);
        for(int i = 0; i < n; i++) {
            scanf("%d",&num);
            Insert_List(L,num);
        }
        printf("新建链表完毕
    ");
    }
    void List_Ope() {
        List* La,*Lb;
        La = Lb = NULL;
        int key,num;
        while(1) {
            key = getch();
            if(key == 27) {
                system("cls");
                return;
            } else if(key == 49) { ///1 新建
                system("cls");
                Init_List(La);
                BackList_GUI();
            } else if(key == 50) { ///2  插入
                system("cls");
                if(La == NULL) {
                    printf("当前没有链表,请先建立链表
    ");
                } else {
                    printf("请输入要插入的数值
    ");
                    scanf("%d",&num);
                    Insert_List(La,num);
                    printf("元素%d已经被插入成功
    ",num);
                }
                BackList_GUI();
            } else if(key == 51) { ///3 删除
                system("cls");
                if(La == NULL) {
                    printf("当前无链表
    ");
                    BackList_GUI();
                    continue;
                }
                printf("请输入要删除的数值
    ");
                scanf("%d",&num);
                int ok = Delete_Num(La,num);
                if(ok) printf("%d已经被删除
    ",num);
                else printf("删除失败,链表里没有这里元素
    ");
                BackList_GUI();
            } else if(key == 52) { ///4 查找
                system("cls");
                if(La != NULL) {
                    printf("请输入要查找的数值
    ");
                    scanf("%d",&num);
                    int id = Find_elem(La,num);
                    if(id == 0) printf("元素不存在
    ");
                    else printf("元素第一次出现在链表的第%d个位置
    ",id);
                } else printf("当前无链表
    ");
                BackList_GUI();
            } else if(key == 53) { ///5 合并
                system("cls");
                if(La == NULL) {
                    printf("当前没有初始链表,请先新建
    ");
                    BackList_GUI();
                    continue;
                } else {
                    printf("请新建一个新的链表与先前链表合并
    ");
                    Init_List(Lb);
                    printf("新链表建立完成,1秒后开始合并链表
    ");
                    Sleep(1000);
                }
                Union(La,Lb);
                BackList_GUI();
            } else if(key == 54) { ///6 显示
                system("cls");
                Show_List(La);
                BackList_GUI();
            } else if(key == 55) { ///7 销毁
                system("cls");
                Destroy(La);
                BackList_GUI();
            }
        }
    }
    void Sequence_GUI() {
        printf("|**********************顺序表****************************|
    ");
        printf("|                                                        |
    ");
        printf("|------------新建顺序表请按 “1” -----------------------|
    ");
        printf("|------------插入元素请按   “2” -----------------------|
    ");
        printf("|------------删除元素请按   “3” -----------------------|
    ");
        printf("|------------查找元素请按   “4” -----------------------|
    ");
        printf("|------------显示顺序表请按 “5” -----------------------|
    ");
        printf("|------------销毁顺序表请按 “6” -----------------------|
    ");
        printf("|------------返回上一级按   “ESC” ---------------------|
    ");
        printf("|                                                        |
    ");
        printf("|********************************************************|
    ");
    }
    void BackSq_GUI() {
        printf("按“ESC”返回上一层
    ");
        while(1) {
            if(getch() == 27) {
                system("cls");
                Sequence_GUI();
                return;
            }
        }
    }
    void Insert_Sq(Squence &Sq,int num) {
        for(int i = 0; i <= Sq.length; i++) {
            if(i == Sq.length) {
                Sq.data[Sq.length] = num;
                break;
            }
            if(Sq.data[i] >= num) {
                for(int j = Sq.length; j > i; j--) {
                    Sq.data[j] = Sq.data[j-1];
                }
                Sq.data[i] = num;
                break;
            }
        }
        Sq.length++;
    }
    void Init_Sq(Squence &Sq) {
        printf("请输入顺序表的初始元素数目
    ");
        int n,num;
        scanf("%d",&n);
        printf("请输入这%d个元素
    ",n);
        for(int i = 0; i < n; i++) {
            scanf("%d",&num);
            Insert_Sq(Sq,num);
        }
        printf("顺序表新建完成!
    ");
    }
    int Delete_Num(Squence &Sq,int num) {
        int flag = 0;
        for(int i = 0; i < Sq.length; i++) {
            if(Sq.data[i] == num) {
                flag = 1;
                for(int j = i; j < Sq.length-1; j++) {
                    Sq.data[j] = Sq.data[j+1];
                }
                i--;
                Sq.length--;
            }
        }
        return flag;
    }
    int Find_elem(Squence &Sq,int num) {
        for(int i = 0; i < Sq.length; i++) {
            if(Sq.data[i] == num) {
                return i+1;
            }
        }
        return 0;
    }
    void Sequence_Show(Squence Sq) {
        if(Sq.length == 0) {
            printf("当前无顺序表
    ");
            return;
        }
        printf("顺序表元素如下:
    ");
        for(int i = 0; i < Sq.length; i++) {
            printf("%d ",Sq.data[i]);
        }
        printf("
    ");
    }
    void Destroy_Sq(Squence &Sq) {
        if(Sq.length == 0) {
            printf("当前无顺序表
    ");
            return;
        }
        Sq.length = 0;
        printf("顺序表已经被销毁
    ");
    }
    void Sequence_Ope() {
        Squence Sq;
        Sq.length = 0;
        int num;
        while(1) {
            int key = getch();
            if(key == 27) {
                system("cls");
                return;
            } else if(key == 49) { ///1 新建
                system("cls");
                Init_Sq(Sq);
                BackSq_GUI();
            } else if(key == 50) { ///2  插入
                system("cls");
                if(Sq.length == 0) {
                    printf("当前没有顺序表,请先建立顺序表
    ");
                } else {
                    printf("请输入要插入的数值
    ");
                    scanf("%d",&num);
                    Insert_Sq(Sq,num);
                    printf("元素%d已经被插入成功
    ",num);
                }
                BackSq_GUI();
            } else if(key == 51) { ///3 删除
                system("cls");
                if(Sq.length == 0) {
                    printf("当前无顺序表
    ");
                } else {
                    printf("请输入要删除的数值
    ");
                    scanf("%d",&num);
                    int ok = Delete_Num(Sq,num);
                    if(ok) printf("%d已经被删除
    ",num);
                    else printf("删除失败,顺序表里没有这里元素
    ");
                }
                BackSq_GUI();
            } else if(key == 52) { ///4 查找
                system("cls");
                if(Sq.length != 0) {
                    printf("请输入要查找的数值
    ");
                    scanf("%d",&num);
                    int id = Find_elem(Sq,num);
                    if(id == 0) printf("元素不存在
    ");
                    else printf("元素第一次出现在顺序表的第%d个位置
    ",id);
                } else printf("当前无顺序表
    ");
                BackSq_GUI();
            } else if(key == 53) { ///显示
                system("cls");
                Sequence_Show(Sq);
                BackSq_GUI();
            } else if(key == 54) { ///销毁
                system("cls");
                Destroy_Sq(Sq);
                BackSq_GUI();
            }
        }
    }
    void Start() {
        int key;
        OnGUI();
        while(1) {
            key = getch();
            if(key == 27) {
                printf("程序结束,感谢使用!
    ");
                return;
            } else if(key == 49) {
                system("cls");
                List_GUI();
                List_Ope();
                OnGUI();
            } else if(key == 50) {
                system("cls");
                Sequence_GUI();
                Sequence_Ope();
                OnGUI();
            }
        }
    }
    int main() {
        Start();
        return 0;
    }
    View Code
  • 相关阅读:
    01Angular开发环境配置
    不再显示广告案例(php操作cookie)
    php操作 cookie
    JPush Android 推送如何区分开发、生产环境
    10 分钟实现一个自己的服务器监控器
    iOS 轻松使用 App 数据统计
    认识本质:黑天鹅、关键时刻与张小龙的产品观
    C# 服务端推送,十步十分钟,从注册到推送成功
    聊天界面-自适应文字
    极光推送的角标问题——让人又爱又恨的小红点
  • 原文地址:https://www.cnblogs.com/jifahu/p/5967004.html
Copyright © 2011-2022 走看看