C写链表与线性表的创建插入与删除

链表

#include <bits/stdc++.h>
typedef struct node {
    int data;
    struct node *next;
} Lnode;
Lnode *L;
void dele(int pos){
    Lnode *p;
    p=L;
    for(int i=1; i<pos-1; i++){
        p=p->next;
    }
    p->next=p->next->next;
}
void insert(int pos,int val){
    Lnode *p;
    p=L;
    for(int i=1; i<pos-1; i++){
        p=p->next;
    }
    Lnode *q;
    q=(Lnode *) malloc(sizeof(Lnode));;
    q->data=val;
    q->next=p->next;
    p->next=q;
}
int main() {
    L = (Lnode *) malloc(sizeof(Lnode));
    L->data=1;
    L->next = NULL;
    Lnode *p, *temp;
    p = (Lnode *) malloc(sizeof(Lnode));
    p = L;
    for (int i = 1; i <= 10; i++) {
        temp = (Lnode *) malloc(sizeof(Lnode));
        temp->data = i+1;
        temp->next = NULL;
        p->next = temp;
        p = temp;
    }
    int len=10;
    while(1){
        char ch;
        printf("若您要插入则输入Y/y,若删除则输入N/n,若要输出则输入P/p,若要结束程序则输入0\n");
        scanf(" %c",&ch);
        if(ch=='0'){
            break;
        }
        if(ch=='Y' || ch=='y'){
            int pos,val;
            printf("请输入插入的位置与数值: \n");
            scanf("%d %d",&pos,&val);
            while(pos>len || pos<0){
                printf("输入不合法,请重新输入合法的值\n");
                scanf("%d %d",&pos,&val);
            }
            insert(pos,val);
            len++;
        }
        else if(ch=='N' || ch=='n'){
            int pos;
            printf("请输入删除的位置: \n");
            scanf("%d",&pos);
            while(pos>len || pos<0){
                printf("输入不合法,请重新输入合法的值\n");
                scanf("%d",&pos);
            }
            dele(pos);
            len--;
        }
        else if(ch=='P' || ch=='p'){
            p=L;
            while (p->next != NULL) {
                printf("%d  ", p->data);
                p=p->next;
            }
            printf("\n");
        }
    }
    printf("最终链表存储结果: ");
    while (L->next != NULL) {
        printf("%d  ", L->data);
        L=L->next;
    }
    printf("\n");
    return 0;
}

线性表

#include <bits/stdc++.h>

using namespace std;
struct node {
    int *list;
    int length;
    int listsize;
} L;

void init(int q) {
    L.list = (int *) malloc(q * sizeof(int));
    L.length = 0;
    L.listsize = q;
}

void insert(int val, int pos) {
    for (int i = L.length; i >= pos; i--) {
        L.list[i] = L.list[i - 1];
    }
    L.list[pos - 1] = val;
    L.length++;
}

void dele(int pos) {
    for (int i = pos - 1; i < L.length - 1; i++) {
        L.list[i] = L.list[i + 1];
    }
    L.length--;
}
void print(){
    for(int i=0; i<L.length; i++){
        if(i==0 || (i%9==1 && i!=1)){
            printf("%d",L.list[i]);
        }
        else if(i==L.length-1 || i%9==0){
            printf("  %d\n",L.list[i]);
        }
        else  printf("  %d",L.list[i]);
    }
}
int main() {
    int maxn;
    printf("请输入您想定义的线性表长度: \n");
    scanf("%d", &maxn);
    init(maxn);
    for(int i=0; i<maxn/2; i++){
        L.list[i]=i+1;
        L.length++;
    }
    while (1) {
        printf("若您要进行插入操作请键入(Y/y),若删除则键入(N/n),若要查看线性表存储元素则键入(P/p),若要结束程序,请键入exit\n");
        char str[10];
        scanf("%s",str);
        if (strlen(str) == 1 && (str[0] == 'y' || str[0] == 'Y')) {
            if(L.length==maxn){
                printf("此线性表已满,不可进行插入操作\n");
                continue;
            }
            printf("请键入您想插入的权值及位置\n");
            int val, pos;
            printf("权值: ");
            scanf("%d", &val);
            printf("\n位置: ");
            scanf("%d", &pos);
            printf("\n");
            while (pos > L.length) {
                printf("此位置超出现有线性表长度,请重新输入插入位置: ");
                printf("提示:现有线性表长度为%d\n",L.length);
                scanf("%d", &pos);
                printf("\n");
            }
            while(pos<0){
                printf("此位置不合法,请重新输入插入位置: ");
                scanf("%d",&pos);
                printf("\n");
            }
            insert(val, pos);
        } else if (str[0] == 'n' || str[0] == 'N') {
            if(L.length==0){
                printf("此线性表为空,不可进行删除操作\n");
                continue;
            }
            printf("请键入您要删除的位置: ");
            int pos;
            scanf("%d", &pos);
            printf("\n");
            while (pos > L.length) {
                printf("此位置超出现有线性表长度,请重新输入删除位置: ");
                printf("提示:现有线性表长度为%d\n",L.length);
                scanf("%d", &pos);
                printf("\n");
            }
            while(pos<0){
                printf("此位置不合法,请重新输入插入位置: ");
                scanf("%d",&pos);
                printf("\n");
            }
            dele(pos);
        }
        else if(str[0]=='p' || str[0]=='P'){
            print();
        }
        else if(strcmp(str,"exit")==0) break;
        else{
            printf("请键入有效信息!\n");
        }
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值