wsf

//程序实现在单链表的某给定字符节点后插入新结点算法,如找不到则查在尾部;
//查找给定结点并删除的算法
//单链表逆转算法

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值