plz convert this program

converting c - c++

Page 1 of 1

4 Replies - 612 Views - Last Post: 12 October 2008 - 05:16 AM Rate Topic: -----

#1 ajazz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-October 08

plz convert this program

Post icon  Posted 12 October 2008 - 03:15 AM

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

struct list{
char item;
struct list *link;
};
typedef struct list x;
x *stack,*temp=NULL;


push(char data) /* pushes characters to stack */
{
stack=(x *)malloc(sizeof(x));
stack->item=data;
stack->link=temp;
temp=stack;
}

pop() /* pops characters from stack */
{
x *t;
while(temp!=NULL)
{
cout<<"%c"<<temp->item;
t=temp;
temp=temp->link;
free(t); /* deallocate allocated memory */
}
printf(" ");
temp=NULL;
}



main()
{
int i;
char string[30];
clrscr();
cout<<"\nReverser 1.0";
cout<<"\nThis reverses a sentence word by word making use of stacks\n\n";
cout<<"Enter the sentence :";
gets(string);
i=0;
printf("The reverse is :");
while(string[i]!='\0')
{
if(isspace(string[i])!=0) /* if space is encountered */
pop();
else
push(string[i]);
i++;
}
pop();
}

if anyone hav any data sturcture implementations like stack,queues,binary trees application program, Or anyone know any website abt these program then plz help me...

Is This A Good Question/Topic? 0
  • +

Replies To: plz convert this program

#2 schnalf  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 129
  • Joined: 09-February 08

Re: plz convert this program

Posted 12 October 2008 - 03:27 AM

hi,
c is 100% compatible to c++, so when your code is valid c code, it is also valid c++ code.

in the stl are stack, deque, vector classes eg. implemented.
here some good references:

stack: http://www.cplusplus...ence/stl/stack/
deque: http://www.cplusplus...ence/stl/deque/
vector: http://www.cplusplus...nce/stl/vector/
Was This Post Helpful? 0
  • +
  • -

#3 ajazz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-October 08

Re: plz convert this program

Posted 12 October 2008 - 03:31 AM

well i know its compatible to c++..
actually i hav to submit a program in c++ based on data structures and this program is based on stack in c.. so is it possible to convert it into c++
Was This Post Helpful? 0
  • +
  • -

#4 schnalf  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 129
  • Joined: 09-February 08

Re: plz convert this program

Posted 12 October 2008 - 04:34 AM

i think nobody here will do your homework.
but this code is might be usefull for you: (its out of the "Data Structures In C++ Tutorial" (http://www.dreamincode.net/forums/showtopic10157.htm))

#include <iostream>

using namespace std;

struct node
{
   int data;
   node *link;
};

class lstack
{
      private:
              node* top;

      public:
             lstack()
             {
             	top=NULL;
             }
             
             void push(int n)
             {
             	node *tmp;
             	tmp=new node;
             	if(tmp==NULL)
             	   cout<<"\nSTACK FULL";
             	   
             	tmp->data=n;
             	tmp->link=top;
             	top=tmp;
             }

             int pop()
             {
             	if(top==NULL)
             	{
             		cout<<"\nSTACK EMPTY";
             		return NULL;
             	}
                node *tmp;
                int n;
                tmp=top;
                n=tmp->data;
                top=top->link;
                delete tmp;
                return n;
             }

             ~lstack()
             {
             	if(top==NULL)
             	   return;

             	node *tmp;
             	while(top!=NULL)
             	{
             	   tmp=top;
             	   top=top->link;
             	   delete tmp;
             	}
             }
};

int main()
{
	lstack s;
	s.push(11);
	s.push(101);
	s.push(99);
	s.push(78);
	cout<<"Item Popped = "<<s.pop()<<endl;
	cout<<"Item Popped = "<<s.pop()<<endl;
	cout<<"Item Popped = "<<s.pop()<<endl;
	return 0;
}


Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5832
  • View blog
  • Posts: 12,683
  • Joined: 16-October 07

Re: plz convert this program

Posted 12 October 2008 - 05:16 AM

If you're just stealing code; steal better code. :P The stuff you found is really quite bad.

You should be able to find numerous better examples, closer to what you're looking for, in this site's snippet section.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1