Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ Implement getFirst and getLast in a Linked List program A getFirst function that returns a pointer to the first node in the list. A

C++ Implement getFirst and getLast in a Linked List program

A getFirst function that returns a pointer to the first node in the list.

A getLast function that returns a pointer to the last node in the list.

I have everything working for the program except I dont know how to implement the getFirst and getLast function.

I will post my .cpp and .h files

.h

#include

#include

using namespace std;

template

class List

{

struct Node

{

T data;

Node *next;

Node(T d, Node *n = 0):data(d), next(n) {}

};

Node *head;

public:

List(Node *h = 0):head(h){}

~List();

void insert(Node *loc, T d);

void push_back(T d);

void push_front(T d);

T pop_back();

T pop_front();

void erase(Node *loc);

void display();

Node *search(T d);

};

// destructor

template

List::~List()

{

Node *tmp;

while(head) {

tmp = head;

head = head->next;

delete tmp;

}

}

// insert d before loc

template

void List::insert(Node *loc, T d)

{

Node *new_node = new Node(d,0);

if(!head) {

head = new_node;

return;

}

if(loc == head) {

push_front(d);

return;

}

Node *cur = head;

while(cur->next) {

if(cur->next == loc) {

new_node->next = cur->next;

cur->next = new_node;

return ;

}

cur = cur->next;

}

}

template

void List::push_back(T d)

{

Node *new_node = new Node(d,0);

if(!head) {

head = new_node;

return;

}

Node *cur = head;

while(cur) {

if(!cur->next) {

cur->next = new_node;

return;

}

cur = cur->next;

}

}

template

void List::push_front(T d)

{

Node *new_node = new Node(d,0);

if(!head) {

head = new_node;

return;

}

new_node->next = head;

head = new_node;

return;

}

template

T List::pop_back()

{

Node *cur = head;

while(cur) {

if(!cur->next) {

T data (cur->data);

delete cur;

head = NULL;

return data;

}

else {

if(!cur->next->next) {

T data (cur->next->data);

cur->next = NULL;

delete cur->next;

return data;

}

}

cur = cur->next;

}

return NULL;

}

template

T List::pop_front()

{

if(!head) return NULL;

Node *tmp = head;

T data (head->data);

if(head->next) {

head = head->next;

delete tmp;

return data;

}

delete tmp;

head = NULL;

return data;

}

template

void List::erase(Node *loc)

{

if(loc == head) {

Node *tmp = head;

head = head->next;

delete tmp;

return;

}

Node *cur = head;

while(cur) {

if(cur->next == loc) {

cur->next = loc->next;

delete loc;

}

cur = cur->next;

}

}

template

typename List::Node* List::search(T d)

{

if(!head) return NULL;

Node* cur = head;

while(cur) {

if(cur->data == d) return cur;

cur = cur->next;

}

return NULL;

}

template

void List::display()

{

if(!head) return;

Node *cur = head;

while(cur) {

cout << cur->data << " " << endl;

cur = cur->next;

}

cout << endl;

}

.cpp

#include

#include "nodeList.h"

#include

using namespace std;

int main()

{

List *myList = new List(NULL);

cout << "push_back() Milk, Eggs, Butter, Cheese ";

myList->push_back("Milk");

myList->push_back("Eggs");

myList->push_back("Butter");

myList->push_back("Cheese");

myList->display();

cout << "push_front() Flour, Sugar, Apples, Oranges ";

myList->push_front("Flour");

myList->push_front("Sugar");

myList->push_front("Apples");

myList->push_front("Oranges");

myList->display();

cout << "Erase Eggs ";

myList->erase(myList->search("Eggs"));

myList->display();

cout << "Insert Eggs before Oranges ";

myList->insert(myList->search("Oranges"),"Eggs");

myList->display();

cout << "pop_back() ";

cout << myList->pop_back() << " just back popped ";

myList->display();

cout << "pop_front() ";

cout << myList->pop_front() << " just front popped ";

myList->display();

return 0;

}

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_2

Step: 3

blur-text-image_3

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

Sybase Database Administrators Handbook

Authors: Brian Hitchcock

1st Edition

0133574776, 978-0133574777

More Books

Students also viewed these Databases questions