📄 main.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 + -