i was giving an assignemnt to show stacks with out using the STL .
my question is is my code a correct example or not ?
and if so can some some one guide or explain to me what i did wrong or what i need to change

#include <stdio.h>
#include <string>
#include <vector>

class Foo
{
   std::string _str;
public:
   Foo(void)
   {
      printf("Foo: Void constructor\n");
      _str = "";
   }
   Foo ( const char *s )
   { 
      printf("Foo: Full constructor [%s]\n", s);
      _str = s;
   }
   Foo( const Foo& aCopy )
   {
      printf("Foo: Copy constructor\n");
      _str = aCopy._str;
   }
   virtual ~Foo()
   {
      printf("Foo: Destructor\n");
   }
   Foo operator=(const Foo& aCopy)
   {
      _str = aCopy._str;
      return *this;
   }
   std::string String()
   {
      return _str;
   }
   void setString( const char *str )
   {
      _str = str;
   }
};


int main()
{
   printf("Creating array via new\n");
   Foo *f = new Foo[2];("Hello");// line 47 
   printf("Creating array on heap\n");
   Foo f1[3];

   // Create a vector
   printf("Creating vector of foo\n");
   std::vector<Foo> fooVector;

   printf("Adding objects to vector\n");
   Foo f2;
   Foo f3;
   Foo f4;
   fooVector.insert( fooVector.end(), f2 );
   fooVector.insert( fooVector.end(), f3 );
   fooVector.insert( fooVector.end(), f4 );

   printf("Deleting array on heap\n");
   delete [] f;
}

Recommended Answers

All 7 Replies

I could have sworn <vector> is part of the STL....

Comatose is right. Vectors are a no-no

And your code won't compile, we discussed it in this thread

I could have sworn <vector> is part of the STL....

IT IS !! thats why i am asking how can this be done with out it

IT IS !! thats why i am asking how can this be done with out it

No you didn't:

my question is is my code a correct example or not ?

And Comatose gave you the correct answer: No.

Now if you're going to shout and cry about it, nobody is going to help you anymore

commented: Perfectly Worded +10

http://www.cplusplus.com/reference/stl/stack/
You can implement it however you want (arrays, lists, blah blah) so long as you have "push" and "pop".

ok i think i figured it out but how do i add "top" to my code ?

#include<iostream>
using namespace std;
struct node{
	int data;
	node *next;
	};
//This function pushes data(node) to the stack
node * push(node*,node*);
//This function pops data (node) and returns pointer to the poped node
node * pop(node*);
//This function shows the stack
void show_stack(node*);
///These two global variables will help us track the start and end of the list
node *f,*l;

int main()
{
	int i,ch;
	f=l=NULL;
	cout<<"\t 1 to push"<<endl;
	cout<<"\t 2 to pop"<<endl;
	cout<<"\t 3 to show stack"<<endl;
	cout<<"\t 0 to exit"<<endl;
	while(1)
	{
		cout<<"Enter your choice:";
		cin>>ch;
		switch(ch)
		{
		case 1:
			{
			if(f==NULL)
			l=f=push(f,l);
			else
			l=push(f,l);
			break;
			}
		case 2:
			{
			cout<<"popped:"<<pop(f)->data<<endl;
			break;
			}

		case 3:
			{
			show_stack(f);
			break;
			}
		case 0:
			break;
		default:
			cout<<"Please enter a proper choice"<<endl;break;
		}
		if(ch==0)
		break;
	}
}
node * push(node *f,node *l)
{
	node *n;
	n=new node;
	cout<<"Enter data:";
	cin>>n->data;
	n->next=NULL;
	if(f==NULL)
	{f=l=n;return f;}
	else
	{
		l->next=n;
		l=n;return l;
	}

}
void show_stack(node *f)
{
	cout<<"showing data"<<endl;
	node *guest=f;
	while(guest!=NULL)
	{
		cout<<"\t"<<guest->data<<endl;
		guest=guest->next;
	}
}
node * pop(node *f)
{
	node *guest,*lb;
	guest=lb=f;
	while(guest->next!=NULL)
	{
		lb=guest;
		guest=guest->next;
	}
	lb->next=NULL;
	l=lb;
	return guest;
}

//This function pushes data(node) to the stack
node * push(node*,node*);
//This function pops data (node) and returns pointer to the poped node
node * pop(node*);
//This function shows the stack
void show_stack(node*);
///These two global variables will help us track the start and end of the list
node *f,*l;

Good, now put this lot in another class called say mystack.

Then your main contains things like
mystack s;

and
s.push( value );

and
value = s.pop();

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.