4 Replies - 290 Views - Last Post: 19 September 2009 - 03:39 PM Rate Topic: -----

#1 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,405
  • Joined: 15-July 09

Stacks and Functions

Posted 12 August 2009 - 07:55 PM

Well basicly the problem is that i have to call the function push while the char that is asked for is not '0' then i have to send the modified "*top" and "*temp" into the function POP so that i can show the values and do the deleting of the charaters that where introduced f3
I think i didnt explained myself verywell i hope you can help me, this is the code i got so far

#include <iostream>
#include <conio.h>

using namespace std;

struct Stack
{
	char letra;
	Stack *next;
};
void push(char n, Stack *top, Stack *temp)
{
		temp=new(Stack);
		temp->letra=n;
		temp->next=top;
		top=temp;// i need to return this values to the main
}
void pop(Stack *top, Stack *temp)//this function should receive the values of top and temp from function push()
{
	int x1;
	temp=top;
	x1=temp->letra;
	cout<<x1;
	top=top->next;
	delete temp;
}
int main()
{
	Stack *start=NULL, *temp=NULL;
	char x;
	do
	{
		cin>>x;
		push(x,start,temp);//i think this function is working
	}
	while(x!='0');
	while(top!=NULL)
	{
		pop(start,temp);//my problem is here f3
	}
	getch();
}

The assignment says that we cant use global declarations for the Stack *start or any Stack structure
which would make this program alot easier f3.
This is my first time using stacks so i know i messed up really bad.
Hope you can help me
Thank you

Is This A Good Question/Topic? 0
  • +

Replies To: Stacks and Functions

#2 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2247
  • View blog
  • Posts: 9,237
  • Joined: 18-February 07

Re: Stacks and Functions

Posted 12 August 2009 - 09:15 PM

Well lets think about something -- you can't use global values but you really want to. In my opinion you should not use global variables. So, how might you avoid using them, and yet still act like it? Check this version out:
#include <iostream>

using namespace std;

struct Node {
	char letter;
	Node *next;
};

struct Stack {
	Node *top;
};

void push(Stack &s, char ch) {
	Node *temp = new Node;
	temp->letter = ch;
	temp->next = s.top;
	s.top = temp;
}

char pop(Stack &s) {
	char retVal = 0; //default return value
	if (s.top!=NULL) {
		Node *temp = s.top;
		retVal = temp->letter;
		s.top = temp->next;
		delete temp;
	}
	return retVal;
}

void initStack(Stack &s) {
	s.top = NULL;
}

int main() {
	Stack stk;
	char word[] = "This is a test";
	initStack(stk);
	for (char *ptr = word; *ptr!=0; ptr++) {
		push(stk, *ptr);
	}
	
	char ch;
	while((ch = pop(stk))!=0) {
		cout << ch;
	}
	cout << endl;

	return 0;
}

Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5643
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: Stacks and Functions

Posted 13 August 2009 - 05:16 AM

View Postponcho4all, on 12 Aug, 2009 - 08:55 PM, said:

or any Stack structure


That one is kind of confusing.

Let's go with what you have. In order for anything to work, you need to be able to change the pointer value of the top element. This unfortunately means you have to use the most confusing syntax in C and something you can normally avoid in C++; the pointer to a pointer. You do not, and should not, be throwing temp around.

Here's you code, only slightly modified. Hope it makes sense.

#include <iostream>

using namespace std;

struct Stack {
	char letra;
	Stack *next;
};

// don't pass temp!
// your instructor is almost certaintly looking for a pointer pointer
void push(char n, Stack **top) {
	// temp=new(Stack); bad syntax
	Stack *temp = new Stack;
	temp->letra=n;
	temp->next=*top;
	*top=temp; 
	// i need to return this values to the main 
	// NO, you don't.  You've added it to stack.
	
}

char pop(Stack **top) {
	Stack *temp = *top;
	char x1=temp->letra;
	*top=temp->next;
	delete temp;
	return x1;
}

int main() {
	Stack *top = NULL;
	
	while(true) {
		char x;
		cin>>x;
		if (x=='0') { break; }
		push(x, &top);
	}
	
	//cout << top->letra; 
	
	while(top!=NULL) {
		cout << pop(&top) << endl;
	}
}


Was This Post Helpful? 1
  • +
  • -

#4 sana1990  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 15-September 09

Re: Stacks and Functions

Posted 19 September 2009 - 03:15 PM

hello...
i have to bulit a function using stack in which the user enters a index number of the number he wants to pull or push from the stack...and the program then pulls or pushs it ...
my pull funtion is running....but my push and pull are almost the same...and that is why i am not sure whether it is a push funtion or not..
the onlky difference is the inputtin of -1 rather then the element the user wants to input



void main()
{

int x[10], int w[10],int r, key;
int toss =9;

for(it y=9; y>=0; y--)
{
cin>x[y]; //inputin the stack

}

cout<<"enter the index number of the element you want to remove";
int z;
cin>>z;

for(y=0; y<z; y++)
{
x[y]=w[toss]; //copyin the first stack i.e x into the second stack i.e w
toss--;

}

x[z]=-1; //to delte the number i declare it equal to -1


//this is the only differnece i will be havin in my push funtion
// i.e i will be putting x[z] equal to the user inputed number

toss=r=z;

for(y=0; y<z; y++)
{
x[r-1]=w[toss];
toss++;
r--;
}

}



thanks.
Was This Post Helpful? 0
  • +
  • -

#5 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,405
  • Joined: 15-July 09

Re: Stacks and Functions

Posted 19 September 2009 - 03:39 PM

First off this should be a bew topic the one you posted on its already solved.

Now:

Where is your node structure?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1