链表:女生节快乐

这里写图片描述

好久不写指针、、、代码能力大不如从前

#include<cstdio> 
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
const int N=233;
int cnt;
struct node{
    int num;
    struct node *next;
    node(){num=0;next=NULL;}

    node *build(node **head){
        *head=new node;
        puts("Tell me how many numbers");
        puts("you want to put into the linklist");
        int n;scanf("%d",&n);
        printf("give me %d numbers\n",n);
        int arr[N];cnt=n;
        for (int i=1;i<=n;i++)scanf("%d",&arr[i]);
        for (int i=n;i>=1;i--){//倒过来,前插法 
            node *p=new node;
            p->num=arr[i];
            p->next=(*head)->next;
            (*head)->next=p;
        }
        return *head;
    }

    void insert(node *head){
        int k=cnt+2;
        while (k>cnt+1){
            puts("tell me where do you want to insert");
            scanf("%d",&k);
            if (k>cnt)puts("too big,give me a smaller location");
        }
        puts("tell me which number");
        int x;scanf("%d",&x);
        node *p=(node*)malloc(sizeof(node));
        node *q=(node*)malloc(sizeof(node));
        p=head;
        for (int i=1;i<k;i++) p=p->next;
        q->next=p->next;
        q->num=x;
        p->next=q;
        cnt++;
    }

    void deltee(node *head){
        int k=cnt+1;
        while (k>cnt){
            puts("tell me which do you want to delete");
            scanf("%d",&k);
            if (k>cnt)puts("too big,give me a smaller location");
        }
        node *p=(node*)malloc(sizeof(node));
        node *q=(node*)malloc(sizeof(node));
        p=head;
        for (int i=1;i<k;i++) p=p->next;
        q=p->next;//q=即将删除的元素 
        p->next=q->next;
        free(q);
    }

    void print(node* head){
        node* t=head->next;
        while (t!=NULL){
            printf("%d ",t->num);
            t=t->next;
        }
        puts("");
    }
};

int solve(){
    puts("Welcome");
    char st[5];
    struct node *head;
    head=NULL;
    while (scanf("%s",st)&&(st[0]!='q')){
        if (st[0]=='b')head=head->build(&head);//建立 
        if (st[0]=='i')head->insert(head);//插入
        if (st[0]=='d')head->deltee(head);//删除 
        if (st[0]=='s')head->print(head);//显示 
    }
    return 0;
}

int main(){
    return solve();
}

感谢【静静】的指导
这里写图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值