0

Hey guys/gals for some reason in my main function my first while loop will not take in more than two values. any reasons why that wont happen? and just to be on the safe side, did I implement my remove function properly?

#ifndef SORTEDLIST_H
#define SORTEDLIST_H
 
#include<iostream>
using namespace std;
 
struct node
{
 int info;
 node* next;
 node* previous;
};
 
class SortedList
{
public:
SortedList(); 
~SortedList();
void insert(int x);
void displayForward();
void displayReverse();
void remove(int x);
int getLength(); 
 
private:
node* getNode(int x);
bool exists(int x);
void insertAtBeg(node *p);
void insertAtEnd(node *p);
void insertInBetween(node *p);
node* listBeg; //pointer to the beginning of the list
node* listEnd; //pointer to the end of the list
int length; //size of the list
};
 
#endif
#include "SortedList.h"
 
SortedList::SortedList()
{
 length = 0;
 listBeg = NULL;
 listEnd = NULL;
 }//constructor
 
SortedList::~SortedList()
 {
	while (listBeg != NULL)
    {
		node* next = listBeg->next;
        delete listBeg;
        listBeg = next;
     }
 }//destructor
 
void SortedList::insert(int x)
 {
	 if(!exists(x))
	 {
		 node* p = getNode(x);
		 if(listBeg == NULL)
		 {
			 listBeg = p;
			 listEnd = p;
		 }else if (p->info < listBeg->info)
		 {
			 insertAtBeg(p);
		 }else if(p->info > listEnd->info)
		 {
			 insertAtEnd(p);
		 }else
		 {
			 insertInBetween(p);
		 }
		 length++;
	 }else
	 {
		 cout << "Error. Node already exists in the list. " << endl;
	 }
 }//insert
 
void SortedList::displayForward()
{
	node* t = listBeg;
	while(t != NULL)
	{
		cout << t->info << " ";
		t = t->next;
	}
	cout << endl;
}//displayForward
 
void SortedList::displayReverse()
{
	node* t = listEnd;
	while(t != NULL)
	{
		cout << t->info << " ";
		t = t->previous;
	}
	cout << endl;
}//displayReverse
 
void SortedList::remove(int x)
{
	if(exists(x))
	{
		node *p = new node;
		p->info = x;
		p->previous->next = p->next;
		p->next->previous = p->previous;
		delete p;	 
	}else
	{
		cout << "Node selected cannot be deleted because it does not exist." << endl;
	}

}//remove
 
int SortedList::getLength()
{
	return length;
}//getLength
 
node* SortedList::getNode(int x)
{
	node *p = new node;
	p->info = x;
	p->next = NULL;
	p->previous = NULL;
	return p;
}//getNode
 
bool SortedList::exists(int x)
{ 
	node* t = listBeg;
	bool found = false;
	while(t != NULL)
	{
		if(t->info == x)
		{
			found = true;
			t = t->next;
		}
	}
}//exists 
 
void SortedList::insertAtBeg(node *p)
{
	p->next = listBeg;
	listBeg->previous = p;
	listBeg = p;
}//insertAtBeg
 
void SortedList::insertAtEnd(node *p)
{
	p->previous = listEnd;
	listEnd->next = p;
	listEnd = p;
}//insertAtEnd
 
void SortedList::insertInBetween(node *p)
{
	node* t = listBeg;
	while(t->info < p->info)
	{
		t = t->next;
	}
	p->next = t;
	p->previous = t->previous;
	t->previous->next = p;
	t->previous = p;
}//insertInBetween
//main program
#include "SortedList.h"

int main()
{
	SortedList list;
	int choice = 0;
	while(choice != -1)
	{
		cout << "Choose a node to enter(-1 to stop):";
		cin >> choice;
		list.insert(choice);
	}
	
	cout << "Displaying List forward." << endl;
	list.displayForward();
	cout << "Displaying List in reverse." << endl;
	list.displayReverse();

	int del = 0;
	while(del != -1)
	{
		cout << "Choose a node to delete(-1 to stop):";
		cin >> del;
		list.remove(del);
	}

	cout << "After deletion: " << endl;
	cout << "Displaying List forward." << endl;
	list.displayForward();
	cout << "Displaying List in reverse." << endl;
	list.displayReverse();

	return 0;
}
2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by prvnkmr194
0

Hello Error is in ur exit function

bool SortedList::exists(int x)
{ 
	node* t = listBeg;
	bool found = false;
	while(t != NULL)
	{
		if(t->info == x)
		{
			found = true;
			[B]t = t->next; [/B]//it should be out side the if
		}
	}
}//exists

Use like this

bool SortedList::exists(int x)
{ 
	node* t = listBeg;
	bool found = false;
	while(t != NULL)
	{
		if(t->info == x)
		{
			found = true;
		
		}
	t = t->next;
	}
}//exists

I hope u understand why?

If not ask for help
Best Of Luck

Edited by prvnkmr194: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.