目录
一、双向循环链表
1.1 概念
1.2 操作
1.2.1定义结构体
#ifndef _DOUBLELIST_H_
#define _DOUBLELIST_H_
#include <stdio.h>
#include <stdlib.h>
typedef int DataType;
typedef struct doublelist
{
DataType data;
struct doublelist *next;
struct doublelist *front;
}doublelist;
#endif
1.2.2 创建一个空的双向循环链表
//创建一个空的双向循环链表
doublelist* DoubleListCreate()
{
doublelist *head = (doublelist *)malloc(sizeof(doublelist));
head->front = head;
head->next = head;
return head;
}
1.2.3 插入数据
//插入数据
void DoubleListInsert(doublelist *head,DataType value)
{
doublelist *tmp = (doublelist *)malloc(sizeof(doublelist));
tmp->front = NULL;
tmp->next = NULL;
tmp->data = value;
//插入数据
tmp->front = head;
tmp->next = head->next;
head->next->front = tmp;
head->next = tmp;
}
1.2.4 遍历链表
//遍历双向循环链表
void DoubleListPrint(doublelist *head)
{
doublelist *p = head;
while (p->next != head)
{
p = p->next;
printf("%d ",p->data);
}
putchar(10);
}
1.2.5 判断双向循环链表是否为空
//判断双向循环链表是否为空
int DoubuleListIsEmpty(doublelist *head)
{
return head->next == head ? 1 : 0;
}
1.2.6 删除数据
//删除数据(头删法)
DataType DoubleListDelete(doublelist *head)
{
if(DoubuleListIsEmpty(head))
{
printf("删除失败,链表为空!\n");
return (DataType)-1;
}
else
{
doublelist *p = head;
doublelist *tmp = p->next;
DataType value;
value = tmp->data;
p->next = p->next->next;
p->next->next->front = p;
free(tmp);
tmp = NULL;
return value;
}
}
二、栈(stack)
2.1 概念
栈的性质:后进先出
栈的操作:
入栈(压栈)push
出栈(单栈)pop
2.2 顺序栈 seqstack
2.2.1定义数据类型及结构体
#ifndef _SEQSTACK_H_
#define _SEQSTACK_H_
#include <stdio.h>
#include <stdlib.h>
#define N 32
//定义数据类型
typedef int DataType;
//定义结构体
typedef struct seqstack
{
DataType data[N];
int pos;
}seqstack;
#endif
2.2.2 创建一个空栈
//创建一个空栈
seqstack *SeqStackCreate()
{
seqstack *ss = (seqstack *)malloc(sizeof(seqstack));
ss->pos = -1;
return ss;
}
2.2.3 判断栈是否为满
//判断栈是否为满
int SeqStackIsFull(seqstack *ss)
{
return ss->pos == N - 1 ? 1 : 0;
}
2.2.4 入栈
//入栈
void SeqStackInsert(seqstack *ss,DataType value)
{
if(SeqStackIsFull(ss))
{
printf("入栈失败,栈表为满!\n");
return;
}
else
{
ss->pos++;
ss->data[ss->pos] = value;
printf("入栈成功!");
}
}
2.2.5判断栈是否为空
//判断栈是否为空
int SeqStackIsEmpty(seqstack *ss)
{
return ss->pos == -1 ? 1 : 0;
}
2.2.6 出栈
//出栈
DataType SeqStackDelete(seqstack *ss)
{
if(SeqStackIsEmpty(ss))
{
printf("出栈失败,栈表为空!\n");
return (DataType)-1;
}
else
{
DataType value = ss->data[ss->pos];
ss->pos--;
return value;
}
}
2.3 链栈
2.3.1 定义结构体
#ifndef _LINKSTACK_H_
#define _LINKSTACK_H_
#include <stdio.h>
#include <stdlib.h>
typedef int DataType;
typedef struct node //保存结点信息
{
DataType data;
struct node *next;
}Node;
typedef struct linkstack //保存栈的信息
{
Node *top;
int length;
}stack;
#endif
2.3.2 初始化栈信息
//初始化栈信息
void InitStack(stack *s)
{
s->length = 0;
s->top = NULL;
}
2.3.3 入栈
//压栈
void push(stack *s,DataType value)
{
if(NULL == s)
{
printf("栈空间分配失败,初始化失败!\n");
return;
}
Node *tmp = (Node *)malloc(sizeof(Node));
tmp->next = NULL;
tmp->data = value;
tmp->next = s->top;
s->top = tmp;
s->length++;
}
2.3.4 获取栈顶元素
//获取栈顶元素
DataType GetTop(stack *s)
{
if (NULL == s)
{
return (DataType)-1;
}
if(s->top == NULL)
{
return (DataType)-1;
}
return s->top->data;
}
2.3.5 出栈
//出栈
DataType pop(stack *s)
{
if (NULL == s)
{
return (DataType)-1;
}
if(s->top == NULL)
{
return (DataType)-1;
}
Node *tmp = s->top;
s->top = tmp->next;
DataType value = tmp->data;
free(tmp);
tmp = NULL;
s->length--;
return value;
}
2.3.6清空栈
//清空栈
DataType ClearStack(stack *s)
{
if (NULL == s)
{
return (DataType)-1;
}
if(s->top == NULL)
{
return (DataType)-1;
}
Node *tmp = s->top;
while (tmp)
{
s->top = tmp->next;
free(tmp);
tmp = s->top;
s->length--;
}
return 1;
}