Thread: Pointers
View Single Post
Old 02-06-2003, 10:21 PM   #10 (permalink)
carrja99
Registered User
 
Join Date: Feb 2003
Posts: 34
carrja99 is on a distinguished road
Quote:
Originally posted by Vlad902
Pointer are evil! I have seen idiots point to pointers of pointers! AHH! :o But they are generally good.
How is that a bad thing???

Code:
*Description: Queries the user for a filename,
 *which should contain a one lined, postfix
 *equation, and solves this equation by using
 *a linked list implementation of a stack.
 ***********************************************/
#include <iostream.h>
#include <fstream.h>
#include <cstring>

struct node
	{
	int key;
	node* next; 
	};

typedef node* stack_ptr; /*stack pointer for parameter passing */

void push(stack_ptr &list, int value)
	{
	if(!list) /* if the list is empty, do this */
		{
		list = new node;       
		list->key = value;
		list->next = NULL;

		return;
		}
	/****************************************** 
	 *create a new node, give it a value, then*
	 *add it to the beginning of the list, and*
	 *set the list pointer to the first node. *
	 ******************************************/

	node* tmp = new node;   
	tmp->key = value;        
	tmp->next = list;	
	list = tmp;		
	}			

int pop(stack_ptr &list)
	{
	if(!list)
		{
		cout << "The stack is currently empty!" << endl;
		return 0;
		}
	/**************************************
	* rtn_value is set to the key of the
	* current node, then a temp node is
	* created to point to the next node 
	* while the current node is deleted, 
	* the list pointer is then set to the
	* temp node as the current node
	***************************************/
	int rtn_val = list->key;     
	stack_ptr tmp = list->next;    
	delete list;		       
	list = tmp;		       	

	return rtn_val;
	}

void print_list(stack_ptr& list)
	{
	if(!list)
		{
		cout << "The stack is currently empty!" << endl;
		return;
		}

	node *tmp = list; /* use a temporary pointer to avoid problems */

	while(tmp)
		{
		cout << tmp->key << " ";
		tmp = tmp->next;
		}

	cout << endl;
	}

int main()
	{
	stack_ptr t = NULL;
	char inFileName[16], str1[50];
	int v;
	ifstream read;

	/****** Ask user for input file *****/
	cout << "Enter file name to be read: ";
	cin >> inFileName;

	/***** open file **********/

	read.open(inFileName);
	if (read.fail())
		{
		cout << "Input file opening failed!"<<endl;
		exit(1);
		}

        /* read file, make function calls to solve equation */

	for (int i = 0; !read.eof(); i++)   
		{
		read.getline(str1,50, ' '); /* gets current token */	
		                            /* using ' ' as diliminator */
		if (isdigit(str1[0]))
	 		{
			v = atoi(str1); /* convert string to int value */
			push(t, v);     
			}
		else if (str1[0] == '+')
			{
			v = pop(t) + pop(t);
			push (t, v);
			}
		else if (str1[0] == '-')
			{
			v = pop(t) - pop(t);
			push (t, v);
			}	
		else if (str1[0] == '*')
			{
			v = pop(t) * pop(t);
			push (t, v);
			}
		else if (str1[0] == '/')
			{
			v = pop(t) / pop(t);
			push (t, v);
			}
		
		}
	cout << "The result is: ";
	
	print_list(t); /* at this juncture, if input file is in
			  correct format, one item should be on
                          the list, the solution.            */

return 0;
}
Try implementing the above code using an array rather than a linked list.
carrja99 is offline   Reply With Quote