【线性表】
线性表的特征:
1) 对非空表,a0是表头,无前驱;
2) an-1是表尾,无后继;
3) 其它的每个元素ai有且仅有一个直接前驱(ai-1)和一个直接后继(ai+1)。
【顺序存储结构】
数组 空间先固定好, 连续存储
优点:遍历(查找)方便;
不足:插入和删除时间复杂度差;
一种模型【顺序表】
#define N 10
struct list{
char data[N];
int last;
};
改进:
#define MAXSIZE 10
typedef int datatype;
typedef int postype;
typedef struct list{
datatype data[MAXSIZE];
postype last;
}Seqlist, * Seqlist_p;
创建空表函数
Seqlist_p CreateSeqList(void);
Seqlist_p CreateSeqList(void){
//1. NULL;
Seqlist_p L = NULL;
//Seqlist_p L = (Seqlist *)malloc(sizeof(Seqlist));//bug
//2. malloc 设置空表标志;
L = (Seqlist *)malloc(sizeof(Seqlist));//!sizeof
if(NULL == L){
printf("creat error!\n");
return NULL;
}
L->last = -1;
return L;
}
课堂实现:
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 10
typedef int datatype;
typedef int postype;
//typedef int * p;
typedef struct list{
datatype data[MAXSIZE];
postype last;
}Seqlist, * Seqlist_p;
Seqlist_p CreateSeqList(void);
void ClearSeqList(Seqlist_p L);
int InsertSeqList(Seqlist_p L, datatype x, postype pos);
int IsFullSeqList(Seqlist_p L);
void ShowSeqList(Seqlist_p L);
int IsEmptySeqList(Seqlist_p L);
int DeleteSeqList(Seqlist_p L, postype pos);
void FreeSeqList(Seqlist_p *L);
int LengthSeqList(Seqlist_p L);
datatype GetSeqList(Seqlist_p L, postype pos);
postype LocateSeqList(Seqlist_p L, datatype x);
作业:顺序表,两个表合并;
有数据表两个, 分别为LA, LB;
LA = {1,3,6,8,10};
LB = {1,3,7,9,10};
实现:LA = LA U LB;
void UnionSeqList(Seqlist_p L1, Seqlist_p L2){
if( NULL == L1 || NULL == L2){
printf("L is NULL\n");
return;
}
int i, x, k;
for(i = 0; i < LengthSeqList(L2); i++){
x = GetSeqList(L2, i);
if( (k = LocateSeqList(L1, x)) == -1){
InsertSeqList(L1, x, 0);
}
}
return;
}
【链式存储结构】
模型一【单链表】
#ifndef __LINKLIST_H__
#define __LINKLIST_H__
#include <stdio.h>
#include <stdlib.h>
typedef int datatype;
typedef struct node{
datatype data;
struct node * next;
}Linknode, * Linklist;
Linklist CreateLinkList();
int HeadInsertLinkList(Linklist H, datatype x);
void ShowLinkList(Linklist H);
Linklist GetLinkList(Linklist H, int pos);
Linklist LocateLinkList(Linklist H, datatype value);
int InsertLinkList(Linklist H, int pos, datatype x);
int DeleteLinkList(Linklist H, int pos);
#endif
模型二【单向循环链表】
#ifndef __LINKLIST_H__
#define __LINKLIST_H__
#include <stdio.h>
#include <stdlib.h>
typedef int datatype;
typedef struct node{
datatype data;
struct node * next;
}Linknode, * Linklist;
Linklist CreateLinkList();
#endif
Linklist CreateLinkList(){
Linklist H = NULL;
H = (Linknode *)malloc(sizeof(Linknode));
if(NULL == H){
printf("create malloc error!\n");
return NULL;
}
H->data = 0;
H->next = H;
return H;
}
模型三【双向循环链表】
#ifndef __DLINKLIST_H__
#define __DLINKLIST_H__
#include <stdio.h>
#include <stdlib.h>
typedef int datatype;
typedef struct dnode{
datatype data;
struct dnode * prior;
struct dnode * rear;
}DLinknode, * DLinklist;
DLinklist CreateDLinkList();
int InsertDLinkList(DLinklist H, datatype x);
int DeleteDLinkList(DLinklist H);
void ShowDLinkList(DLinklist H);
#endif
DLinklist CreateDLinkList(){
DLinklist H = NULL;
H = (DLinknode *)malloc(sizeof(DLinknode));
if(NULL == H){
printf("create malloc error!\n");
return NULL;
}
H->data = 0;
H->prior= H;
H->rear = H;
return H;
}
int InsertDLinkList(DLinklist H, datatype x){
if(NULL == H){
return -1;
}
DLinklist new = NULL;
new = (DLinknode *)malloc(sizeof(DLinknode));
if(NULL == new){
return -1;
}
new->data = x;
new->rear = H->rear;
new->prior = H;
(new->rear)->prior = new;
(new->prior)->rear = new;
return 0;
}
int DeleteDLinkList(DLinklist H){
if(NULL == H){
return -1;
}
DLinklist p = H->rear;
(p->rear)->prior = H;
(p->prior)->rear = p->rear;
free(p);
p = NULL;
return 0;
}
void ShowDLinkList(DLinklist H){
if(NULL == H){
return;
}
#if 1
DLinklist p = H;
while(p->rear != H){
p = p->rear;
printf("%d\n", p->data);
}
#else
while(H->rear != H){
H = H->rear;
printf("%d\n", H->data);
}
#endif
}