1. 实现一个单链表,链表初始为空,支持三种操作:

1. 向链表头插入一个数; 2. 删除第 k 个插入的数后面的数; 3. 在第 k 个插入的数后插入一个数

#include<iostream>
typedef struct table table;
using namespace std;
struct table{
    int dateint;
    int size;
    int curpos;
    struct table *next;
};
table *create(int n){
    static int pos=1;
    table *k = new table;
    k->dateint = n;
    k->next = NULL;
    k->curpos = pos;
    pos++;
    return k;
}
void insert(table *head){
    int n;
    cin >> n;
    table *j;
    j=create(n);
    j->next = head->next;
    head->next = j;
    head->size++;
}
void delete_num(table *head){
    table *p=head->next;
    table *q;
    int k;
    cin >> k;
    while(p->curpos!=k&&p!=NULL){
        p = p->next;
    }
    q = p->next;
    if(q!=NULL){
        p->next = q->next;
        free(q);
        head->size--;
    }
    
}
void insert_after_k(table *head){
    int k;
    cin >> k;
    table *p = head->next;
    while(p->curpos!=k&&p!=NULL){
        p = p->next;
    }
    insert(p);
    head->size++;
}
void print_h(table *head){
    table *p = head->next;
    while(p!=NULL){
        cout << p->dateint<<" "<<p->curpos<<endl;
        p = p->next;
    }
    cout << "当前链表尺寸" <<head->size << endl;
}
void menu(table *head){
    int choose = 0;
    while(1){
    cout << "请输入你要使用的功能"<<endl;
    cout << "1.向链表头插入一个数;\n2.删除第k个插入的数后面的数;\n3.在第k个插入的数后插入一个数\n4.打印链表元素\n5.退出菜单" << endl;
    cin >> choose;
    switch(choose){
        case 1:{
            insert(head);
            break;
        }
        case 2:{
            delete_num(head);
            break;
        }case 3:{
            insert_after_k(head);
            break;
        }
        case 4:{
            print_h(head);
            break;
        }
        case 5:{
            exit(0);
        }
        default:{
            cout << "输入有误,请重新输入!" << endl;
        }
    }
    }
}

int main(){
    table *head = new table;
    head->next = NULL;
    menu(head);
    return 0;
}

 

 

 

 

 

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值