先是尾插法(正序)
需要一个尾指针同样指向头结点,注意最后应置空;
#include <stdio.h>
#include <stdlib.h>
typedef struct LNode
{
int data;
struct LNode *next;
}LNode, *LinkList;
LinkList Create();
void print(LinkList L);
int main()
{
LinkList L = Create();
print(L);
return 0;
}
LinkList Create()
{
LinkList L, p, s;
int e;
L = (LinkList)malloc(sizeof(LNode));
L->next = NULL;
p = L;
scanf("%d", &e);
while (e != -1)
{
s = (LinkList)malloc(sizeof(LNode));
s->data = e;
p->next=s;
p = s;
scanf("%d", &e);
}
p->next = NULL;
return L;
}
void print(LinkList L)
{
LinkList p;
p = L->next;
while (p)
{
printf("%d ", p->data);
p = p->next;
}
}
头插法(逆序)
#include <stdio.h>
#include <stdlib.h>
typedef struct LNode
{
int data;
s