线性表
静态、顺序存储实现
#define MAXSIZE 1024
typedef int Elem; //可以是普通数据类型或结构体
struct seqList{ //顺序存储结构
Elem data[MAXSIZE];
int lenght; //元素个数
};
void seqList_init(struct List *LL) //初始化
{
if(LL == NULL) return;
LL->lenght = 0;
memset(LL->data,0,MAXSIZE);
}
动态、可扩展、顺序存储实现
#define INITSIZE 10
#define EXTSIZE 5;
typedef int Elem;
struct seqList{
Elem *data;
int size; //表总长
int lenght //元素个数
};
void seqList_init(struct seqList *LL){
if(LL == NULL) return;
LL->data = (Elem *)malloc(INITSIZE * sizeof(Elem));
LL->size = INITSIZE;
LL->lenght = 0;
memset(LL->data,0,LL->size);
}
int Ext(struct seqList *LL){ //扩展表长
Elem *p = (Elem *)malloc(LL->size * sizeof(Elem) + EXTSIZE);//分配更长的表
if(p == NULL) return 0;
memset(p,0,LL->size * sizeof(Elem) + EXTSIZE); //初始化
memcpy(p,LL->data,sizeof(Elem)*LL->lenght); //拷贝原来数据
free(LL->data);
LL->data = p;
LL->size = LL-size + EXTSIZE;
return 1;
}
链式存储实现
设置头指针是必要的,但有没有头结点,则需要根据实际问题特殊分析
typedef int Elem;
struct linkList{
Elem data;
struct linkList *next;
};
struct linkList * linkList_init(void){ //返回头节点指针
struct linkList *head = (struct linkList*)malloc(sizeof(struct linkList));
if(head == NULL) return NULL;
head->next = NULL;
return head;
}
//无头节点链表,直接初始化为NULL
struct linkList *LL = NULL;
双向链表
typedef int Elem;
struct linkList{
Elem data;
struct linkList *pre,*next;
}
栈
栈是操作受限的线性表
顺序存储实现
#define MAXSIZE 10
typedef int Elem;
struct seqStack{
Elem data[MAXSIZE];
int top; //堆栈指针
};
void seqStack_init(struct seqStack *SS){
if(SS == NULL) return ;
memset(SS->data,0,sizeof(Elem)*MAXSIZE); //
SS->top = -1; //初始为-1,压栈+1,弹栈-1
}
链式存储实现(带头节点)
struct linkStack{
Elem data;
struct linkStack *next;
};
struct linkStack * linkStack_init(){ //返回指针,或者传入二级指针
struct linkStack *head = (struct linkStack *)malloc(sizeof(struct linkStack));
if(head == NULL) return NULL;
head->next = NULL;
return head;
}
push(); //从头部插入
pop(); //从头部弹出
队列
队列也是操作受限的线性表
顺序存储实现
一般的队列顺序存储有假溢出问题,所以使用循环队列
#define SIZE 10
typedef int Elem;
struct seqQueue{
Elem data[];
int head,rear;
};
void seqQueue_init(struct seqQueue *QQ){
if(QQ == NULL) return;
QQ->head = QQ = rear = 0;
memset(QQ-data,0,SIZE * sizeof(Elem));
}
/*
队空 head == rear
队满 (rear+1)%MAXSIZE == head
队列长度 (rear - head + MAXSIZE)%MAXSIZE
*/
链式存储实现(带头节点)
typedef int Elem;
struct QNode{
Elem data;
struct QNode *next;
};
struct linkQueue{
struct QNode *head,*tail;
};
/*
传入队列结构指针
创建头节点
初始化指针
*/
int linkQueue_init(struct QNode *QQ){
if(QQ == NULL) return 0;
struct QNode *node = (struct QNode *)malloc(sizeof(struct QNode));
if(node == NULL) return 0;
head->next = NULL;
QQ->head = QQ->tail = head;
return 1;
}
二叉树
typedef int Elem;
struct Tree{
Elem data;
struct Tree *left,*right;
};
hash
链地址法:
typedef struct Data{ //定义数据
unsigned int key; //关键字
int data;
} Elem;
struct Node{ //节点
Elem data;
struct Node *next;
};
typedef struct Hash{ //连地址法hash
struct Node *head; //动态分配数组
int size; //数组大小
int count; //元素个数
} HashTable;
HashTable * Hash_init(const unsigned int size){ //返回指针,或者传入二级指针
HashTable * p = (HashTable *)malloc(sizeof(HashTable));
if(p == NULL) return NULL;
p->head = (struct Node*)malloc(size * sizeof(struct Node));
if(p->head == NULL){
free(p);
return NULL;
}
memset(p->head,0,size * sizeof(struct Node));
p->size = size;
p->count = 0;
return p;
}
开放寻址法:
typedef struct{
unsigned int key;
...//data
} Elem;
struct hash{
Elem *data; //动态分配数组
int count; //元素个数
};