Programming C: Posts 15 – Install stack (Stack)
Stacks (Stack) is an unordered list that allows insertion and deletion is performed at the end of the list and we call this the tip end (top) the stack
In this article we learn how to install and on Stack on the array pointer
Content
1. Stack installed on the array
#define Max 100 //so phan tu toi da cua Stack typedef int item; //kieu du lieu cua Stack struct Stack { int Top; //Dinh Top item Data[Max]; //Mang cac phan tu };
1.1 Initialize empty list, check list is empty, full
void Init (Stack &S) //khoi tao Stack rong { S.Top = 0; //Stack rong khi Top la 0 } int Isempty(Stack S) //kiem tra Stack rong { return (S.Top == 0); } int Isfull(Stack S) //kiem tra Stack day { return (S.Top == Max); // }
1.2 Add elements to Stack (Push)
To insert element inserted into one location Stack Top, Top up and Funeral 1 unit
void Push(Stack &S, item x) //them phan tu vao Stack { if (!Isfull(S)) { S.Data[S.Top] = x; //Gan du lieu S.Top ++; //Tang Top len 1 } }
1.3 Get data on Top but not deleted (Peak)
int Peak(Stack S) //Lay phan tu o dau Stack nhung khong xoa { return S.Data[S.Top-1]; //Lay du lieu tai Top }
1.4 Remove and retrieve data at Top (Pop)
int Pop(Stack &S) //Loai bo phan tu khoi Stack { if (!Isempty(S)) { S.Top --; //Giam Top return S.Data[S.Top]; //Lay du lieu tai Top } }
1.5 Code entire program (full)
#include <stdlib.h> #include <stdio.h> #define Max 100 //so phan tu toi da cua Stack typedef int item; //kieu du lieu cua Stack struct Stack { int Top; //Dinh Top item Data[Max]; //Mang cac phan tu }; void Init (Stack &S); //khoi tao Stack rong int Isempty(Stack S); //kiem tra Stack rong int Isfull(Stack S); //kiem tra Stack day void Push(Stack &S, item x); //them phan tu vao Stack int Peak(Stack S); //Lay phan tu o dau Stack nhung khong xoa int Pop(Stack &S); //Loai bo phan tu khoi Stack void Input (Stack &S); //Nhap Stack void Output(Stack S); //Xuat Stack void Init (Stack &S) //khoi tao Stack rong { S.Top = 0; //Stack rong khi Top la 0 } int Isempty(Stack S) //kiem tra Stack rong { return (S.Top == 0); } int Isfull(Stack S) //kiem tra Stack day { return (S.Top == Max); // } void Push(Stack &S, item x) //them phan tu vao Stack { if (!Isfull(S)) { S.Data[S.Top] = x; //Gan du lieu S.Top ++; //Tang Top len 1 } } int Peak(Stack S) //Lay phan tu o dau Stack nhung khong xoa { return S.Data[S.Top-1]; //Lay du lieu tai Top } int Pop(Stack &S) //Loai bo phan tu khoi Stack { if (!Isempty(S)) { S.Top --; //Giam Top return S.Data[S.Top]; //Lay du lieu tai Top } } void Input (Stack &S) { int n; item x; do { printf("Nhap so phan tu cua Stack (<%d) :",Max); scanf("%d",&n); } while (n>Max || n<1); for (int i=0; i<n; i++) { printf("Nhap phan tu thu %d : ", i+1); scanf("%d",&x); Push(S,x); } } void Output(Stack S) { for (int i=S.Top-1; i>=0; i--) printf("%d ",S.Data[i]); printf("\n"); } int main() { Stack S; Init(S); Input(S); Output(S); int lua_chon; printf("Moi ban chon phep toan voi DS LKD:"); printf("\n1: Kiem tra Stack rong"); printf("\n2: Kiem tra Stack day"); printf("\n3: Them phan tu vao Stack"); printf("\n4: Xoa phan tu trong Stack"); printf("\n5: Xuat Stack"); printf("\n6: Thoat"); do { printf("\nBan chon: "); scanf("%d",&lua_chon); switch (lua_chon) { case 1: { if (Isempty(S)) printf("Stack rong !"); else printf ("Stack khong rong !"); break; } case 2: { if (Isfull(S)) printf("Stack day !"); else printf ("Stack chua day !"); break; } case 3: { item x; printf ("Nhap phan tu can chen vao DS: "); scanf("%d",&x); Push(S,x); break; } case 4: { Pop(S); break; } case 5: { Output(S); break; } case 6: break; } }while (lua_chon !=6); return 0; }
2. Stack installed on the cursor
Stack on the stack pointer is still functioning normally, but because it uses link pointer to allocate and manage, no excess or lack of anything.
2.1 Building structures
typedef int item; //kieu du lieu struct Node { item Data; //du lieu Node *Next; //link }; typedef struct Stack { Node *Top; };
2.2 Initialize empty list, check list is empty, length list
void Init (Stack &S) //khoi tao Stack rong { S.Top = NULL; } int Isempty(Stack S) //kiem tra Stack rong { return (S.Top == NULL); } int Len (Stack S) { Node *P = S.Top; int i=0; while (P != NULL) //trong khi chua het Stack thi van duyet { i++; P = P->Next; } return i; }
2.3 Create 1 Node
Node *MakeNode(item x) //tao 1 Node { Node *P = (Node*) malloc(sizeof(Node)); P->Next = NULL; P->Data = x; return P; }
2.4 Inserting elements into Stack (Push)
To insert the element into the stack pointer just for that point and Top Note, Then point it is finished Top
void Push(Stack &S, item x) //them phan tu vao Stack { Node *P = MakeNode(x); P->Next = S.Top; S.Top = P; }
2.5 Get data on Top but not deleted (Peak)
int Peak(Stack S) //Lay phan tu o dau Stack nhung khong xoa { return S.Top->Data; }
2.6 Remove and retrieve data at Top (Pop)
One need only point to the cursor position Top 2 stop.
int Pop(Stack &S) //Loai bo phan tu khoi Stack { if (!Isempty(S)) { item x = S.Top->Data; //luu lai gia tri S.Top = S.Top->Next; //Xoa phan tu Top return x; } }
2.7 Complete program (full code)
#include <stdlib.h> #include <stdio.h> typedef int item; //kieu du lieu struct Node { item Data; //du lieu Node *Next; //link }; typedef struct Stack { Node *Top; }; void Init (Stack &S); //khoi tao Stack rong int Isempty(Stack S); //kiem tra Stack rong int Len (Stack S); //Do dai Stack void Push(Stack &S, item x); //them phan tu vao Stack int Peak(Stack S); //Lay phan tu o dau Stack nhung khong xoa int Pop(Stack &S); //Loai bo phan tu khoi Stack void Input (Stack &S); //Nhap Stack void Output(Stack S); //Xuat Stack Node *MakeNode(item x); //Tao 1 Node void Init (Stack &S) //khoi tao Stack rong { S.Top = NULL; } int Isempty(Stack S) //kiem tra Stack rong { return (S.Top == NULL); } int Len (Stack S) { Node *P = S.Top; int i=0; while (P != NULL) //trong khi chua het Stack thi van duyet { i++; P = P->Next; } return i; } Node *MakeNode(item x) //tao 1 Node { Node *P = (Node*) malloc(sizeof(Node)); P->Next = NULL; P->Data = x; return P; } void Push(Stack &S, item x) //them phan tu vao Stack { Node *P = MakeNode(x); P->Next = S.Top; S.Top = P; } int Peak(Stack S) //Lay phan tu o dau Stack nhung khong xoa { return S.Top->Data; } int Pop(Stack &S) //Loai bo phan tu khoi Stack { if (!Isempty(S)) { item x = S.Top->Data; //luu lai gia tri S.Top = S.Top->Next; //Xoa phan tu Top return x; } } void Input (Stack &S) //nhap danh sach { int i=0; item x; do { i++; printf ("Nhap phan tu thu %d : ",i); scanf("%d",&x); if (x != 0) Push(S,x); } while(x != 0); //nhap 0 de ket thuc } void Output(Stack S) { Node *P = S.Top; while (P != NULL) { printf("%d ",P->Data); P = P->Next; } printf("\n"); } int main() { Stack S; Init(S); Input(S); Output(S); int lua_chon; printf("Moi ban chon phep toan voi DS LKD:"); printf("\n1: Kiem tra Stack rong"); printf("\n2: Do dai Stack"); printf("\n3: Them phan tu vao Stack"); printf("\n4: Xoa phan tu trong Stack"); printf("\n5: Xuat Stack"); printf("\n6: Thoat"); do { printf("\nBan chon: "); scanf("%d",&lua_chon); switch (lua_chon) { case 1: { if (Isempty(S)) printf("Stack rong !"); else printf ("Stack khong rong !"); break; } case 2: { printf("Do dai Stack: %d",Len(S)); break; } case 3: { item x; printf ("Nhap phan tu can chen vao DS: "); scanf("%d",&x); Push(S,x); break; } case 4: { Pop(S); break; } case 5: { Output(S); break; } case 6: break; } }while (lua_chon !=6); return 0; }
3. Using Stack is available in C
In C has built the method (jaw) related to Stack, It is only the declaration and use
#include <iostream> // io #include <stack> // use stack using namespace std; int main() { stack <int> S; // khai bao Stack voi kieu du lieu la int for (int i = 0; i < 10; i++) { S.push(i*78 + 26); // chen cac phan tu vao stack } cout << "Do dai stack: " << S.size() << "\n"; // xuat tat ca phan tu trong stack while (!S.empty()) { // trong khi stack chua trong int x = S.top(); // lay gia tri top S.pop(); // loai bo khoi stack cout << x << " "; } cout << "\n"; return 0; }
4. Examples of applications of Stack
Stack has many applications, The following is 1 application of transformation of. The following code will transfer the base 10 to base x keyboard input
#include <iostream> // io #include <stack> // use stack using namespace std; int main() { char num[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; stack <char> S; // khai bao Stack voi kieu du lieu la int int coSo, so, du; cout << "Nhap so can chuyen: "; cin >> so; cout << "Nhap co so can chuyen: "; cin >> coSo; // chuyen doi bang cach dua vao Stack while(so) { du = so % coSo; S.push(num[du]); so /= coSo; } // Xuat ra while(!S.empty()) { cout << S.top(); S.pop(); } return 0; }
[rps-include post=”2703″ shortcodes=”false”]
Hello nguyenvanquan7826,
Because you explain your fears is why such functions:
void Init (Stack &S)
int Pop(Stack &S)
void Push(Stack &S, item x)
used to obtain the address operator “&”.
Also functions as:
int Isempty(Stack S)
int Isfull(Stack S)
int Peak(Stack S)
do not use operators “&”
Thank you.
Operator & Simply say to the exit function, the variables that can change. If the function does not alter the minutes that do not need.
her little questions, Why to use
—–
typedef int item;
struct Stack
{
int Top;
item Data[Max];
};
—–
Why not use:
—–
struct Stack
{
int Top;
intData[Max];
};
—–
What effect typedef?
It is used to define a data type. its easy to use item. For example now you want your data type is int, but few today would want to switch to float, or 1 struct, Meanwhile correction will greatly, This definition, I just edit here alone.
Even better, it should build import and export function item, Such matter how correct data type is not relevant anymore, Simply enter discontinued repair is completed.
a Force for e asked ti.cai data function takes place but was then himself taken k delete data at the top, it has to return top(S.data[S.top]) why to return(S.data[S.top-1]) .He explained to e understood to be no sir?
it is the array. portions are from 0-> n-1 you. Hence the first in Stack (ie the last one in the array) was [top-1]
This question again a help e nhé: the value of the inserted element does not depend on the value of i.vay purpose of this is to use the loop to do a eh gi?a giải thích giúp e với .e không hiểu 🙁
for (int i = 0; in < 10; i ) {
S.push(i*78 + 26); // chen elements to stack
}
Ie, the values at random for you.
a Force for e hoi.vi example e have 1 ds : 2 3 4 5 6 7 8
e put th element 3 di.thi e print ds figure it is: 2 3 20 5 6 7 8
why it is rather secondary element 3 a.cach other states other values for how a???
So you're wrong code alone.
Where is the delete function nth element of e ea see Help with sir:
void pop_n(stack* s,int n)
{
int the = 1;
stackNode *p=s->top;
stackNode *temp;
int m;
if(n>only(with))
{
return;
}
else
{
while(demtop=s->top->next;
free(p);
return;
}
if(the == n)
{
temp=p->next;
free(p);
return;
}
temp=p;
p=p->next;
the ;
}
}
return;
}
What they do not know what is your demtop?
What you have written in standard C code on their stack to refer Learning is not ạ.
Your article Nguyen Van Quan save dc-tailed implementation .cpp , chứ đuôi .C thi ko chạy dc 🙁 . I was to learn standard C. hix
a to e ask s.top and s->Nothing different top ạ???
s.top is used when s is 1 struct normal
s-> top is used when the cursor s structure.
ctrinh my friend change his base why DC k run through,with code written in c k yourself vs
If you use the code above, then save the file as * .cpp
And their C code with no. 🙂 Bạn dựa vào stack đã xây dựng để code cũng được mà.
Sensei, stack stored in doubly linked list in C, how ạ?
The thing I have not done so, also never seen anyone do stack with the double bond ds. You can find all doubly linked list for more nhé. https://www.cachhoc.net/2014/12/21/lap-trinh-c-bai-14-danh-sach-lien-ket-kep/
Star took place at the top spot value :
int Peak(Stack S) //Lay Stack o dau tu phan but not cleared
{
return S.Data[S.Top-1]; //Lay du lieu tai Top
}
int Pop(Stack &S) //Molecular mass removal Stack
{
if (!Isempty(S))
{
S.Top –; //Giam Top
return S.Data[S.Top]; //Lay du lieu tai Top
}
}
why time is s.data[s.top] sometimes it is s.data[s.top-1]
For the second TH 2 was Top– Then you go
why I do the same demo, but the results came out wrong right. always check the stack also “hollow” and “wireless”.
though her for input max = 3 3 rather, they must complete. admin you have to run the test code testing parts that have not. you see their household rate bared ;in
You try to check back gradually see nhé. Maybe somewhat error. Code của mình chạy ổn rồi bạn có thể test thử code 🙂
Welcome. My friend, you can add content Peek. Ie write subroutine to take the contents of the element at the top of the stack is not it?
I have seen you write function Peak but stars that you see used it anyway?
Writing is 1 matter, full article can be used in not is another story. I keep writing to let you use only enough.
And that you yourself have.
His comments on code.
1/cái struct Stack{…. Where not need typedef
2/peak levels also need to check whether or not to use an empty stack
3/miss pop function stack is empty, it returns something, you do not cover over this case
Thank you for feedback.
Without typedef it everywhere you have to write “struct Stack” even when used typedef (define 1 datatypes) should not attract the following struct
– 2 peak function and pop it right as you say. I'll revise.
typedef struct Stack
{
Node *Top;
};
he asked me what was what, sir declaration
How struct declaration diamond ban, This struc 1 con tro Top kie Note.
he asked me why this function declaration Init (Stack &S) to declare stack &S even when declaring the function IsEmpty(Stack S) to declare stack S ạ
While there & then out of function S will retain value after change. And no & before and after the function, the value of it does not change
#12h20m 10/6/2016
Hôm nay đi thi về, gặp Stack
làm đc…. nhưng bị lỗi là output. output viết như trên ko đúng bản chất của stack…
Thầy bắt output thì dùng hàm pop(), ko phải duyệt trên xuống.
Browse then write Display().
Expecting him to fix the code review for you later.
want to delete 1 ANY data in the code Stack sir ntn??
You see here nhé
https://www.google.com.vn/search?q=delete+item+from+stack&oq=delete+item+stack&aqs=chrome.1.69i57j0.8935j0j4&client=ms-android-samsung&sourceid=chrome-mobile&ie=UTF-8#xxri=0
British Army for when I asked why do list application, you can link segment
typedef Node *List; //List is a list of the Node
Also in the stack, there is no, although in essence stack but also a list of links
It was in this period nhé
typedef struct Stack
{
Node *Top;
};
at the stack installed on the cursor, in the pop function e think you should delete the top node of the stack because when allocating pointers on the heap so that the memory of the cursor will still exist throughout the program, if not remove the node go when applications are vulnerable to stack overflow stack
a ơi cho e hỏi e dùng code::block . but why e = c re-run program does not use such referenced sir. ecasm Thanks
Not quite understand, How you ask too general.
No reference in C where b, code::Block does not run is right, DevC run is because C ++ compilers bla bla.. (This section explains how m kb)
E asked stack pointer created with very similar list of links created by the cursor menu is not it
I ask you are int isEmpty isfull function but return is 1 clause, the result returned is how ạ?
That value proposition is 1 or 0 you. VD L == NULL, there would be value 1 If right, 0 if wrong.
ad for e asked:
push(): to add dl
peek(): dl and not get deleted
pop(): remove and take dl
“dl and not get deleted” then e appears reasonable to cater to pop() after that. So in pop() have”erase” it is finished, still “take dl” What else to do that sir?
darling added more functions to delete the position k away his
Stack not have that nhé. You thought to yourself, if necessary.
write function by ds special Input_stack. and saw how