標籤

bat (54) 作品 (41) python (24) shell (17) windows (11) 虛擬機 (11) php (10) CPP (6) KMS (6) 程式設計 (6) docker (5) 使用教學 (5) xoops (4) 公文 (4) Apache2 (3) Excel (3) juniper (3) 資料庫 (3) 轉檔 (3) mysql (2) 免動手 (2) 資料結構 (2) 軟體廣播 (2) 電腦維修 (2) Android Studio (1) Apple IPAD管理 (1) Arduino (1) CSS (1) LAMP (1) NAS (1) Ubuntu (1) VHD (1) Windows Server (1) 原因 (1) 程式應用 (1) 程式積木 (1) 編輯器 (1) 雲端硬碟 (1)

2021年3月4日 星期四

CPP關於資料結構Stack堆疊的資料

         目前發現CPP對於理解資料結構,真的很好用。此外,還有很多文章能夠促進理解資料結構。因此,闢一個文章,來收集各家對資料結構Stack的解說。如果可以,甚至節錄程式碼。



一、用陣列Array 來寫
01檔名:StackArray01.cpp
內容:
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#define MAX_SIZE 10
void push(int);
void pop(void);
int stack[MAX_SIZE];
int top = 0;
using namespace std;
int main(void) {
    push(0);
    push(1);
    push(2);
    pop();
    pop();
    push(3);
    push(4);
    pop();
    pop();
    pop();
    push(5);
    push(6);
    push(7);
    pop();
    pop();
    pop();

    system("pause");
}
void push(int data) {
if (top == MAX_SIZE) {
cout <<"The Stack is full "<<endl;
} else {
stack[top]=data;
top++;
}
}
void pop(void) {
if (top == 0) {
cout <<"The Stack is empty "<<endl;
} else {
cout <<stack[--top]<<endl;
}
}
輸出結果

02檔名:StackArray02.cpp
內容:
#include <iostream>
using namespace std;

int stack[105],stack_top;
int main() {
string cmd;
int i,j;
stack_top = 0;
cout <<"When the command is 'push 3' We push 3 into the stack!\n";
cout <<"When the command is 'pop' ,we get a element from the stack!\n";
cout <<"When the command is 'All' ,we will get all elements of the stack!\n";
cout <<"When the command is 'exit' ,we will quit the program!\n";
while(cin >> cmd) {
if(cmd == "push") {
cin >> i;
stack[stack_top]=i;
cout <<"stack["<<stack_top<<"]= "<<stack[stack_top]<<endl;
stack_top++;
} else if (cmd == "pop"){
if(stack_top==0) {
cout << "pop: nothing in stack"<<endl;
} else {
cout << "pop: " <<stack[stack_top-1] <<endl;
stack_top--;
}
}
if(cmd == "exit") {
break;
}
if(cmd == "All") {
   for(j=stack_top-1;j>=0;j--) {
    cout <<"stack["<<j<<"]="<<stack[j]<<endl;
   }
}
}
return 0;
}
03檔名:StackArray03.cpp
內容:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
typedef struct stack {
int top;
int maxTop;
int *data;
}stack_t;
void create_stack(stack_t *st,int size) {
st->top=-1;
st->maxTop=size-1;
st->data=(int *)malloc(sizeof(int)*size);
}
int isEmpty(stack_t *st) {
if (st->top==-1) {
return 1;
}
return 0;
}
int isFull(stack_t *st) {
if (st->top==st->maxTop) {
return 1;
}
return 0;
}
void push(stack_t *st,int num) {
if (isFull(st)==1) {
printf("The stack is full.\n");
} else {
st->top++;
st->data[st->top] = num;
//printf(" st->top is %d\t",st->top);
//printf("st->data[st->top] is %d\n",st->data[st->top]);
}
}
int pop(stack_t *st) {
int TempInt;
if (isEmpty(st)==1) {
printf("The stack is empty.\n");
} else {
TempInt = (st->data[st->top]);
st->top--;
}
return TempInt;
}

void print(stack_t *st) {
if (isEmpty(st)==1) {
printf("The stack is empty.\n");
} else {
for(int i=0;i<=st->top;i++) {
printf(" %d",st->data[i]);
}
printf("\n");
}
}

int main() {
stack_t *s = (stack_t *)malloc(sizeof(stack_t));
printf("Create a stack which size is 5.\n");
create_stack(s,5);
printf("Push:1,2,3,4,5\n");
for(int i=1;i<6;i++) {
push(s,i);
}
print(s);
printf("1st pop,the top is %d.\n",pop(s));
print(s);
printf("2nd pop,the top is %d.\n",pop(s));
print(s);
return 0;
}

04檔名:StackArray04.cpp
內容:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h> 
#define MAX 10
void push_f(void);
void pop_f(void);
void list_f(void);
char item[MAX][20];
int top = -1;

int main() {
char option;
while(1) {
printf("\n ----------------------------------\n");
printf(" (1) insert (push)\n");
printf(" (2) delete (pop)\n");
printf(" (3) list\n");
printf(" (4) quit\n");
printf("\n ----------------------------------\n");
printf(" Please enter your choice...");
option = getche();
switch(option) {
case '1':
push_f();
break;
case '2':
pop_f();
break;
case '3':
list_f();
break;
case '4':
printf("\n");
return 0;
}
}
}
void push_f(void) {
if (top>=MAX-1) {
printf("\nStack is full !\n");
} else {
top++;
printf("\n Please enter item to insert: ");
gets(item[top]);
}
}
void pop_f(void) {
if (top<0) {
printf("\nNo item,stack is empty!\n");
} else {
printf("\n Item %s deleted \n",item[top]);
top--;
}
}
void list_f(void) {
int count = 0,i;
if (top < 0) {
printf("\nNo item,stack is empty!\n");
} else {
printf("\n Item\n");
printf("---------------------\n");
for(i=0;i<=top;i++) {
printf(" %-20s\n",item[i]);
count++;
if(count % 20 ==0) getch();
}
printf("---------------------\n");
printf("Total item: %d\n",count);
getch();
}
}


二、用鏈結串列LinkedList 來寫
檔名:StackLinkedList001.cpp
內容:

題目練習



沒有留言:

張貼留言

只要點兩下,就能夠將InputAndOutput資料夾底下的子子孫孫資料夾內所有Word通通轉成PDF

  系列文章: 1. 只要點兩下,就能將一堆的Doc與Docx 轉成 PDF 1. https://skjhcreator.blogspot.com/2023/05/docdocx-pdf.html 2. 只要點兩下,就能將一堆的JPG轉成一個PDF,並以JPG所在的資料夾名稱為...