1. 程式人生 > >數據結構C語言-棧的順序表結構與鏈表結構

數據結構C語言-棧的順序表結構與鏈表結構

順序棧 尾指針 onclick 順序 是否為空 script eem tac lib

技術分享圖片
 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 }
mian.c 技術分享圖片
 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
Element.h 技術分享圖片
 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
LinkedStack.h 技術分享圖片
 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 }
LinkedStack.c 技術分享圖片
 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
SeqStack.h 技術分享圖片
 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 }
SqeStack.c 技術分享圖片
 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
SeqQueue.h 技術分享圖片
 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 }
SeqQueue.c

數據結構C語言-棧的順序表結構與鏈表結構