Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Help Bug Fixing my C++ Code Node.h #ifndef NODE_H #define NODE_H #include #include using namespace std ; template < class T > class node {

Help Bug Fixing my C++ Code

Node.h

#ifndef NODE_H

#define NODE_H

#include

#include

using namespace std;

template<class T>

class node {

public:

node();

node(T);

node(T, node<T> *);

node(const node<T>& orig);

~node();

void printAll();

node<T>* GetNextNode() const {

return nextNode;

}

void SetNextNode(node<T>* _nextNode) {

nextNode = _nextNode;

}

T GetValue() const {

return value;

}

void SetValue(T _value) {

value = _value;

}

private:

T value;

node<T> *nextNode;

};

#endif /* NODE_H */

Node.cpp

#include "node.h"

template<class T>

node<T>::node() : value(0), nextNode(NULL){

}

template<class T>

node<T>::node(T _value): value(_value), nextNode(NULL) {

}

template<class T>

node<T>::node(T _value, node *_nextNode) : value(_value), nextNode(_nextNode) {

}

template<class T>

node<T>::node(const node<T>& orig) : value(orig.GetValue()), nextNode(orig.GetNextNode()) {

}

template<class T>

node<T>::~node() {

}

template<class T>

void node<T>::printAll(){

cout << "Node content:" << endl;

cout << value << endl;

cout << nextNode << endl;

}

LinkedList.h

#ifndef LINKEDLIST_H

#define LINKEDLIST_H

#include "Node.h"

template

class LinkedList

{

int size;

node *head;

node *tail;

public:

LinkedList();

~LinkedList();

int getSize() const;

node* getHead() const;

node* getTail() const;

void setSize(int s);

void setHead(node &h);

void setTail(node &t);

// insert the node n at t-th position(0...size)

void insertNode(int n, int pos);

// deletes the element at position pos from the list

void deleteNode(int pos);

//searches the node and returns its position

int search(T n);

void print();

};

#endif

LinkedList.cpp

#include "linkedlist.h"

#include

using namespace std;

template

LinkedList::LinkedList()

{

size=0;

head=tail=NULL;

}

template

LinkedList::~LinkedList()

{

node *temp=head;

while(temp)

{

temp=head->GetNextNode();

delete head;

head=temp;

}

}

template

void LinkedList::print()

{

cout << "List contents:" << endl;

node *temp(*head);

while(temp)

{

temp->printAll();

}

}

template

int LinkedList::getSize() const

{

return size;

}

template

node* LinkedList::getHead() const

{

return head;

}

template

node* LinkedList::getTail() const

{

return tail;

}

template

void LinkedList::setSize(int s)

{

size=s;

}

template

void LinkedList::setHead(node &h)

{

head=h;

}

template

void LinkedList::setTail(node &t)

{

tail=t;

}

template

void LinkedList::insertNode(int n, int pos)

{

node *newnode(n);

if((pos==0) && (head==NULL))

{

head=tail=newnode;

}

else if(pos==0)

{

newnode->SetNextNode(head);

head=newnode;

}

else if(pos==size)

{

tail->GetNextNode()=newnode;

tail=newnode;

}

else

{

int i;

node *temp=head;

for(i=0;i

{

temp=temp->GetNextNode();

}

newnode->GetNextNode()=temp->GetNextNode();

temp->GetNextNode()=newnode;

}

size++;

}

template

void LinkedList::deleteNode(int pos)

{

if(head == tail)

{

head=tail=NULL;

}

else if(pos==0)

{

head=head->GetNextNode();

}

else

{

int i;

node *temp=head;

for(i=0;i

{

temp=temp->GetNextNode();

}

temp->GetNextNode()=temp->GetNextNode()->GetNextNode();

size--;

}

}

template

int LinkedList::search(T el)

{

int pos=0,flag=0;

node *temp=head;

while(temp)

{

if(el==temp->GetValue())

{

pos++;

flag=1;

}

temp=temp->GetNextNode();

}

if(flag)

return pos;

else

return 0;

}

TestProgram.cpp

#include "LinkedList.h"

#include "Node.h"

#include

using namespace std;

int main()

{

LinkedList list;

int ch;

char choice='y';

int el,pos;

cout<<"This program performs operations on linked list";

do

{

cout<<" 1. Insert";

cout<<" 2. Delete";

cout<<" 3. Search";

cout<<" 4. Show all elements";

cout<<" 5. Exit";

cout<<" Enter your choice: ";

cin>>ch;

switch(ch)

{

case 1:

cout<<"Enter a value to be inserted: ";

cin>>el;

cout<<"Enter a position where you want to enter an element: ";

cin>>pos;

if(pos<0 || pos>(list.getSize()))

cout<<" Please enter a valid position (0-"<

else

{

list.insertNode(pos-1,el);

list.print();

}

break;

case 2:

printf("Enter a position where you want to delete: ");

cin>>pos;

if(pos<0 || pos> (list.getSize()-1))

cout<<" Please enter a valid position(0-"<

else

{

list.deleteNode(pos-1);

list.print();

}

break;

case 3:

if(list.getHead()==NULL)

{

cout<<"List is empty. ";

}

else

{

cout<<"Enter an element you want to search: ";

cin>>el;

pos=list.search(el);

if(pos)

cout<<"Element you want to serach is at position: %d ",pos;

else

cout<<" Element not found in the list. ";

list.print();

}

break;

case 4:

cout<<"Elements in the list are: ";

list.print();

break;

case 5:

cout<<"Exiting...";

exit(0);

break;

default:

cout<<"Invalid choice!";

break;

}

}while(choice=='y'||choice=='Y');

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Put Your Data To Work 52 Tips And Techniques For Effectively Managing Your Database

Authors: Wes Trochlil

1st Edition

0880343079, 978-0880343077

More Books

Students also viewed these Databases questions

Question

What does NASDAQ stand for? What is NASDAQ?

Answered: 1 week ago

Question

What is the purpose of the Salary Structure Table?

Answered: 1 week ago

Question

What is the scope and use of a Job Family Table?

Answered: 1 week ago