⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cpp

📁 易学c++程序实例 本章主要讲述一些学习程序设计前需要了解的一些知识和一些学习程序设计的方法。并且对 C++语言作了一个简要的介绍。学好这一章
💻 CPP
字号:
#include "iostream.h"
struct node
{
	char data;
	node *next;
};
node * create();
void showList(node *head);
node * search(node *head,char keyWord);
void insert(node * &head,char keyWord,char newdata);
void Delete(node * &head,char keyWord);
void destory(node * &head);
void main()
{
	node *head=NULL;
	head=create();
	showList(head);
	insert(head,'h','c');
	showList(head);
	Delete(head,'t');
	showList(head);
	destory(head);
	showList(head);
}
node * create()
{
	node * head=NULL;
	node * pEnd=head;
	node * pS;
	char temp;
	cout <<"Please input a string end with '#':" <<endl;
	do
	{
		cin >>temp;
		if (temp!='#')
		{
			pS=new node;
			pS->data=temp;
			pS->next=NULL;
			if (head==NULL)
			{
				head=pS;
			}
			else
			{
				pEnd->next=pS;
			}
			pEnd=pS;
		}
	}while (temp!='#');
	return head;
}
void showList(node *head)
{
	node *pRead=head;
	cout <<"The data of the link list are:" <<endl;
	while (pRead!=NULL)
	{
		cout <<pRead->data;
		pRead=pRead->next;
	}
	cout <<endl;
}
node * search(node *head,char keyWord)
{
	node *pRead=head;
	while (pRead!=NULL)
	{
		if (pRead->data==keyWord)
		{
			return pRead;
		}
		pRead=pRead->next;
	}
	return NULL;
}
void insert(node * &head,char keyWord,char newdata)
{
	node *newnode=new node;
	newnode->data=newdata;
	node *pGuard=search(head,keyWord);
	if (head==NULL || pGuard==NULL)
	{
		newnode->next=head;
		head=newnode;
	}
	else
	{
		newnode->next=pGuard->next;
		pGuard->next=newnode;
	}
}
void Delete(node * &head,char keyWord)
{
	if (head!=NULL)
	{
		node *p;
		node *pGuard=head;
		if (head->data==keyWord)
		{
			p=head;
			head=head->next;
			delete p;
			cout <<"The deleted node is " <<keyWord <<endl;
			return;
		}
		else
		{
			while (pGuard->next!=NULL)
			{
				if (pGuard->next->data==keyWord)
				{
					p=pGuard->next;
					pGuard->next=p->next;
					delete p;
					cout <<"The deleted node is " <<keyWord <<endl;
					return;
				}
				pGuard=pGuard->next;
			}
		}
	}
	cout <<"The keyword node is not found or the link list is empty!" <<endl;
}
void destory(node * &head)
{
	node *p;
	while (head!=NULL)
	{
		p=head;
		head=head->next;
		delete p;
	}
	cout <<"The link list has been deleted!" <<endl;
}














⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -