vendredi 27 février 2015

WIll the stack pointer in the function would be formal parameter?




#include<iostream>
#include<stdio.h>
#include<stdlib.h>

using namespace std;

struct node

{

int data;

struct node *next;

};



struct stack

{

node *head;

};

void push(struct stack *stack1,int data)
{

struct node *new_node= new node;

new_node->data=data;

if(stack1->head!=NULL)

new_node->next=stack1->head;

else

new_node->next=NULL;

stack1->head=new_node;

}


void pop(struct stack *stack1)

{

node *temp= new node;

if(stack1->head==NULL)

cout<<"EMPTY"<<endl;

else

{

temp=stack1->head;

stack1->head=(stack1->head)->next;

int t;

t=temp->data;

free(temp);

cout<<t<<" ";

}

}



void show(struct stack *stack1)

{

node *new_node=new node;

new_node=stack1->head;

if(stack1->head==NULL)

{

cout<<"EMPTY"<<endl;

return;

}

while(new_node!=NULL)

{

cout<<new_node->data<<" ";

new_node=new_node->next;

}

}



void peek(struct stack *stack1)

{

if(stack1->head==NULL)

cout<<"EMPTY";

else

cout<<stack1->head->data<<" ";

}





int main()

{

int temp,temp2;

struct stack *stack1=new stack;

stack1->head=NULL;

while(1)

{

cin>>temp;

switch(temp)

{

case 0: exit(0);



case 1: cin>>temp2;

cout<<endl;

push(stack1,temp2);

break;



case 2: pop(stack1);

cout<<endl;

break;



case 3: peek(stack1);

cout<<endl;

break;



case 4: show(stack1);

cout<<endl;

break;

}

}

return 0;

}



Aucun commentaire:

Enregistrer un commentaire