Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,628 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,038 people online right now. Registration is fast and FREE... Join Now!




Linked Lists With Classes

 
Reply to this topicStart new topic

Linked Lists With Classes, Modifying A Fixed-Array

Quiksilver
post 2 Apr, 2006 - 09:31 AM
Post #1


New D.I.C Head

Group Icon
Joined: 12 Mar, 2005
Posts: 45



Dream Kudos: 25
My Contributions


hey, i am currently working on modifying a program that originally used a stack array to store values. I am modifying it to use a linked list instead of a fixed-size array. I completley understand the concept of a linked list, but i really don't know how to implement them into my program, and change the array to a linked list. i am hoping someone would please point me in the right direction smile.gif . thank you. i didn't include main(), its self explanitory.

#ifndef _CLASSES_H
#define _CLASSES_H

class STACK
{
private:
int top;
double stack[20];
public:
STACK(); //Constructor
~STACK(); //Destructor
void push(double x);
double pop();
bool isEmpty() //if top is empty, return true, and do not execute 'pop'
{
if(top==-1){
return true;
}else{
return false;
}
}
double current ()
{
return stack[top+1];
}
};

class RPN: public STACK
{
public:
RPN(); //Constructor
void add();
void sub();
void mult();
void div();
};
#include "membermethods.cpp"
#endif
---------------------------------------------------------------------
#include "classes.h"
//RPN Constructor
RPN::RPN()
{
}

//Calculator functions
void RPN::add()
{
double first=0, second=0, ans=0;
first = pop();
second= pop();
push(first + second);
ans=first+second;
cout<<"\nResult of addition: " <<ans<<endl<<endl;
}
void RPN::sub()
{
double first=0, second=0, ans=0;
first = pop();
second= pop();
push(first - second);
ans=first-second;
cout<<"\nResult of subtraction: " <<ans<<endl<<endl;
}
void RPN::mult()
{
double first=0, second=0, ans=0;
first = pop();
second= pop();
push(first * second);
ans=first*second;
cout<<"\nResult of multiplication: " <<ans<<endl<<endl;
}
void RPN::div()
{
double first=0, second=0, ans=0;
first = pop();
second= pop();
push(first / second);
ans=first/second;
cout<<"\nResult of division: " <<ans<<endl<<endl;
}

//Default Constructor
STACK::STACK()
{
top=-1;
for(int i=0;i<20;i++)
{
stack[i]=0;
}
}

//'Push' member method
void STACK::push (double x)
{
top++;
stack[top] = x;
}

//'Pop' member method
double STACK::pop ()
{
double x = stack[top];
top--;
return x;
}

//Destructor
STACK::~STACK()
{
delete []stack;
}

This post has been edited by Quiksilver: 2 Apr, 2006 - 09:33 AM
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 2 Apr, 2006 - 10:02 AM
Post #2


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,176



Thanked 33 times

Dream Kudos: 25
My Contributions


This is a good overview of linked lists:
http://richardbowles.tripod.com/cpp/linklist/linklist.htm
Essentially, instead of having an array, you want to create a node structure...adding new nodes as you require them.
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/23/08 03:40AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month