#include<string.h>
#include<iostream.h>
#include<stdio.h>
#include<stdlib.h>
//链表结构
struct wq
{
wq(){x=9999;}
int x;
//char name[10]; //复杂结构的遍历
wq* link;
};
int num=0;//链表的个数
//bool real[100];//是否有长串
//菜单函数
void Tool();
//创建堆栈结构
void creatlink(wq*head);
//遍历链表
void getout(wq* head);
//插入元素
void insertI(wq* head );
//删除元素
void dxy(wq* head );
//释放堆栈
void deleall(wq* head);
//查找函数
void findme(wq* me);
void main()
{
bool re = true; int can=0;
wq * me=new wq;
me->link=NULL;
while (re)
{
int w;
Tool();
cout<<"give me a number!"<<endl;
cin>>w;
switch (w)
{
case 1:
if(can==1)
{
char m;
cout<<"you will destroy the frist one! y/n"<<endl;
cin>>m;
if(m=='y'||m=='Y')
{
can = 0;
deleall(me);
}
}
creatlink(me);can=1;
break;
case 2:
getout(me);
break;
case 3:
dxy(me);
break;
case 4:
insertI(me);
break;
case 5:
findme(me);
break;
case 6:
deleall(me);
break;
case 7:
re = false;
break;
}
}
}
//创建堆栈wq *n1=new wq;
void creatlink(wq*head)
{
bool yen=1;
while(yen)
{ //char m;
int s;
//real[num]=false;
head->link=new wq;
cout<<"请输入堆栈的数据"<<endl;
cin>>head->link->x;
//cout<<"是否输入字符长串?y/n ";
//cin>>m; cout<<" ";
//if(m=='Y'||m=='y')
// {
// real[num]=1;flushall();
// gets(head->link->name );
// cout<<" end ";
// }
head =head->link ;
head->link = NULL;
num++;
cout<<"是否继续?1=继续,0=退出! ";
cin>>s;
if(s==0) yen=0;
}
}
//遍历链表
void getout(wq* head)
{
wq* temp;
if(num==0)
{
cout<<"no"<<endl;
}else
{
for(int i= 0;i<10;i++)
cout<<"-";
cout<<endl;
while(head)
{
int i=0;
temp=head;
head=head->link;
cout<<"堆栈的元素是:"<<endl;
cout<<temp->x<<endl;
//if(real[i])
// {//??????
// for(int i= 0;i<10;i++)
// cout<<temp->name[i];
// }
cout<<endl;i++;
}
}
}
//插入元素
void insertI(wq* head)
{
int w=0,wo=0;
cout<<"输入要插入的位置!"<<endl;
cin>>w;
cout<<"插入的值?"<<endl;
cin>>wo;
if(w>num) {cout<<"the all is:"<<num<<endl<<"越界"<<endl;return;}
int op=0;
while(head)
{
head=head->link;
op++;
if(op==w-1)
{
wq * me;
me = head->link;
head->link = new wq;
head->link->x=wo;
/*cout<<"输入长串? y/n"<<endl;*/
head->link->link=me;
}
}
}
//删除元素
void dxy(wq* head)
{
int ip=0,w=0;
cout<<"输入要删除第几个元素?"<<endl;
cin>>w;
if(w>num)
{
cout<<"链表元数的个数"<<num<<endl;
return;
}
while(head)
{
if(ip==w-1) {
wq * me;me=head->link;
head->link =head->link->link;
free(me ); num--;return;} head=head->link;ip++;
}
}
//释放堆栈
void deleall(wq* head)
{
wq* tem;num=0;
while(head)
{
tem=head;
head=head->link;
free(tem);
}
cout<<"释放完毕!"<<endl;
}
// 菜单函数
void Tool()
{
cout<<"*****************************"<<endl;
cout<<" 1.creat"<<endl;
cout<<" 2.cout"<<endl;
cout<<" 3.delete"<<endl;
cout<<" 4.insert"<<endl;
cout<<" 5.find"<<endl;
cout<<" 6.free"<<endl;
cout<<" 7.out"<<endl;
cout<<"****************************"<<endl;
}
//查找函数
void findme(wq* me)
{
int who;
cout<<"输入要查找的元素?"<<endl;
cin>>who;
while(me)
{
me=me->link;
if(me->x==who)
{
cout<<"find!"<<endl;return;
}
}
}
/*
void put(int q,char p)
{
while(q)
{
cout<<p;q--;
}
}
void Tool()
{
// 第一行
put(3,' ');put(17,'=');cout<<endl;
// 第二行
put(3,' ');put(2,'|');put(4,' ');cout<<"1.creat";put(2,' ');put(2,'|');cout<<endl;
put(3,' ');put(2,'|');put(4,' ');cout<<"2.cout";put(3,' ');put(2,'|');cout<<endl;
put(3,' ');put(2,'|');put(4,' ');cout<<"3.delete";put(1,' ');put(2,'|');cout<<endl;
put(3,' ');put(2,'|');put(4,' ');cout<<"4.insert";put(1,' ');put(2,'|');cout<<endl;
put(3,' ');put(2,'|');put(4,' ');cout<<"5.free";put(3,' ');put(2,'|');cout<<endl;
put(3,' ');put(2,'|');put(4,' ');cout<<"6.out";put(4,' ');put(2,'|');cout<<endl;
put(3,' ');put(17,'=');cout<<endl;
}
// 接受输入
void dothings(wq* ty)
{
bool rel=1; char yi,kl;int e,w;
cout<<"put a char tell me ";cin>>yi;
while(rel){
switch (yi)
{
case '1': creatlink(ty);cout<<"end!"<<endl;dothings(ty);break;
case '2': getout(ty);cout<<"end!"<<endl;dothings(ty);break;
case '3':cout<<"输入删除的索引号!";cin>>e;dxy(ty,e);dothings(ty);break;
case'4': cout<<"输入插入的索引号!";cin>>w;cout<<"输入插入的索引号的值!";
cin>>kl;inserI(ty,w,kl);dothings(ty);break;
case'5': deleall(ty);dothings(ty);break;
case'6':rel=0;break;
default:Tool();dothings(ty);break;
}
}
}*/
/* 没有反转链表的函数*/
/* 没有查找函数*/
//遍历链表