码迷,mamicode.com
首页 > 其他好文 > 详细

有序链表和顺序表

时间:2016-10-16 19:12:04      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:

  通过捕捉键盘动作来进行交互,头文件为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]********************|\n");
    printf("|                                                        |\n");
    printf("|------------链表操作请按   “1” -----------------------|\n");
    printf("|------------顺序表操作请按 “2” -----------------------|\n");
    printf("|------------退出程序请按   “ESC”----------------------|\n");
    printf("|                                                        |\n");
    printf("|********************************************************|\n");
}
void List_GUI() {
    printf("|**********************链 表******************************|\n");
    printf("|                                                         |\n");
    printf("|------------新建链表请按   “1” ------------------------|\n");
    printf("|------------插入元素请按   “2” ------------------------|\n");
    printf("|------------删除元素请按   “3” ------------------------|\n");
    printf("|------------查找元素请按   “4” ------------------------|\n");
    printf("|------------合并链表请按   “5” ------------------------|\n");
    printf("|------------显示链表请按   “6” ------------------------|\n");
    printf("|------------销毁链表请按   “7” ------------------------|\n");
    printf("|------------返回上一级按   “ESC” ----------------------|\n");
    printf("|                                                         |\n");
    printf("|*********************************************************|\n");
}
void BackList_GUI() {
    printf("按“ESC”返回上一层\n");
    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("当前链表为空\n");
        return;
    }
    printf("链表元素如下:\n");
    for(List*p = L; p != NULL; p = p->next) {
        printf("%d ",p->num);
    }
    printf("\n");
}
void Destroy(List* &L) { ///销毁一个链表
    if(L == NULL) {
        printf("当前无链表\n");
        return;
    }
    List* Back;
    for(List* p =L; p!=NULL; p=Back) {
        Back = p->next;
        free(p);
    }
    L = NULL;
    printf("销毁操作完成\n");
}
void Union(List* &L1,List* L2) { ///合并链表
    for(List*p = L2; p != NULL; p = p->next) {
        Insert_List(L1,p->num);
    }
    printf("合并操作完成\n");
}
void Init_List(List* &L) { ///新建链表
    int n,num;
    L = NULL;
    printf("请输入链表的初始长度\n");
    scanf("%d",&n);
    printf("请输入这 %d 个元素\n",n);
    for(int i = 0; i < n; i++) {
        scanf("%d",&num);
        Insert_List(L,num);
    }
    printf("新建链表完毕\n");
}
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("当前没有链表,请先建立链表\n");
            } else {
                printf("请输入要插入的数值\n");
                scanf("%d",&num);
                Insert_List(La,num);
                printf("元素%d已经被插入成功\n",num);
            }
            BackList_GUI();
        } else if(key == 51) { ///3 删除
            system("cls");
            if(La == NULL) {
                printf("当前无链表\n");
                BackList_GUI();
                continue;
            }
            printf("请输入要删除的数值\n");
            scanf("%d",&num);
            int ok = Delete_Num(La,num);
            if(ok) printf("%d已经被删除\n",num);
            else printf("删除失败,链表里没有这里元素\n");
            BackList_GUI();
        } else if(key == 52) { ///4 查找
            system("cls");
            if(La != NULL) {
                printf("请输入要查找的数值\n");
                scanf("%d",&num);
                int id = Find_elem(La,num);
                if(id == 0) printf("元素不存在\n");
                else printf("元素第一次出现在链表的第%d个位置\n",id);
            } else printf("当前无链表\n");
            BackList_GUI();
        } else if(key == 53) { ///5 合并
            system("cls");
            if(La == NULL) {
                printf("当前没有初始链表,请先新建\n");
                BackList_GUI();
                continue;
            } else {
                printf("请新建一个新的链表与先前链表合并\n");
                Init_List(Lb);
                printf("新链表建立完成,1秒后开始合并链表\n");
                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("|**********************顺序表****************************|\n");
    printf("|                                                        |\n");
    printf("|------------新建顺序表请按 “1” -----------------------|\n");
    printf("|------------插入元素请按   “2” -----------------------|\n");
    printf("|------------删除元素请按   “3” -----------------------|\n");
    printf("|------------查找元素请按   “4” -----------------------|\n");
    printf("|------------显示顺序表请按 “5” -----------------------|\n");
    printf("|------------销毁顺序表请按 “6” -----------------------|\n");
    printf("|------------返回上一级按   “ESC” ---------------------|\n");
    printf("|                                                        |\n");
    printf("|********************************************************|\n");
}
void BackSq_GUI() {
    printf("按“ESC”返回上一层\n");
    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("请输入顺序表的初始元素数目\n");
    int n,num;
    scanf("%d",&n);
    printf("请输入这%d个元素\n",n);
    for(int i = 0; i < n; i++) {
        scanf("%d",&num);
        Insert_Sq(Sq,num);
    }
    printf("顺序表新建完成!\n");
}
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("当前无顺序表\n");
        return;
    }
    printf("顺序表元素如下:\n");
    for(int i = 0; i < Sq.length; i++) {
        printf("%d ",Sq.data[i]);
    }
    printf("\n");
}
void Destroy_Sq(Squence &Sq) {
    if(Sq.length == 0) {
        printf("当前无顺序表\n");
        return;
    }
    Sq.length = 0;
    printf("顺序表已经被销毁\n");
}
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("当前没有顺序表,请先建立顺序表\n");
            } else {
                printf("请输入要插入的数值\n");
                scanf("%d",&num);
                Insert_Sq(Sq,num);
                printf("元素%d已经被插入成功\n",num);
            }
            BackSq_GUI();
        } else if(key == 51) { ///3 删除
            system("cls");
            if(Sq.length == 0) {
                printf("当前无顺序表\n");
            } else {
                printf("请输入要删除的数值\n");
                scanf("%d",&num);
                int ok = Delete_Num(Sq,num);
                if(ok) printf("%d已经被删除\n",num);
                else printf("删除失败,顺序表里没有这里元素\n");
            }
            BackSq_GUI();
        } else if(key == 52) { ///4 查找
            system("cls");
            if(Sq.length != 0) {
                printf("请输入要查找的数值\n");
                scanf("%d",&num);
                int id = Find_elem(Sq,num);
                if(id == 0) printf("元素不存在\n");
                else printf("元素第一次出现在顺序表的第%d个位置\n",id);
            } else printf("当前无顺序表\n");
            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("程序结束,感谢使用!\n");
            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

 

有序链表和顺序表

标签:

原文地址:http://www.cnblogs.com/jifahu/p/5967004.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!