//程序实现在单链表的某给定字符节点后插入新结点算法,如找不到则查在尾部;
//查找给定结点并删除的算法
//单链表逆转算法
#include "iostream.h"
const MAXLEN=10;
typedef struct _nodetype{
char elem;
struct _nodetype* lp;
} node;
int myinterface()
{
int i;
cout<<endl<<" List Operation"<<endl;
cout<<" 0 --- Create List"<<endl;
cout<<" 1 --- Delete Node"<<endl;
cout<<" 2 --- Insert Node"<<endl;
cout<<" 3 --- Show List"<<endl;
cout<<" 4 --- Reverse List"<<endl;
cout<<" 5 --- Exit"<<endl;
cin>>i;
return i;
}
void insert(node*&head,char a)
{
//向链表头插入新结点
node*p=new node;
p->elem=a;
p->lp=head->lp;
head->lp=p;
p=NULL;
}
void create(node*& head)
{
// cout<<"head"<<head<<endl;
node* p=new node;//创建有空头节点的链表
head=p;
head->lp=NULL;
cout<<"请输入一串字符,并以?结尾"<<endl;
char temp;
while(cin>>temp)
{
if(temp=='?') break;
insert(head,temp);
}
cout<<"You Have Established a List, ASSHOLE"<<endl;
}
void del(node* head,char temp)
{
node *p=head;
node *q=head->lp;
while(q!=NULL && q->elem!=temp)
{
p=q;
q=q->lp;
}
if(q==NULL)
cout<<"The Char you want to kill isn't here. Go somewhere else"<<endl;
else
{
p->lp=q->lp;
delete q;
cout<<"Your enemy is killed, happy?"<<endl;
}
p=NULL;q=NULL;
}
void show(node *&head)
{
node*p=head->lp;
while(p!=NULL)
{
cout<<p->elem<<'\t';
p=p->lp;
}
cout<<endl;
p=NULL;
}
void total(node*head)
{
int num=0;
node *p=head->lp;
while(p!=NULL)
{
num++;
p=p->lp;
}
cout<<"total number is:"<<num;
}
void clear(node *&head)
{
node *p=head->lp;
while(p!=NULL)
{
del(head,p->elem);
p=head->lp;
}
head=NULL;p=NULL;
}
void reverse(node *&head)
{
node*p=new node;
p->lp=NULL;
node*q=head->lp;
while(q!=NULL)
{
insert(p,q->elem);
q=q->lp;
}
clear(head);
head=p;
p=NULL;
}
int main(int argc, char* argv[])
{
node *r=NULL;
int i;
char temp;
while(i=myinterface(),i>=0&&i<10)
{
// cout<<'r'<<r<<endl;
switch(i)
{
case 0:
{
create(r);
break;
}
case 1:
{
char a=NULL;
cout<<"Input the Character you desperately want to elimilate"<<endl;
while(a==NULL)
cin>>a;
del(r,a);
break;
}
case 2:
{
cin>>temp;
insert(r,temp);
break;
}
case 3:
{
show(r);
break;
}
case 4:
{
reverse(r);
break;
}
case 5:
{
return 0;
}
case 6:
{
total(r);
break;
}
}
}
return 0;
}
wsf
最新推荐文章于 2024-04-05 10:00:39 发布