stack
, một biến capacity
để lưu kích thước(sức chứa) của stack và một biến top
để lưu chỉ số của phần tử ở top của Stack stack
.
stack
hiện tại đã đầy hay chưa. Nếu chỉ số top
của stack
đang bằng với capacity - 1
, tức stack
đã đầy.
bool IsFull(int capacity){ if(top >= capacity - 1){ return true; }else{ return false; } }
bool IsEmpty(){ if(top == -1){ return true; }else{ return false; } }
void Push(int stack[], int value, int capacity){ if(IsFull(capacity) == true){ printf("\nStack is full. Overflow condition!"); }else{ ++top; stack[top] = value; } }
void Pop(){ if(IsEmpty() == true){ printf("\nStack is empty. Underflow condition!"); }else{ --top; } }
int Top(int stack[]){ return stack[top]; }
top
lưu chỉ số lớn nhất của stack. Như vậy, việc lấy size của stack cực kỳ đơn giản:
int Size(){ return top + 1; }Và cuối cùng, chúng ta sẽ có 1 chương trình cài đặt stack hoàn thiện như sau:
#include <stdio.h> int top = -1; bool IsFull(int capacity){ if(top >= capacity - 1){ return true; }else{ return false; } } bool IsEmpty(){ if(top == -1){ return true; }else{ return false; } } void Push(int stack[], int value, int capacity){ if(IsFull(capacity) == true){ printf("\nStack is full. Overflow condition!"); }else{ ++top; stack[top] = value; } } void Pop(){ if(IsEmpty() == true){ printf("\nStack is empty. Underflow condition!"); }else{ --top; } } int Top(int stack[]){ return stack[top]; } int Size(){ return top + 1; } int main(){ int capacity = 3; int top = -1; int stack[capacity]; // pushing element 5 in the stack . Push(stack, 5, capacity); printf("\nCurrent size of stack is %d", Size()); Push(stack, 10, capacity); Push(stack, 24, capacity); printf("\nCurrent size of stack is %d", Size()); // As the stack is full, further pushing will show an overflow condition. Push(stack, 12, capacity); //Accessing the top element printf("\nThe current top element in stack is %d", Top(stack)); //Removing all the elements from the stack for(int i = 0 ; i < 3;i++) Pop(); printf("\nCurrent size of stack is %d", Size()); //As the stack is empty , further popping will show an underflow condition. Pop(); }Kết quả chạy chương trình trên:
Current size of stack is 1 Current size of stack is 3 Stack is full. Overflow condition! The current top element in stack is 24 Current size of stack is 0 Stack is empty. Underflow condition!
#include <stdio.h> int top; void check (char str[ ], int n, char stack [ ]) { for(int i = 0 ; i < n ; i++ ) { if (str [ i ] == '(') { top = top + 1; stack[ top ] = '('; } if(str[ i ] == ')' ) { if(top == -1 ) { top = top -1 ; break ; } else { top = top -1 ; } } } if(top == -1) printf("String is balanced!\n"); else printf("String is unbalanced!\n"); } int main ( ) { //balanced parenthesis string. char str[ ] = "(())()"; // unbalanced string . char str1 [] = "((()"; char stack [15] ; top = -1; check (str , 9 , stack ); //Passing balanced string top = -1 ; check(str1 , 5 , stack) ; //Passing unbalanced string return 0; }Kết quả
String is balanced! String is unbalanced!
#include <stdio.h> #include <stdlib.h> #include<limits.h> typedef struct Stack { int top; unsigned capacity; int* array; }Stack; Stack* createStack(unsigned capacity) { struct Stack* stack = (Stack*) malloc(sizeof(Stack)); stack->capacity = capacity; stack->top = -1; stack->array = (int*) malloc(stack->capacity * sizeof(int)); return stack; } int Full(Stack* stack) { return stack->top == stack->capacity - 1; } int Empty(Stack* stack) { return stack->top == -1; } void push(Stack* stack, int item) { if (Full(stack)) return; stack->array[++stack->top] = item; printf("%d pushed to stack\n", item); } int pop(Stack* stack) { if (Empty(stack)) return INT_MIN; return stack->array[stack->top--]; } int peek(Stack* stack) { if (Empty(stack)) return INT_MIN; return stack->array[stack->top]; } int main() { Stack* stack = createStack(100); push(stack, 14); push(stack, 25); push(stack, 38); push(stack, 48); printf("%d popped from stack\n", pop(stack)); printf("Top item is %d\n", peek(stack)); return 0; } //output /*14 pushed to stack 25 pushed to stack 38 pushed to stack 48 pushed to stack 48 popped from stack Top item is 38*/
#define Maxsize 10 #include<iostream> using namespace std; class stacks { public: int Top=-1; int s[10]; void push(int); void pop(); void print(); }; void stacks::push(int x) { if(Top==Maxsize) { cout<<"Error : cannot insert"; } else { Top++; s[Top]=x; } } void stacks::pop() { if(Top<0) { cout<<"Error : cannot delete"; } else { Top--; return; } } void stacks::print() { int i; cout<<"Stack:"; for(i=Top;i>=0;i--) { cout<<s[i]; } } int main() { stacks o; int e,i; for(i=0;i<5;i++) { cin>>e; o.push(e); } //o.pop(); o.print(); }
#include <iostream> using namespace std; struct Stack { int data; Stack *next; } *TOS = NULL; void Push(int value) { Stack *new_node = new Stack(); new_node -> data = value; new_node -> next = TOS; TOS = new_node; } void Pop() { if(TOS == NULL) cout << "Stack is empty" << endl; else { Stack *temp; temp = TOS; TOS = TOS -> next; temp -> next = NULL; delete temp; } } void Print_Stack() { if(TOS == NULL) cout << "Stack is empty" << endl; else { cout << "Top of stack is " << TOS -> data << endl; if(TOS -> next != NULL) { cout << "Other Elements are : "; Stack *current = TOS -> next; while(current) { cout << current -> data << " "; current = current -> next; } cout << endl; } } } int main() { for(int i = 0; i < 5; i++) Push(i); Print_Stack(); for(int i = 0; i < 6; i++) Pop(); Print_Stack(); return 0; } /* Output Top of stack is 4 Other Elements are : 3 2 1 0 Stack is empty Stack is empty */
#include <iostream> #include <stack> int main () { std::stack<int> mystack; for (int i=0; i<5; ++i) mystack.push(i); std::cout << "Popping out elements..."; while (!mystack.empty()) { std::cout << ' ' << mystack.top(); mystack.pop(); } std::cout << '\n'; return 0; }
Cùng nhau học tập, khám phá các kiến thức nền tảng về Lập trình web, mobile, database nhé.
Nền tảng kiến thức - Hành trang tới tương lai hân hạnh phục vụ Quý khách!
Khám phá, trải nghiệm ngay
Vui lòng đăng nhập để gởi bình luận!
Đăng nhậpChưa có bình luận nào!