数据结构实验单链表

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

typedef int ElmType;
typedef struct Node {
    ElmType data;
    Node* next;
}LNode,*LinkList;
LinkList listA;
void init(LinkList L) {
    L = (LinkList) malloc(sizeof(LNode));
    L->next = NULL;
    return;
}
LinkList create(int n = 0) {// 创建一个长度为n的单链表
    LNode *tail;
    LNode *p;
    LinkList L = (LinkList) malloc(sizeof(LNode));
    L->next = NULL;
    tail = L;
    if(!n) return L;

    printf("请输入 %d 个整数\n",n);
    for(int i = 0;i < n;i ++) {
        p = (LinkList) malloc(sizeof(LNode));
        scanf("%d",&p->data);
        tail->next = p;
        p->next = NULL;
        tail = p;
    }
    return L;
}
void clear(LinkList L) {
    LNode *p = L->next;
    while(p) {
        LNode *t = p;
        p = p->next;
        free(t);
    }
    L->next = NULL;
    printf("链表已清空!\n");
    return;
}
void append(LinkList L,ElmType value) {
    Node* now = L->next;
    while(now->next != NULL) {
        now = now->next;
    }
    Node *p;
    p = (LinkList)malloc(sizeof(LNode));
    now->next = p;
    p->data = value;
    p->next = NULL;
    return;
}

bool insert(LinkList L,int pos,ElmType value) {
    Node *p = L;
    int now = 0;
    while(now + 1 < pos && p->next != NULL) {
        p = p->next;
        now ++;
    }
    if(now + 1 < pos || !p) {
        printf("插入失败\n");
        return false;
    }

    Node *new_node = (LinkList)malloc(sizeof(LNode));
    new_node->data = value;

    new_node->next = p->next;
    p->next = new_node;
    return true;
}
bool erase(LinkList L,int pos) {
    Node *p = L;
    int now = 0;
    while(now + 1 < pos && p) {
        p = p->next;
        now ++;
    }
    if(!p || now + 1 < pos) {
        printf("删除失败\n");
        return false;
    }
    
    LNode *nxt = p->next->next;
    LNode *prev = p;
    p = p->next;
    free(p);
    prev->next = nxt;
    return true;
}

ElmType getElm(LinkList L,int pos) {
    LNode *p = L->next;
    int now = 1;
    while(now < pos && p) {
        p = p->next;
        now ++;
    }
    if(!p || now < pos) {
        printf("查询失败\n");
        return -1;
    }
    return p->data;
}
bool updata(LinkList L,int pos,ElmType value) {
    int now = 1;
    LNode *p = L->next;
    while(p && now < pos) {
        p = p->next;
        now ++;
    }
    if(!p || now < pos) {
        printf("修改失败\n");
        return false;
    }
    p->data = value;
    return true;
}
bool empty(LinkList L) {
    return !(L->next);
}
void show(LinkList L) {
    LNode *p = L->next;
    while(p) {
        printf("%d ",p->data);
        p = p->next;
    }
    puts("");
    return;
}
LinkList merge(LinkList la,LinkList lb) {
    LinkList ans;
    init(ans);
    LNode *tail = ans;
    LNode *p = la->next;
    LNode *q = lb->next;

    while(p && q) {
        if(p->data <= q->data) {
            LNode *t = (LinkList)malloc(sizeof(LNode));
            t->data = p->data;
            tail->next = t;
            t->next = NULL;
            tail = t;
            p = p->next;
        }else {
            LNode *t = (LinkList)malloc(sizeof(LNode));
            t->data = q->data;
            tail->next = t;
            t->next = NULL;
            tail = t;
            q = q->next;
        }
    }

    while(p) {
        LNode *t = (LinkList)malloc(sizeof(LNode));
        t->data = p->data;
        tail->next = t;
        t->next = NULL;
        tail = t;
        p = p->next;
    }
    while(q) {
        LNode *t = (LinkList)malloc(sizeof(LNode));
        t->data = q->data;
        tail->next = t;
        t->next = NULL;
        tail = t;
        q = q->next;
    }
    return ans;
}
int size(LinkList L) {
    LNode *p = L->next;
    int ans = 0;
    while(p) {
        ans ++;
        p = p->next;
    }
    return ans;
}
void options(int x) {
    if(x == 1) {
        printf("请输入链表长度以创建链表: ");
        int n;
        scanf("%d",&n);
        listA = create(n);
        printf("结果如下:\n");
        show(listA);
    }else if(x == 2) {
        printf("请输入要删除元素位置:");
        int pos;
        scanf("%d",&pos);
        erase(listA,pos);
        show(listA);
    }else if(x == 3) {
        printf("您确定要清空链表吗?\n 确定请输入1  取消输入0\n");
        int x;
        scanf("%d",&x);
        if(x) {
            clear(listA);
        }
    }else if(x == 4) {
        printf("请输入要添加元素:");
        ElmType x;
        scanf("%d",&x);
        append(listA,x);
        printf("更新后链表为:\n");
        show(listA);
    }else if(x == 5) {
        printf("请输入要插入元素位置和元素值:");
        int pos;
        ElmType val;
        scanf("%d%d",&pos,&val);
        insert(listA,pos,val);
        printf("结果为:\n");
        show(listA);
    }else if(x == 6) {
        printf("请输入要更改元素位置和元素值:");
        int pos;
        ElmType val;
        scanf("%d%d",&pos,&val);
        updata(listA,pos,val);
        printf("结果为:\n");
        show(listA);
    }else if(x == 7) {
        printf("请输入查询值位置: ");
        int pos;
        scanf("%d",&pos);
        ElmType ans = getElm(listA,pos);
        printf("位置 %d 的元素值为 %d\n",pos,ans);
    }else if(x == 10) {
        LinkList listB;

        printf("注意:归并前请先保证两链表有序!!\n请先创建第二个链表:\n请输入创建链表长度:");
        int n;
        scanf("%d",&n);
        listB = create(n);
        printf("链表B创建完成:\n当前两链表值如下:\n");
        show(listA);
        show(listB);
        printf("归并后答案为:\n");
        LinkList ans = merge(listA,listB);
        show(ans);
    }else if(x == 8) {
        if(empty(listA)) {
            printf("当前链表为空!\n");
        }else printf("当前链表不空,长度为 %d\n",size(listA));
    }else if(x == 9) {
        printf("当前链表长度为 %d\n",size(listA));
    }
}
void showTip() {
    printf("*****************************************\n");
    printf("********1----建立链表********************\n");
    printf("********2----删除链表中指定位置元素**********\n");
    printf("********3----清空整个链表******\n");
    printf("********4----尾部添加一个元素******\n");
    printf("********5----在指定位置插入一个元素****************\n");
    printf("********6----改变某个位置的值************\n");
    printf("********7----查询某个位置的值************\n");
    printf("********8----查询链表是否为空************\n");
    printf("********9----查询链表长度************\n");
    printf("********10----归并有序表************\n");
    printf("*************退出:输入一个负数***********\n");
    printf("*****************************************\n");
}
int main(){
    listA = create();
    while(1) {
        showTip();
        int op;
        scanf("%d",&op);
        if(op < 0) break;
        options(op);
        printf("按任意键继续下一操作\n");
        getchar();
        getchar();
        system("cls");
    }
    return 0;
}
posted @ 2022-03-24 15:39  Mxrurush  阅读(75)  评论(1编辑  收藏  举报