zoukankan      html  css  js  c++  java
  • 链表:女生节快乐

    这里写图片描述

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

    #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);
            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();
    }

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

  • 相关阅读:
    Appium [安装包] Appium 国内下载地址 (百度云盘,已更新至 AppiumDesktop_1.7.1)(转载)
    PLSQL 触发器
    Java解析Json数据的两种方式
    easyui combobox 动态加载数组数据
    js控制easyui datagrid列的显示和隐藏
    Js中for循环的阻塞机制
    数据库中的视图
    严重:Error configuring application listener of class org.springframework.web.util.IntrospectorCleanupListener
    eclipse背景设置什么颜色缓解眼睛疲劳之一
    Maven详解
  • 原文地址:https://www.cnblogs.com/cww97/p/7534020.html
Copyright © 2011-2022 走看看