线性表:具有相同数据类型的n个数据元素的有限序列
链表:链式存储实现的线性表
单链表
单链表的建立
头插法(带头结点)
#include<stdio.h>
#include<stdlib.h>
typedef struct LNode{
int data;
struct LNode *next;
}*LinkList;
void showLinkList(LinkList &L) {
struct LNode *p;
p=L->next;
while(1){
printf("%d\t",p->data);
if(p->next !=NULL)
p=p->next;
else
break;
}
}
int main(){
int n;
struct LNode *s,*p,*L;
L=(LNode *)malloc(sizeof(LNode)); //分配一个头结点
L->next=NULL;
p=L;
scanf("%d",&n);
while(n!=-1){ //输入-1结束
s=(LNode *)malloc(sizeof(LNode)); //对头结点的后插操作
s->data=n;
s->next=p->next;
p->next=s;
scanf("%d",&n);
}
showLinkList(L);
}
尾插法(带头结点)
#include<stdio.h>
#include<stdlib.h>
typedef struct LNode{
int data;
struct LNode *next;
}*LinkList;
void showLinkList(LinkList &L) {
struct LNode *p;
p=L->next;
while(1){
printf("%d\t",p->data);
if(p->next !=NULL)
p=p->next;
else
break;
}
}
int main(){
int n;
struct LNode *s,*p,*L;
L=(LNode *)malloc(sizeof(LNode)); //分配一个头结点
L->next=NULL;
p=L;
scanf("%d",&n);
while(n!=-1){ //输入-1结束
s=(LNode *)malloc(sizeof(LNode));
s->data=n;
p->next=s;
p=s;
scanf("%d",&n);
}
p->next=NULL;
showLinkList(L);
}