简单链表

#include <iostream>
#include <stdlib.h>
using namespace std;
struct node{
  int data;
  struct node *link;
};
typedef struct node *plist;
plist creat_list(int m){
  plist head=(plist)malloc(sizeof(plist));
  plist next=(plist)malloc(sizeof(plist));
  head->link=next;
  while(m--){
    plist p=(plist)malloc(sizeof(plist));
    next->link=p;
    next=next->link;
    next->link=NULL;
  }
  cout<<"creat successful"<<endl;
  return head;
}
void show_data(plist p){
  int i=1;
  while(p->link!=NULL){
    cout<<"output:"<<i<<' '<<p->data<<endl;
    p=p->link;i++;
  }
}
void cin_data(plist p){
  while(p->link!=NULL){
    cin>>p->data;
    p=p->link;
    cout<<"succeful"<<endl;
  }
}
void del_data(plist p,int m){
    while(p->link->data!=m&&p->link!=NULL)p=p->link;
    p->link=p->link->link;
    cout<<"successful!"<<endl;
}

int main()
  {

    plist head;
    bool flag=true;
   while(flag){
     printf("choose you want to do :          \n");
     printf("   1,creat_list                  \n");
     printf("   2,input data for list         \n");
     printf("   3,show all data you hava input\n");
     printf("   4,delte                       \n");
     printf("   5,exit                        \n");
     int n;
     cin>>n;
     switch(n){
       case 1:
          cout<<"how mach ?"<<endl;
          int m;cin>>m;
          head=creat_list(m);
          break;
       case 2:
          cin_data(head);
          cout<<"ddd";
          break;
      case 3:
         show_data(head);
         break;
      case 4:
         int l;
         cout<<"Enter what you want to delete"<<endl;
         cin>>l;
         del_data(head,l);
         break;
      case 5:
         flag=false;

    }
   }

    return 0;
  }

posted @ 2020-09-19 16:58  嘻嘻嘻ziixi  阅读(117)  评论(0编辑  收藏  举报