3.1 栈
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
typedef int ElemType;
typedef struct DNode {
ElemType data;
struct DNode *prior,* next;//前驱 后继
}DNode, * DLinkList;
//双向链表头插法
DLinkList Dlist_head_insert(DLinkList& DL)
{
DNode* s; int x;
DL = (DLinkList)malloc(sizeof(DNode));//带头结点的链表,DL就是头结点
DL->next = NULL;
DL->prior = NULL;
scanf("%d", &x);
while (x != 9999)
{
s = (DLinkList)malloc(sizeof(DNode));//申请一个空间,强制类型转换
s->data = x;
s->next = DL->next;
if (DL->next != NULL)//插入第一个结点时,不需要这一步操作
{
DL->next->prior = s;
}
s->prior = DL;
DL->next=s;
scanf("%d", &x);
}
return DL;
}
//双向链表尾插法
DLinkList DList_tail_insert(DLinkList& DL)
{
int x;
DL = (DLinkList)malloc(sizeof(DNode));//带头结点的链表
DNode *s, * r = DL;//r代表尾指针
DL->prior = NULL;
//3 4 5 6 7 9999
scanf("%d", &x);
while (x != 9999)
{
s = (DNode*)malloc(sizeof(DNode));
s->data = x;
r->next = s;
s->prior = r;
r = s;//r指向新的表尾结点
scanf("%d", &x);
}
r->next = NULL;
return DL;
}
DNode* GetElem(DLinkList DL, int i)
{
int j = 1;
DNode* p = DL->next;
if (i == 0)
return DL;
if (i < 1)
return NULL;
while (p && j < i)
{
p = p->next;
j++;
}
return p;
}
//新节点插入第i个位置
bool DListFrontInsert(DLinkList DL, int i, ElemType e)
{
DLinkList p = GetElem(DL, i - 1);//找前一个位置的地址;
if (NULL == p)
{
return false;
}
DLinkList s = (DLinkList)malloc(sizeof(DNode));//为新插入的结点申请空间
s->data = e;
s->next = p->next;
p->next->prior = s;
s->prior = p;
p->next = s;
return true;
}
//删除第i个结点
bool DListDelete(DLinkList DL, int i)
{
DLinkList p = GetElem(DL, i - 1);
if (NULL == p)
{
return false;
}
DLinkList q;
q = p->next;
if (q = NULL)//删除的元素不存在
{
return false;
}
p->next = q->next;//断链
if (q->next != NULL)//q->next为NULL删除的是最后一个结点
{
q->next->prior = p;
}
free(q);
return true;
}
//链表打印
void PrintDList(DLinkList DL)
{
DL = DL->next;
while (DL != NULL)
{
printf("%3d", DL->data);
DL = DL->next;
}
printf("\n");
}
int main()
{
DLinkList DL;
DLinkList search;
//Dlist_head_insert(DL);//头插法
DList_tail_insert(DL);//尾插法
PrintDList(DL);
search = GetElem(DL, 2);
if (search != NULL)
{
printf("按序号查找成功\n");
printf("%d\n", search->data);
}
DListFrontInsert(DL, 3, 99);
PrintDList(DL);
DListDelete(DL, 2);
PrintDList(DL);
return 0;
}
3.2 循环队列
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#define MaxSize 5
typedef int ElemType;
//当结构体中没有含有自身的指针时,可以省略结构体名
typedef struct {
ElemType data[MaxSize];//数组,存放MaxSize-1个元素
int rear, front;//队列头 队列尾
}SqQueue;
//初始化
void InitQueue(SqQueue& Q)
{
Q.rear = Q.front = 0;
}
//判断是否为空
bool isEmpty(SqQueue& Q)
{
if (Q.rear == Q.front)//不需要都等于零
{
return true;
}
return false;
}
//入队
bool EnQueue(SqQueue& Q, ElemType x)
{
if ((Q.rear + 1) % MaxSize == Q.front)
{
return false;//队列满了
}
Q.data[Q.rear] = x;
Q.rear = (Q.rear + 1) % MaxSize;//向后移动一格
return true;
}
//出队
bool DeQueue(SqQueue& Q, ElemType& x)
{
if (isEmpty)
{
return false;
}
x = Q.data[Q.front];
Q.front = (Q.front + 1) % MaxSize;
return true;
}
int main()
{
SqQueue Q;
bool ret;//存储返回值
ElemType element;//存储出队元素
InitQueue(Q);//初始化循环队列
ret = isEmpty(Q);
if (ret)
{
printf("队列不为空\n");
}
else
{
printf("队列为空\n");
}
EnQueue(Q, 3);
EnQueue(Q, 4);
EnQueue(Q, 5);
ret = EnQueue(Q, 6);
ret = EnQueue(Q, 7);
if (ret)
{
printf("入队成功\n");
}
else
{
printf("入队失败\n");
}
ret = DeQueue(Q, element);
if (ret)
{
printf("出队成功\n");
}
else
{
printf("出队失败\n");
}
ret = DeQueue(Q, element);
if (ret)
{
printf("出队成功\n");
}
else
{
printf("出队失败\n");
}
return 0;
}
3.3 队列的链式存储
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
typedef int ElemType;
typedef struct LinkNode {
ElemType data;
struct LinkNode* next;
}LinkNode;
typedef struct {
LinkNode* front, * rear;
}LinkQueue;
void InitQueue(LinkQueue& Q)
{
Q.front = Q.rear = (LinkNode*)malloc(sizeof(LinkNode));//头和尾指向同一个结点
Q.front->next = NULL;//头结点的next指针为NULL
}
//入队,尾部插入法
void EnQueue(LinkQueue& Q, ElemType x)
{
LinkNode* s = (LinkNode*)malloc(sizeof(LinkNode));
s->data = x; s-> next= NULL;
Q.rear->next = s;//rear始终指向尾部
Q.rear = s;
}
//出队 头部删除法
bool DeQueue(LinkQueue& Q, ElemType& x)
{
if (Q.rear == Q.front) return false;//队列为空
LinkNode* p = Q.front->next;//头结点什么都没存,所以头结点的下一个结点才有数据
x = p->data;
Q.rear->next = p->next;//断链
if (Q.rear == p)
Q.rear = Q.front;
free(p);
return true;
}
int main()
{
LinkQueue Q;
bool ret;
ElemType element;//存储出队元素
InitQueue(Q);
EnQueue(Q, 3);
EnQueue(Q, 4);
EnQueue(Q, 5);
EnQueue(Q, 6);
EnQueue(Q, 7);
ret = DeQueue(Q, element);
if (ret)
{
printf("出队成功,元素值为%d\n", element);
}
else
{
printf("出队不成功\n");
}
}
3.4 斐波那契数列
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
int Fib(int n)
{
if (n == 0)
{
return 0;
}
else if (n == 1)
{
return 1;
}
else
{
return Fib(n - 1) + Fib(n - 2);
}
}
int main()
{
int num;
while (scanf("%d", &num) != EOF)
{
printf("Fib(%d)=%d\n", num, Fib(num));
}
return 0;
}