简单链表

#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 :          ");
     printf("   1,creat_list                  ");
     printf("   2,input data for list         ");
     printf("   3,show all data you hava input ");
     printf("   4,delte                       ");
     printf("   5,exit                        ");
     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;
  }

原文地址:https://www.cnblogs.com/yusuph/p/13696871.html