Doubly linked list implementation

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.

Here is my code that I have so far:

Client:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include "List.h"

using namespace std;
int const MAX = 100;


void change_list(char* inputArr);


int main()
{
    char user_input[MAX];

    cout << "Input:   ";

    cin.get(user_input, 99);

    change_list(user_input);

    return 0;
}

void change_list(char* inputArr){

    List l;

    for(int i = 0; inputArr[i] != '\0'; i++){

        if(isalpha(inputArr[i])){
           l.append(inputArr[i]);
           }
        if(inputArr[i] == '#'){
            l.remove_last();
           }
    }
cout << l;
}


Header:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57

#ifndef LIST_H
#define LIST_H
#include <stdio.h>
#include <stdlib.h>
#include <iostream>

using namespace std;

class List{

    public:
    // TYPE DEF
    typedef char 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


Implementation:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86

#include "List.h"
#include <iostream>
using namespace std;


List::List(){

head = NULL; // Set the list to be empty
tail = NULL;
size = 0;
}

  List::Node::Node(Item a)       //Parameterized Constructor
{
    data = a;
    next = prev = NULL;
}
List::~List(){

}

bool List::empty(){

     if(size == 0)
        return true;
     else
        return false;

}

void List::append(Item a){ // Appends an item to the tail of the list


    Node* temp = new Node(a);

    if(tail == NULL){
        head = tail = temp;
    }else{

        tail -> next = temp;
        temp -> prev = tail;
        tail = temp;
    }
    size++;
}

void List::remove_last(){

    if(!empty()){
        Node* temp = tail; // Save address of Node to delete
        
        if (head == tail){
            head = NULL;
            tail = NULL;
            delete temp;
            size = 0;
        }

        tail = temp -> prev; // Point tail at the new last Node in the list
        tail -> next = NULL;
        delete temp;

        size--;

    }

}

// Friend function

ostream& operator << (ostream& out_s, const List& l){

    List::Node* cursor;

    for (cursor = l.head; cursor != NULL && cursor -> next != NULL; cursor = cursor -> next){
        out_s << cursor -> data;
    }

    if( cursor != NULL){
        out_s << cursor -> data;
    }
    return out_s;

}
Last edited on
Solved this. Anyone wondering, I had to change temp in line 60 of the List.cpp file to tail
Topic archived. No new replies allowed.