Saturday, 20 May 2017

Struktur data 2

Struktur data tipe stack

Operasi – operasi pada Stack (Tumpukan)
Operasi yang sering diterapkan pada struktur data Stack (Tumpukan) adalah Push dan Pop. Operasi – operasi yang dapat diterapkan adalah sebagai berikut :
1. Push : digunakan untuk menembah item pada Stack pada Tumpukan paling atas.
2. Pop : digunakan untuk mengambil item pada Stack pada Tumpukan paling atas.
3. Clear : digunakan untuk mengosongkan Stack.
4. Create Stack : membuat Tumpukan baru S, dengan jumlah elemen kosong.
5. MakeNull : mengosongkan Tumpukan S, jika ada elemen maka semua elemen dihapus.
6. IsEmpty : fungsi yang digunakan untuk mengecek apakah Stack sudah kosong.
7. Isfull : fungsi yang digunakan untuk mengecek apakah Stack sudah penuh.
           Pada proses Push, Tumpukan (Stack) harus diperiksa apakah jumlah elemen sudah mencapai masimum atau tidak. Jika sudah mencapai maksimum maka OVERFLOW, artinya Tumpukan penuh tidak ada elemen yang dapat dimasukkan ke dalam Tumpukan. Sedangkan pada proses Pop, Tumpukan harus diperiksa apakah ada elemen yang hendak dikeluarkan atau tidak. Jika tidak ada maka UNDERFLOW, artinya tumpukan kosong tidak ada elemen yang dapat diambil.
Contoh :
Stack using linked list
/*
    @author: CreativeCub
*/
#include<iostream>
#include<cstdlib>
using namespace std;
struct node
{
    int info;
    struct node *link;
}*top;
class stack_list
{
    public:
        node *push(node *, int);
        node *pop(node *);
        void traverse(node *);
        stack_list()
        {
            top = NULL;
        }
};
int main()
{
    int choice, item;
    stack_list sl;
    cout<<"Operations on Stack"<<endl;
    cout<<"\n-------------"<<endl;
    cout<<"1.Push Element into the Stack"<<endl;
    cout<<"2.Pop Element from the Stack"<<endl;
    cout<<"3.Traverse the Stack"<<endl;
    cout<<"4.Quit"<<endl;
    while (1)
    {
        cout<<"\nEnter your Choice: ";
        cin>>choice;
        switch(choice)
        {
        case 1:
            cout<<"Enter value to be pushed into the stack: ";
            cin>>item;
            top = sl.push(top, item);
            break;
        case 2:
            top = sl.pop(top);
            break;
        case 3:
            sl.traverse(top);
            break;
        case 4:
            exit(1);
            break;
        default:
            cout<<"Wrong Choice"<<endl;
        }
    }
    return 0;
}
node *stack_list::push(node *top, int item)
{
    node *tmp;
    tmp = new (struct node);
    tmp->info = item;
    tmp->link = top;
    top = tmp;
    return top;
}
node *stack_list::pop(node *top)
{
    node *tmp;
    if (top == NULL)
        cout<<"Stack is Empty"<<endl;
    else
    {
        tmp = top;
        cout<<"Element Popped: "<<tmp->info<<endl;
        top = top->link;
        free(tmp);
    }
    return top;
}
void stack_list::traverse(node *top)
{
    node *ptr;
    ptr = top;
    if (top == NULL)
        cout<<"Stack is empty"<<endl;
    else
    {
        cout<<"Stack elements :"<<endl;
        while (ptr != NULL)
        {
            cout<<ptr->info<<endl;
            ptr = ptr->link;
        }
    }
}

OUTPUT

Operations on Stack

-------------
1.Push Element into the Stack
2.Pop Element from the Stack
3.Traverse the Stack
4.Quit

Enter your Choice: 1
Enter value to be pushed into the stack: 10

Enter your Choice: 1
Enter value to be pushed into the stack: 20

Enter your Choice: 1
Enter value to be pushed into the stack: 30

Enter your Choice: 1
Enter value to be pushed into the stack: 40

Enter your Choice: 3
Stack elements :
40
30
20
10

Enter your Choice: 2
Element Popped: 40

Enter your Choice: 2
Element Popped: 30

Enter your Choice: 3
Stack elements :
20
10

Enter your Choice: 4




No comments:

Post a Comment

Audit Dengan ACL

AUDIT TI PERINTAH AUDIT MENGGUNAKAN ACL 1.Mencari nomor telepon dengan menggunakan  fields subscription_no 082218868660     Klik...