一、双向循环链表
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 *front; //保存前一个结点的地址 struct doublelist *next; //保存后一个结点的地址 }doublelist;
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->next = head->next; tmp->front = head; 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); }
练习:头删法删除数据
//头删法删除数据 DataType DoubleListDelete(doublelist *head) { if(head->next == head) { printf("双向循环链表为空!\n"); return (DataType)-1; } doublelist *tmp = head->next; head->next = tmp->next; tmp->next->front = head; DataType value = tmp->data; 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 *s = (seqstack *)malloc(sizeof(seqstack)); s->pos = -1; return s; }
2.2.3 判断栈是否为满
//判断栈是否为满 int SeqStackIsFull(seqstack *s) { return s->pos == N -1 ? 1 : 0; } 判断栈是否为空 //判断栈是否为空 int SeqStackIsEmpty(seqstack *s) { return s->pos == -1 ? 1: 0; }