1 #include <stdio.h> 2 #include <stdlib.h> 3 #include "SeqStack.h" 4 #include "LinkedStack.h" 5 #include "SeqQueue.h" 6 7 ElementType datas[] = { 8 {1,"维京战机"}, 9 {2,"光子炮台"}, 10 {3,"一方通行"}, 11 {4,"番外个体"}, 12 {5,"上条当麻"}, 13 }; 14 void TestSeqStack(); 15 void TestLinkedStack(); 16 void TestSeqQueue(); 17 18 int main() 19 { 20 //TestSeqStack(); 21 //TestLinkedStack(); 22 TestSeqQueue(); 23 return 0; 24 } 25 26 void TestSeqStack() 27 { 28 SeqStack * stack = (SeqStack*)malloc(sizeof(SeqStack)); 29 ElementType * element = (ElementType*)malloc(sizeof(ElementType)); 30 InitSeqStack(stack); 31 printf("\n初始化后:\n"); 32 PrintSeqStack(stack); 33 for(int i = 0;i < 5;i++) 34 { 35 printf("\n当前入栈:%d\t%s\n",datas[i].id,datas[i].name); 36 PushSeqStack(stack,datas[i]); 37 } 38 printf("\n入栈后:\n"); 39 PrintSeqStack(stack); 40 PopSeqStack(stack,element); 41 printf("\n当前出栈元素:\n%d\t%s\n",element -> id,element -> name); 42 43 printf("\n出栈后:\n"); 44 PrintSeqStack(stack); 45 free(stack); 46 } 47 48 void TestLinkedStack() 49 { 50 LinkedStack * stack = (LinkedStack*)malloc(sizeof(LinkedStack)); 51 InitLinkedStack(stack); 52 printf("压栈后\n"); 53 for(int i = 0;i < 5;i++) 54 { 55 PushLinkedStack(stack,datas[i]); 56 } 57 PrintLinkedStack(stack); 58 59 printf("出栈后\n"); 60 ElementType * element = (ElementType*)malloc(sizeof(ElementType)); 61 PopLinkedStack(stack,element); 62 PrintLinkedStack(stack); 63 printf("\n当前出栈元素:\n%d\t%s\n",element -> id,element -> name); 64 65 ClearLinkedStack(stack); 66 PrintLinkedStack(stack); 67 68 free(stack); 69 } 70 void TestSeqQueue() 71 { 72 SeqQueue seq; 73 InitSeqQueue(&seq); 74 for(int i = 0;i < 5;i++) 75 { 76 OfferSeqQueue(&seq,datas[i]); 77 } 78 for(int i = 0;i < seq.length ;i++) 79 { 80 printf("当前队列:%d\t%s\n",seq.data[i].id,seq.data[i].name); 81 } 82 }
1 #ifndef ELEMENT_H_INCLUDED 2 #define ELEMENT_H_INCLUDED 3 #define TRUE 0 4 #define FALSE 1 5 #define MAX_SIZE 10 6 7 typedef struct{ 8 int id; 9 char * name; 10 }ElementType; 11 12 #endif // ELEMENT_H_INCLUDED
1 #ifndef LINKEDSTACK_H_INCLUDED 2 #define LINKEDSTACK_H_INCLUDED 3 4 /***************************** 5 *project :数据结构第三章案例 6 *function :定义栈链数据以及常用操作 7 *Description: 8 *Author :中子 9 ***************************** 10 *copyright:2019.2.20 by UZT 11 ****************************/ 12 13 #include <stdio.h> 14 #include <stdlib.h> 15 #include "Element.h" 16 17 typedef struct StackNode{ 18 ElementType data; //数据域 19 struct StackNode * next;//指针域 20 }StackNode; 21 22 typedef struct LinkedStack{ 23 StackNode * top; 24 int length; 25 }LinkedStack; 26 27 /** 初始化链栈 */ 28 void InitLinkedStack(LinkedStack * linkedStack); 29 30 /** 链栈压栈*/ 31 int PushLinkedStack(LinkedStack * linkedstack,ElementType element); 32 33 /** 打印链栈 */ 34 void PrintLinkedStack(LinkedStack * linkedStack); 35 36 /** 链栈出栈 */ 37 int PopLinkedStack(LinkedStack * linkedstack,ElementType * element); 38 39 /** 清空栈 */ 40 void ClearLinkedStack(LinkedStack * linkedStack); 41 42 /** 销毁栈 */ 43 void DestroyLinkedStack(LinkedStack * linkedStack); 44 45 #endif // LINKEDSTACK_H_INCLUDED
1 #include "LinkedStack.h" 2 3 void InitLinkedStack(LinkedStack * linkedStack) 4 { 5 linkedStack -> top = NULL; 6 linkedStack -> length = 0; 7 } 8 9 int PushLinkedStack(LinkedStack * linkedStack,ElementType element) 10 { 11 StackNode * NewNode = (StackNode*)malloc(sizeof(StackNode)); 12 NewNode -> data = element; 13 14 NewNode -> next = linkedStack -> top; 15 linkedStack -> top = NewNode; 16 linkedStack -> length ++; 17 return TRUE; 18 } 19 20 void PrintLinkedStack(LinkedStack * linkedStack) 21 { 22 if(!linkedStack -> top || linkedStack -> length == 0) 23 { 24 printf("栈链为空!"); 25 return; 26 } 27 StackNode * node; 28 node = linkedStack -> top; 29 for(int i = 0;i < linkedStack -> length;i++) 30 { 31 printf("%d\t%s\n",node -> data.id,node -> data.name); 32 node = node -> next; 33 } 34 } 35 36 int PopLinkedStack(LinkedStack * linkedStack,ElementType * element) 37 { 38 if(!linkedStack -> top) 39 { 40 printf("空栈,出栈操作失败!"); 41 return FALSE; 42 } 43 StackNode * node; 44 node = linkedStack -> top; 45 *element = node -> data; 46 47 linkedStack -> top = node -> next; 48 linkedStack -> length--; 49 free(node); 50 return TRUE; 51 } 52 53 void ClearLinkedStack(LinkedStack * linkedStack) 54 { 55 StackNode * node; 56 while(linkedStack -> top) 57 { 58 node = linkedStack -> top; 59 linkedStack -> top = linkedStack -> top -> next; 60 linkedStack -> length--; 61 free(node); 62 } 63 } 64 65 void DestroyLinkedStack(LinkedStack * linkedStack) 66 { 67 //先清空,再销毁 68 ClearLinkedStack(linkedStack); 69 free(linkedStack); 70 linkedStack = NULL; 71 }
1 #ifndef SEQSTACK_H_INCLUDED 2 #define SEQSTACK_H_INCLUDED 3 4 /***************************** 5 *project :数据结构第三章栈与队列实例 6 *function :定义顺序栈结构 7 *Description: 8 *Author :中子 9 ***************************** 10 *copyright:2019.2.19 by UZT 11 ****************************/ 12 13 #include <stdio.h> 14 #include <stdlib.h> 15 #include "Element.h" 16 17 typedef struct SeqStack{ 18 ElementType elements[MAX_SIZE];//顺序栈中用来存放数据元素的数组 19 int top; //栈顶(数组中的下标),如果top = -1证明栈为空 20 int length; //当前栈的元素个数 21 }SeqStack; 22 23 /** 初始化栈 */ 24 void InitSeqStack(SeqStack * seqStack); 25 26 /** 向栈中压入元素返回压入结果(TRUE/Fals) */ 27 int PushSeqStack(SeqStack * seqStack,ElementType element); 28 29 /** 出栈 */ 30 int PopSeqStack(SeqStack * seqStack,ElementType * element); 31 32 /** 清空栈 */ 33 void ClearSeqStack(SeqStack * seqStack); 34 35 /** 返回栈顶元素 */ 36 void PoSeqStack(SeqStack * seqStack,ElementType * element); 37 38 /** 打印当前栈内元素 */ 39 void PrintSeqStack(SeqStack * seqStack); 40 41 #endif // SEQSTACK_H_INCLUDED
1 #include "SeqStack.h" 2 3 /** 初始化栈 */ 4 void InitSeqStack(SeqStack * seqStack) 5 { 6 seqStack -> top = -1; //栈顶指向-1的下标 7 seqStack -> length = 0; //长度为0 8 } 9 10 /** 向栈中压入元素返回压入结果(TRUE/Fals) */ 11 int PushSeqStack(SeqStack * seqStack,ElementType element) 12 { 13 if(seqStack -> top == MAX_SIZE - 1) 14 { 15 printf("满栈,压栈操作失败!"); 16 return FALSE; 17 } 18 seqStack -> top ++; 19 seqStack -> elements[seqStack -> top] = element; 20 seqStack -> length++; 21 return TRUE; 22 } 23 24 /** 出栈 */ 25 int PopSeqStack(SeqStack * seqStack,ElementType * element) 26 { 27 if(seqStack -> top == -1) 28 { 29 printf("空栈,出栈失败!\n"); 30 return FALSE; 31 } 32 //返回栈顶指向元素 33 *element = seqStack -> elements[seqStack -> top]; 34 seqStack -> top--; 35 seqStack -> length--; 36 return TRUE; 37 } 38 39 /** 清空栈 */ 40 void ClearSeqStack(SeqStack * seqStack) 41 { 42 seqStack -> top = -1; 43 seqStack -> length = 0; 44 } 45 46 /** 返回栈顶元素 */ 47 void PoSeqStack(SeqStack * seqStack,ElementType * element) 48 { 49 if(seqStack -> top == -1) 50 { 51 printf("空栈,栈顶元素为空!\n"); 52 element = NULL; 53 return; 54 } 55 *element = seqStack -> elements[seqStack -> top]; 56 } 57 58 /** 打印当前栈内元素 */ 59 void PrintSeqStack(SeqStack * seqStack) 60 { 61 if(seqStack -> top == -1) 62 { 63 printf("空栈,无元素!\n"); 64 return; 65 } 66 printf("\n当前栈内元素有:\n"); 67 for(int i = 0;i < seqStack -> length;i++) 68 { 69 printf("\n%d\t%s\n",seqStack -> elements[i].id,seqStack -> elements[i].name); 70 } 71 }
1 #ifndef SEQQUEUE_H_INCLUDED 2 #define SEQQUEUE_H_INCLUDED 3 #define STATE_OK 1 4 #define STATE_FALSE -1 5 6 /***************************** 7 *project :数据结构第三章案例 8 *function :循环队列就是队列头尾相接的顺序结构 9 *Description:数组未满时都可以插入新的队列元素 10 *Author :中子 11 ***************************** 12 *copyright:2019.2.26 by UZT 13 ****************************/ 14 15 #include "Element.h" 16 17 typedef int State; 18 19 typedef struct{ 20 ElementType data[MAX_SIZE]; 21 int front; //队头指针 22 int rear; //对尾指针 23 int length; 24 }SeqQueue; 25 /**初始化*/ 26 void InitSeqQueue(SeqQueue * seqQueue); 27 /**是否为空*/ 28 State IsSeqQueueEmpty(SeqQueue * seqQueue); 29 /**是否为满*/ 30 State IsSeqQueueFull(SeqQueue * seqQueue); 31 /**入队*/ 32 State OfferSeqQueue(SeqQueue * seqQueue,ElementType element); 33 /**出队*/ 34 State PollSeqQueue(SeqQueue * seqQueue,ElementType * element); 35 36 #endif // SEQQUEUE_H_INCLUDED
1 #include "SeqQueue.h" 2 #include <stdio.h> 3 #include <stdlib.h> 4 5 void InitSeqQueue(SeqQueue * seqQueue) 6 { 7 if(!seqQueue) 8 { 9 seqQueue = (SeqQueue*)malloc(sizeof(SeqQueue)); 10 } 11 seqQueue -> length = 0; 12 seqQueue -> front = 0; 13 seqQueue -> rear = 0; 14 } 15 16 State IsSeqQueueEmpty(SeqQueue * seqQueue) 17 { 18 return seqQueue -> front == seqQueue -> rear ? TRUE : FALSE; 19 } 20 21 State IsSeqQueueFull(SeqQueue * seqQueue) 22 { 23 if((seqQueue -> rear + 1) % MAX_SIZE == seqQueue -> front) 24 { 25 return TRUE; 26 } 27 return FALSE; 28 } 29 30 State OfferSeqQueue(SeqQueue * seqQueue,ElementType element) 31 { 32 if(IsSeqQueueFull(seqQueue) == TRUE) 33 { 34 return STATE_FALSE; 35 } 36 seqQueue -> data[seqQueue -> rear] = element; 37 seqQueue -> rear = (seqQueue -> rear + 1) % MAX_SIZE; 38 seqQueue -> length ++; 39 return STATE_OK; 40 } 41 42 State PollSeqQueue(SeqQueue * seqQueue,ElementType * element) 43 { 44 if(IsSeqQueueEmpty(seqQueue) == TRUE) 45 { 46 return STATE_FALSE; 47 } 48 *element = seqQueue -> data[seqQueue -> front]; 49 seqQueue -> front = (seqQueue -> front + 1) % MAX_SIZE; 50 seqQueue -> length--; 51 return STATE_OK; 52 }
原文:https://www.cnblogs.com/cnlntr/p/10474275.html