#include <iostream>
using namespace std;
struct node
{
int data;
struct node *next;
};
void Front_Insert(struct node *L,int k)//头插
{
struct node *p;//建立新节点
int i,m;
for(i=0;i<k;i++)
{
cin>>m;
p=new(struct node);
p->data=m;//将输入的值赋值给节点p
p->next=L->next;//将头结点的指针赋值给p节点
L->next=p;//L节点指向p
}
}
void End_Insert(struct node *L,int k)//尾插
{
struct node *q,*r;//建立新节点
r=L;//头结点赋值与r,这样r就具有头结点的性质
int i,m;
for(i=0;i<k;i++)
{
cin>>m;
q=new(struct node);
q->data=m;将输入的值赋值给节点q
r->next=q;//r结点指向q节点
r=q;//令r接点等于q接点
}
r->next=NULL;//最后将最后一节点的指针置空
}
int main()
{
struct node s;
s.next=NULL;//将初始化的链表置空
int n,k,i;
cin>>n;
Front_Insert(&s,n);//头插
struct node *q;//输出
q->next=s.next;
while(q->next!=NULL)
{
q=q->next;
cout<<q->data<<" ";
}
return 0;
}
线性表
最新推荐文章于 2021-02-18 18:07:12 发布