1.cpp

来自「数据结构中的简单链表创建」· C++ 代码 · 共 55 行

CPP
55
字号
#include<iostream>
using namespace std;
struct node
{
	char data;//用于存放数据
	node *next;//定义一个向下的节点
};
node *Create();//创建链表的函数
void Showlist(node *head);//遍历链表的函数
void main()
{
	node *head;
	head= Create();//以head为表头创建一个链表
	Showlist(head);//表里以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;
}

⌨️ 快捷键说明

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