/**
* @author huihut
* @E-mail:huihut@outlook.com
* @version 创建时间:2016年9月18日
* 说明:本程序实现了一个单链表。
*/
#include "stdio.h"
#include "stdlib.h"
#include "malloc.h"
//5个常量定义
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define OVERFLOW -1
//类型定义
typedef int Status;
typedef int ElemType;
//测试程序长度定义
#define LONGTH 5
//链表的类型
typedef struct LNode {
ElemType data;
struct LNode *next;
} LNode, *LinkList;
Status InitList_L(LinkList &L);
Status DestroyList_L(LinkList &L);
Status ClearList_L(LinkList &L);
Status ListEmpty_L(LinkList L);
int ListLength_L(LinkList L);
LNode* Search_L(LinkList L, ElemType e);
LNode* NextElem_L(LNode *p);
Status InsertAfter_L(LNode *p, LNode *q);
Status DeleteAfter_L(LNode *p, ElemType &e);
void ListTraverse_L(LinkList L, Status(*visit)(ElemType e));
//创建包含n个元素的链表L,元素值存储在data数组中
Status create(LinkList &L, ElemType *data, int n) {
LNode *p, *q;
int i;
if (n < 0) return ERROR;
L = NULL;
p = L;
for (i = 0; i < n; i++)
{
q = (LNode *)malloc(sizeof(LNode));
if (NULL == q) return OVERFLOW;
q->data = data[i];
q->next = NULL;
if (NULL == p) L = q;
else p->next = q;
p = q;
}
return OK;
}
//e从链表末尾入链表
Status EnQueue_LQ(LinkList &L, ElemType &e) {
LinkList p, q;
if (NULL == (q = (LNode *)malloc(sizeof(LNode)))) return OVERFLOW;
q->data = e;
q->next = NULL;
if (NULL == L) L = q;
else
{
p = L;
while (p->next != NULL)
{
p = p->next;
}
p->next = q;
}
return OK;
}
//从链表头节点出链表到e
Status DeQueue_LQ(LinkList &L, ElemType &e) {
if (NULL == L) return ERROR;
LinkList p;
p = L;
e = p->data;
L = L->next;
free(p);
return OK;
}
//遍历调用
Status visit(ElemType e) {
printf("%d\t", e);
}
//遍历单链表
void ListTraverse_L(LinkList L, Status(*visit)(ElemType e))
{
if (NULL == L) return;
for (LinkList p = L; NULL != p; p = p -> next) {
visit(p -> data);
}
}
int main() {
int i;
ElemType e, data[LONGTH] = { 1, 2, 3, 4, 5 };
LinkList L;
//显示测试值
printf("---【单链表】---\n");
printf("待测试元素为:\n");
for (i = 0; i < LONGTH; i++) printf("%d\t", data[i]);
printf("\n");
//创建链表L
printf("创建链表L\n");
if (ERROR == create(L, data, LONGTH))
{
printf("创建链表L失败\n");
return -1;
}
printf("成功创建包含%d个元素的链表L\n元素值存储在data数组中\n", LONGTH);
//遍历单链表
printf("此时链表中元素为:\n");
ListTraverse_L(L, visit);
//从链表头节点出链表到e
printf("\n出链表到e\n");
DeQueue_LQ(L, e);
printf("出链表的元素为:%d\n", e);
printf("此时链表中元素为:\n");
//遍历单链表
ListTraverse_L(L, visit);
//e从链表末尾入链表
printf("\ne入链表\n");
EnQueue_LQ(L, e);
printf("入链表的元素为:%d\n", e);
printf("此时链表中元素为:\n");
//遍历单链表
ListTraverse_L(L, visit);
printf("\n");
return 0;
}
/**
* @author huihut
* @E-mail:huihut@outlook.com
* @version 创建时间:2016年9月23日
* 说明:本程序实现了一个具有头结点的单链表。
*/
#include "stdio.h"
#include "stdlib.h"
#include "malloc.h"
//5个常量定义
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define OVERFLOW -1
//类型定义
typedef int Status;
typedef int ElemType;
//测试程序长度定义
#define LONGTH 5
//链表的类型
typedef struct LNode {
ElemType data;
struct LNode *next;
} LNode, *LinkList;
Status InitList_L(LinkList &L);
Status DestroyList_L(LinkList &L);
Status ClearList_L(LinkList &L);
Status ListEmpty_L(LinkList L);
int ListLength_L(LinkList L);
LNode* Search_L(LinkList L, ElemType e);
LNode* NextElem_L(LNode *p);
Status InsertAfter_L(LNode *p, LNode *q);
Status DeleteAfter_L(LNode *p, ElemType &e);
void ListTraverse_L(LinkList L, Status(*visit)(ElemType e));
//创建包含n个元素的链表L,元素值存储在data数组中
Status create(LinkList &L, ElemType *data, int n) {
LNode *p, *q;
int i;
if (n < 0) return ERROR;
p = L = NULL;
q = (LNode *)malloc(sizeof(LNode));
if (NULL == q) return OVERFLOW;
q->next = NULL;
p = L = q;
for (i = 0; i < n; i++)
{
q = (LNode *)malloc(sizeof(LNode));
if (NULL == q) return OVERFLOW;
q->data = data[i];
q->next = NULL;
p->next = q;
p = q;
}
return OK;
}
//e从链表末尾入链表
Status EnQueue_LQ(LinkList &L, ElemType &e) {
LinkList p, q;
if (NULL == (q = (LNode *)malloc(sizeof(LNode)))) return OVERFLOW;
q->data = e;
q->next = NULL;
if (NULL == L)
{
L = (LNode *)malloc(sizeof(LNode));
if (NULL == L) return OVERFLOW;
L -> next = q;
}
else if (NULL == L->next) L = q;
else
{
p = L;
while (p->next != NULL)
{
p = p->next;
}
p->next = q;
}
return OK;
}
//从链表头节点出链表到e
Status DeQueue_LQ(LinkList &L, ElemType &e) {
if (NULL == L || NULL == L->next) return ERROR;
LinkList p;
p = L->next;
e = p->data;
L->next = p->next;
free(p);
return OK;
}
//遍历调用
Status visit(ElemType e) {
printf("%d\t", e);
}
//遍历单链表
void ListTraverse_L(LinkList L, Status(*visit)(ElemType e))
{
if (NULL == L || NULL == L->next) return;
for (LinkList p = L -> next; NULL != p; p = p -> next) {
visit(p -> data);
}
}
int main() {
int i;
ElemType e, data[LONGTH] = { 1, 2, 3, 4, 5 };
LinkList L;
//显示测试值
printf("---【有头结点的单链表】---\n");
printf("待测试元素为:\n");
for (i = 0; i < LONGTH; i++) printf("%d\t", data[i]);
printf("\n");
//创建链表L
printf("创建链表L\n");
if (ERROR == create(L, data, LONGTH))
{
printf("创建链表L失败\n");
return -1;
}
printf("成功创建包含1个头结点、%d个元素的链表L\n元素值存储在data数组中\n", LONGTH);
//遍历单链表
printf("此时链表中元素为:\n");
ListTraverse_L(L, visit);
//从链表头节点出链表到e
printf("\n出链表到e\n");
DeQueue_LQ(L, e);
printf("出链表的元素为:%d\n", e);
printf("此时链表中元素为:\n");
//遍历单链表
ListTraverse_L(L, visit);
//e从链表末尾入链表
printf("\ne入链表\n");
EnQueue_LQ(L, e);
printf("入链表的元素为:%d\n", e);
printf("此时链表中元素为:\n");
//遍历单链表
ListTraverse_L(L, visit);
printf("\n");
return 0;
}
/**
* @author huihut
* @E-mail:huihut@outlook.com
* @version 创建时间:2016年9月9日
* 说明:本程序实现了一个顺序表。
*/
#include "stdio.h"
#include "stdlib.h"
#include "malloc.h"
//5个常量定义
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define OVERFLOW -1
//测试程序长度定义
#define LONGTH 5
//类型定义
typedef int Status;
typedef int ElemType;
//顺序栈的类型
typedef struct {
ElemType *elem;
int length;
int size;
int increment;
} SqList;
Status InitList_Sq(SqList &L, int size, int inc); //初始化顺序表L
Status DestroyList_Sq(SqList &L); //销毁顺序表L
Status ClearList_Sq(SqList &L); //将顺序表L清空
Status ListEmpty_Sq(SqList L); //若顺序表L为空表,则返回TRUE,否则FALSE
int ListLength_Sq(SqList L); //返回顺序表L中元素个数
Status GetElem_Sq(SqList L, int i, ElemType &e); //用e返回顺序表L中第i个元素的值
int Search_Sq(SqList L, ElemType e); //在顺序表L顺序查找元素e,成功时返回该元素在表中第一次出现的位置,否则返回-1
Status ListTraverse_Sq(SqList L, Status(*visit)(ElemType e)); //遍历顺序表L,依次对每个元素调用函数visit()
Status PutElem_Sq(SqList &L, int i, ElemType e); //将顺序表L中第i个元素赋值为e
Status Append_Sq(SqList &L, ElemType e); //在顺序表L表尾添加元素e
Status DeleteLast_Sq(SqList &L, ElemType &e); //删除顺序表L的表尾元素,并用参数e返回其值
//初始化顺序表L
Status InitList_Sq(SqList &L, int size, int inc) {
L.elem = (ElemType *)malloc(size * sizeof(ElemType));
if (NULL == L.elem) return OVERFLOW;
L.length = 0;
L.size = size;
L.increment = inc;
return OK;
}
//销毁顺序表L
Status DestroyList_Sq(SqList &L) {
free(L.elem);
L.elem = NULL;
return OK;
}
//将顺序表L清空
Status ClearList_Sq(SqList &L) {
if (0 != L.length) L.length = 0;
return OK;
}
//若顺序表L为空表,则返回TRUE,否则FALSE
Status ListEmpty_Sq(SqList L) {
if (0 == L.length) return TRUE;
return FALSE;
}
//返回顺序表L中元素个数
int ListLength_Sq(SqList L) {
return L.length;
}
// 用e返回顺序表L中第i个元素的值
Status GetElem_Sq(SqList L, int i, ElemType &e) {
e = L.elem[--i];
return OK;
}
// 在顺序表L顺序查找元素e,成功时返回该元素在表中第一次出现的位置,否则返回 - 1
int Search_Sq(SqList L, ElemType e) {
int i = 0;
while (i < L.length && L.elem[i] != e) i++;
if (i < L.length) return i;
else return -1;
}
//遍历调用
Status visit(ElemType e) {
printf("%d\t",e);
}
//遍历顺序表L,依次对每个元素调用函数visit()
Status ListTraverse_Sq(SqList L, Status(*visit)(ElemType e)) {
if (0 == L.length) return ERROR;
for (int i = 0; i < L.length; i++) {
visit(L.elem[i]);
}
return OK;
}
//将顺序表L中第i个元素赋值为e
Status PutElem_Sq(SqList &L, int i, ElemType e) {
if (i > L.length) return ERROR;
e = L.elem[--i];
return OK;
}
//在顺序表L表尾添加元素e
Status Append_Sq(SqList &L, ElemType e) {
if (L.length >= L.size) return ERROR;
L.elem[L.length] = e;
L.length++;
return OK;
}
//删除顺序表L的表尾元素,并用参数e返回其值
Status DeleteLast_Sq(SqList &L, ElemType &e) {
if (0 == L.length) return ERROR;
e = L.elem[L.length - 1];
L.length--;
return OK;
}
int main() {
//定义表L
SqList L;
//定义测量值
int size, increment, i;
//初始化测试值
size = LONGTH;
increment = LONGTH;
ElemType e, eArray[LONGTH] = { 1, 2, 3, 4, 5 };
//显示测试值
printf("---【顺序栈】---\n");
printf("表L的size为:%d\n表L的increment为:%d\n", size, increment);
printf("待测试元素为:\n");
for (i = 0; i < LONGTH; i++) {
printf("%d\t", eArray[i]);
}
printf("\n");
//初始化顺序表
if (!InitList_Sq(L, size, increment)) {
printf("初始化顺序表失败\n");
exit(0);
}
printf("已初始化顺序表\n");
//判空
if(TRUE == ListEmpty_Sq(L)) printf("此表为空表\n");
else printf("此表不是空表\n");
//入表
printf("将待测元素入表:\n");
for (i = 0; i < LONGTH; i++) {
if(ERROR == Append_Sq(L, eArray[i])) printf("入表失败\n");;
}
printf("入表成功\n");
//遍历顺序表L
printf("此时表内元素为:\n");
ListTraverse_Sq(L, visit);
//出表
printf("\n将表尾元素入表到e:\n");
if (ERROR == DeleteLast_Sq(L, e)) printf("出表失败\n");
printf("出表成功\n出表元素为%d\n",e);
//遍历顺序表L
printf("此时表内元素为:\n");
ListTraverse_Sq(L, visit);
//销毁顺序表
printf("\n销毁顺序表\n");
if(OK == DestroyList_Sq(L)) printf("销毁成功\n");
else printf("销毁失败\n");
return 0;
}
/**
* @author huihut
* @E-mail:huihut@outlook.com
* @version 创建时间:2016年9月9日
* 说明:本程序实现了一个顺序栈。
* 功能:有初始化、销毁、判断空、清空、入栈、出栈、取元素的操作。
*/
#include "stdio.h"
#include "stdlib.h"
#include "malloc.h"
//5个常量定义
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define OVERFLOW -1
//测试程序长度定义
#define LONGTH 5
//类型定义
typedef int Status;
typedef int ElemType;
//顺序栈的类型
typedef struct {
ElemType *elem;
int top;
int size;
int increment;
} SqSrack;
//函数声明
Status InitStack_Sq(SqSrack &S, int size, int inc); //初始化顺序栈
Status DestroyStack_Sq(SqSrack &S); //销毁顺序栈
Status StackEmpty_Sq(SqSrack S); //判断S是否空,若空则返回TRUE,否则返回FALSE
void ClearStack_Sq(SqSrack &S); //清空栈S
Status Push_Sq(SqSrack &S, ElemType e); //元素e压入栈S
Status Pop_Sq(SqSrack &S, ElemType &e); //栈S的栈顶元素出栈,并用e返回
Status GetTop_Sq(SqSrack S, ElemType &e); //取栈S的栈顶元素,并用e返回
//初始化顺序栈
Status InitStack_Sq(SqSrack &S, int size, int inc) {
S.elem = (ElemType *)malloc(size * sizeof(ElemType));
if (NULL == S.elem) return OVERFLOW;
S.top = 0;
S.size = size;
S.increment = inc;
return OK;
}
//销毁顺序栈
Status DestroyStack_Sq(SqSrack &S) {
free(S.elem);
S.elem = NULL;
return OK;
}
//判断S是否空,若空则返回TRUE,否则返回FALSE
Status StackEmpty_Sq(SqSrack S) {
if (0 == S.top) return TRUE;
return FALSE;
}
//清空栈S
void ClearStack_Sq(SqSrack &S) {
if (0 == S.top) return;
S.size = 0;
S.top = 0;
}
//元素e压入栈S
Status Push_Sq(SqSrack &S, ElemType e) {
ElemType *newbase;
if (S.top >= S.size) {
newbase = (ElemType *)realloc(S.elem, (S.size + S.increment) * sizeof(ElemType));
if (NULL == newbase) return OVERFLOW;
S.elem = newbase;
S.size += S.increment;
}
S.elem[S.top++] = e;
return OK;
}
//取栈S的栈顶元素,并用e返回
Status GetTop_Sq(SqSrack S, ElemType &e) {
if (0 == S.top) return ERROR;
e = S.elem[S.top - 1];
return e;
}
//栈S的栈顶元素出栈,并用e返回
Status Pop_Sq(SqSrack &S, ElemType &e) {
if (0 == S.top) return ERROR;
e = S.elem[S.top - 1];
S.top--;
return e;
}
int main() {
//定义栈S
SqSrack S;
//定义测量值
int size, increment, i;
//初始化测试值
size = LONGTH;
increment = LONGTH;
ElemType e, eArray[LONGTH] = { 1, 2, 3, 4, 5 };
//显示测试值
printf("---【顺序栈】---\n");
printf("栈S的size为:%d\n栈S的increment为:%d\n", size, increment);
printf("待测试元素为:\n");
for (i = 0; i < LONGTH; i++) {
printf("%d\t", eArray[i]);
}
printf("\n");
//初始化顺序栈
if (!InitStack_Sq(S, size, increment)) {
printf("初始化顺序栈失败\n");
exit(0);
}
printf("已初始化顺序栈\n");
//入栈
for (i = 0; i < S.size; i++) {
if (!Push_Sq(S, eArray[i])) {
printf("%d入栈失败\n", eArray[i]);
exit(0);
}
}
printf("已入栈\n");
//判断非空
if(StackEmpty_Sq(S)) printf("S栈为空\n");
else printf("S栈非空\n");
//取栈S的栈顶元素
printf("栈S的栈顶元素为:\n");
printf("%d\n", GetTop_Sq(S, e));
//栈S元素出栈
printf("栈S元素出栈为:\n");
for (i = 0, e = 0; i < S.size; i++) {
printf("%d\t", Pop_Sq(S, e));
}
printf("\n");
//清空栈S
ClearStack_Sq(S);
printf("已清空栈S\n");
return 0;
}