访问次数:

后插法创建单链表(顺序存储) | Wenji's blog
头部背景图片
WenJi's blog |
WenJi's blog |

后插法创建单链表(顺序存储)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include<iostream>
#include<cstdio>
#define OK 1
using namespace std;
typedef struct LNode //这个LNode不能省略
{
int data;
struct LNode *next;
}LNode,*LinkList;
bool InitList(LinkList &L)
{
L=new LNode;
L->next=NULL;
return OK;
}
int main()
{
LinkList L;
InitList(L);
LNode *p;
LNode *last=L; //尾指针
for(int i=0;i<5;i++)
{
p=new LNode;
cin>>p->data;
p->next=NULL;
last->next=p; //尾指针指向它
last=p;
}
p=L->next;
while(p)
{
cout<<p->data<<endl;
p=p->next;
}
return 0;
}