Im trying to write a double linked list with a head and tail node. The program takes in user input (only chars). Each char gets added as a node to the list. If the user enters in the '#' char, then it will delete the last item on the list. At the very end of the program, it will print out the final result. So for an example it may look something like:
input: abcd#
output: abc
or
input: abcd##
output: ab
The problem I am have is the function to remove the last node. The program appears to add nodes correctly. When I add the '#' char, the program enters an infinite loop in the terminal. Any help to why this is happening would be greatly appreciated. Thanks.
#ifndef LIST_H
#define LIST_H
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
usingnamespace std;
class List{
public:
// TYPE DEF
typedefchar Item;
// Functions
// Constructor
List();
//Destructor
~List();
// Returns a bool. Checks if the invoking list is empty
bool empty();
// Insert a Node at the tail
void append(Item a);
// Removes the last item from the list
void remove_last();
// Friend function
friend ostream& operator << ( ostream& out_s, const List& l );
private:
struct Node {
Item data;
Node* next;
Node* prev;
Node(Item); // This line makes it easier to create new nodes
};
Node* head;
Node* tail;
int size;
};
#endif // LIST_H