Jump to content
eita_zuliana

Stack

Recommended Posts

Sapa2 boleh tolong nak selesaikan masalah nie....saya dah buat tapi ade error jugak....mintak tolong.... :wacko:

Create a program using C++ to implement stack using array. You must require the user to input their date of birth in format ddmmyyyy. Push the input from the user into the stack. Display the top element of the stack. Then pop 4 elements. Again display the top element of the stack. Now, push integer 10. Display the top element of the stack and the whole content of the stack.

**As we know, only one element can be pushed into the stack at once.

:unsure:

#include <iostream.h>

const int size=100;

class Stack

{

private:

int Top;

char data;

char bday;

public:

create();

Push();

Pop();

StackTop();

bool Full();

bool Empty();

};

Stack::create()

{

Top = -1;

}

bool Stack::Empty()

{

return bool(Top== -1);

}

bool Stack::Full()

{

return bool(Top== size-1);

}

Stack::Push()

{

char newitem;

int i;

for(i=0;i<8;i++)

cout<<"Enter item into stack:"<<endl;

cin>>newitem;

if(Full())

cout<<"Stack is full!!"<<endl;

else

{

Top=Top+1;

data[Top]= newitem;

}

}

Stack::Pop();

{

if(Empty())

cout<<"Stack is empty!!"<<endl;

else

{

cout<<"Item has been popped is:"<<data[Top]<<endl;

Top=Top-1;

}

}

Stack::StackTop()

{

if(Empty())

cout<<"Stack is empty"<<endl;

else

{

cout<<"Top item in the stack is:"<<data[Top]<<endl;

}

}

void main()

{

Stack d;

int Top;

char bday;

cout<<"What is your birthday?:"<<endl;

cin>>bday;

d.Push();

d.create();

d.StackTop();

d.Full();

d.Empty();

return 0;

}

Share this post


Link to post
Share on other sites

Kita kena wat baik sesama manusia.. tolong menolong, bekerjasama sentiasa mengeratkan silatulrahim.. eh? hehe.. x de siapa nak tolong cik ni ke? cik Eita Zuliana, nanti saya cuba check coding cik... :)

Share this post


Link to post
Share on other sites

apasal aku rase mcm k0 punye malsh?

first tgk koding aku dh tau first error cube buang void dkt main bwh sekali... lain2 error cari sendiri ar...

p/s aku pun ader coding yg perlu disiapkn

Share this post


Link to post
Share on other sites

aku cuba fix error yg terang2 jer ni, banyak lagi bug perlu dibuang.

#include <iostream>

using namespace std;

const int size=100;

class Stack
{
private:

    int Top;
    char data[size];
    char bday;
    char newitem;

public:

    void create();
    void Push();
    void Pop();
    void StackTop();
    bool Full();
    bool Empty();
};

void Stack::create()
{
    Top = -1;
}

bool Stack::Empty()
{
    return bool(Top== -1);
}
bool Stack::Full()
{
    return bool(Top == (size-1));
}
void Stack::Push()
{

    int i;
    for (i=0;i<8;i++)
        cout << "Enter item into stack:" << endl;
    cin >> newitem;
    cout << "ruun" << endl;

    if (Full())
        cout << "Stack is full!!" << endl;
    else
    {
        Top=Top+1;
        data[Top]= newitem;
    }
}

void Stack::Pop()
{
    if (Empty())
        cout<<"Stack is empty!!"<<endl;
    else
    {
        cout<<"Item has been popped is:"<<data[Top]<<endl;
        Top=Top-1;
    }
}
void Stack::StackTop()
{
    if (Empty())
        cout<<"Stack is empty"<<endl;
    else
    {
        cout<<"Top item in the stack is:"<<data[Top]<<endl;
    }
}

int  main()
{
    Stack d;
    char bday;

    cout<<"What is your birthday?:"<<endl;
    cin>>bday;
    d.create();
    d.Push();
    d.StackTop();
    d.Full();
    d.Empty();
    return 0;
}

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...