题目
建立一个包括头结点和4个结点的(5,4,2,1)的单链表,实现单链表建立的基本操作,并且将该单链表的所有元素显示出来
#include<iostream>
using namespace std;
typedef int ElemType;
typedef struct Node{
ElemType data;
struct Node *next;
}Node;
int Init(Node *&L){//初始化链表
L = new Node;
L->next = NULL;
return 1;
}
void Creat(Node *L){//创建链表
int n;
cout<<"前插法输入数据个数:";
cin>>n;
Node *s;
cout<<"输入数据:" ;
for(int i = 0; i < n; i++){
s = new Node;
cin>>s->data;
s->next = L->next;
L->next = s;
}
}
int Show(Node *L){//输出链表
cout<<"表中所有元素:";
Node *p = L->next;
if(!p){
cout<<"表为空!";
return 0;
}
while(p){
cout<<p->data<<" ";
p = p->next;
}
cout<<endl;
}
int main(){
Node *List;
Init(List);
Creat(List);
Show(List);
}
参考资料:
《数据结构 C语言版 第2版》严蔚敏 李冬梅 吴伟民
最后
以上就是瘦瘦钻石最近收集整理的关于建立一个包括头结点和4个结点的(5,4,2,1)的单链表,实现单链表建立的基本操作,并且将该单链表的所有元素显示出来的全部内容,更多相关建立一个包括头结点和4个结点内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复