【C++】【七】栈的实现
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                【C++】【七】栈的实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                棧的線性表實現
stack_liner_stack.h
#ifndef STACK_LINER_H
#define STACK_LINER_H
#include <stdlib.h>
#define MAX_SIZE 1024
#define stack_liner_false 0
#define stack_liner_true 1typedef struct STACK_LINER_H {void* data[MAX_SIZE];int size;
}stack_liner;stack_liner* Init_stack_liner();void Push_stack_liner(stack_liner* stack, void* data);void* Top_stack_liner(stack_liner* stack);void Pop_stack_liner(stack_liner* stack);int IsEmpty_stack_liner(stack_liner* stack);int Size_stack_liner(stack_liner* stack);void Clear_stack_liner(stack_liner* stack);void Free_stack_liner(stack_liner* stack);#endif 
stack_liner_stack.cc
?
#include "stack_linerlist.h"stack_liner* Init_stack_liner()
{stack_liner* stack = (stack_liner*)malloc(sizeof(stack_liner));for (int i = 0; i < MAX_SIZE; ++i) {stack->size = 0;}return stack;
}void Push_stack_liner(stack_liner* stack, void* data)
{if (stack == NULL) {return;}if (stack->size == MAX_SIZE) {return;}if (data == NULL) {return;}stack->data[stack->size] = data;stack->size++;}void* Top_stack_liner(stack_liner* stack)
{if (stack == NULL) {return NULL;}if (stack->size == 0) {return NULL;}return stack->data[stack->size-1];
}void Pop_stack_liner(stack_liner* stack)
{if (stack == NULL) {return;}if (stack->size == 0) {return;}stack->data[stack->size-1] = NULL;stack->size--;
}int IsEmpty_stack_liner(stack_liner* stack)
{if (stack == NULL) {return -1;}if (stack->size == 0) {return stack_liner_true;}return stack_liner_false;
}int Size_stack_liner(stack_liner* stack)
{return stack->size;
}void Clear_stack_liner(stack_liner* stack)
{if (stack == NULL) {return;}for (int i = 0; i < stack->size; i++) {stack->data[i] = NULL;}stack->size = 0;
}void Free_stack_liner(stack_liner* stack)
{if (stack != NULL) {return;}free(stack);
}
 
main.cc
#include <iostream>
#include<stdlib.h>
#include"stack_linerlist.h"
#include<string.h>typedef struct PERSON {char name[64];int age;
}person;
int main()
{stack_liner* stack = Init_stack_liner();person p1 = { "aaa",10 };person p2 = { "bbb",20 };person p3 = { "ccc",30 };person p4 = { "ddd",40 };person p5 = { "eee",50 };Push_stack_liner(stack, &p1);Push_stack_liner(stack, &p2);Push_stack_liner(stack, &p3);Push_stack_liner(stack, &p4);Push_stack_liner(stack, &p5);while (Size_stack_liner(stack) > 0) {PERSON* px = (PERSON*)Top_stack_liner(stack);printf("name = %s, age=%d\n ", px->name, px->age);Pop_stack_liner(stack);}Free_stack_liner(stack);system("pause");return 0;
} 
棧的鏈表實現:
stack_list.h
#ifndef STACK_LIST
#define STACK_LIST
#include<stdlib.h>typedef struct LINKNODE {struct LINKNODE* next;
}linknode;typedef struct LINKLIST {linknode head;int size;
}stack_list;stack_list* Inin_stack_list();void Push_stack_list(stack_list* stack, linknode* data);void Pop_stack_list(stack_list* stack);linknode* Top_stack_list(stack_list* stack);int Size_stack_list(stack_list* stack);void Clear_stack_list(stack_list* stack);void Free_stack_list(stack_list* stack);#endif // !STACK_LIST
 
?stack_list.cc
#include "stack_list.h"stack_list* Inin_stack_list()
{stack_list* stack = (stack_list*)malloc(sizeof(stack_list));stack->head.next = NULL;stack->size = 0;return stack;
}void Push_stack_list(stack_list* stack, linknode* data)
{if (stack == NULL) {return;}if (data == NULL) {return;}data->next = stack->head.next;stack->head.next = data;stack->size++;
}void Pop_stack_list(stack_list* stack)
{if (stack == NULL) {return;}if (stack->size == 0) {return;}linknode* pnext = stack->head.next;stack->head.next = pnext->next;stack->size--;
}linknode* Top_stack_list(stack_list* stack)
{if (stack == NULL) {return NULL;}if (stack->size == 0) {return NULL;}return stack->head.next; 
}int Size_stack_list(stack_list* stack)
{if (stack == NULL) {return -1;}return stack->size;
}void Clear_stack_list(stack_list* stack)
{if (stack == NULL) {return;}stack->head.next = NULL;stack->size = 0;
}void Free_stack_list(stack_list* stack)
{if (stack == NULL) {return;}free(stack);
}
 
main.cc
#include <iostream>
#include<stdlib.h>
#include<string.h>
#include"stack_list.h"typedef struct PERSON {linknode node;char name[64];int age;
}person;int main() {stack_list* stack = Inin_stack_list();person p1, p2, p3, p4, p5;strcpy_s(p1.name, "aaa");strcpy_s(p2.name, "bbb");strcpy_s(p3.name, "ccc");strcpy_s(p4.name, "ddd");strcpy_s(p5.name, "eee");p1.age = 10;p2.age = 20;p3.age = 30;p4.age = 40;p5.age = 50;Push_stack_list(stack, (linknode*)&p1);Push_stack_list(stack, (linknode*)&p2);Push_stack_list(stack, (linknode*)&p3);Push_stack_list(stack, (linknode*)&p4);Push_stack_list(stack, (linknode*)&p5);while (Size_stack_list(stack) > 0) {person* px=(person*)Top_stack_list(stack);printf("name=%s,  age=%d\n", px->name, px->age);Pop_stack_list(stack);}Free_stack_list(stack);system("pause");return 0;
} 
 ?
?
總結
以上是生活随笔為你收集整理的【C++】【七】栈的实现的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: 哈利波特魔法觉醒卡牌怎么升级?
 - 下一篇: 【C++】【九】栈的应用