Linked list using pointers implementation: Runtime error (presumed due to pointers in a class)
May 10, 2013 at 9:10pm UTC
I'm trying to create a linked list using pointers.(full code at the bottom)
A node would look like this:
1 2 3 4 5 6
template <class T>
struct elem
{
T value;
struct elem<T> *prev, *next;
};
While the list itself holds the first and last nodes using pointers.And introduces some functions in which I think i repeat the same mistake so i will only include 1 function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
template <class T>
class LinkedList
{
public :
struct elem<T> *first, *last;
void addFirst(T x)
{
struct elem<T> *aux;
aux=new struct elem<T>;
aux->value=x;
aux->prev=NULL;
aux->next=first;
if (first!=NULL) first->prev=aux;
first=aux;
if (last==NULL) last=first;
}
}
When i test with something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
int main()
{
LinkedList<int > list1;
list1.addFirst(89);
list1.addFirst(34);
list1.addLast(79);
list1.addLast(34);
list1.addFirst(89);
list1.printList();
list1.removeLastOccurrence(89);
list1.printList();
list1.removeLast();
list1.removeFirst();
list1.printList();
}
It goes into a runtime error probably inside the
if (first!=NULL)
.
I don't know how to assign the first and last pointers as NULL by default.
If i do it inside the class where i declare the members it says ANSI C++ forbids initialisation of the respective members and also it makes them static.
Can anyone help me?
full code:
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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
#include <iostream.h>
template <class T>
struct elem
{
T value;
struct elem *prev, *next;
};
template <class T>
class LinkedList
{
public :
struct elem<T> *first=NULL, *last=NULL;
void addFirst(T x)
{
struct elem<T> *aux;
aux=new struct elem<T>;
aux->value=x;
aux->prev=NULL;
aux->next=first;
if (first!=NULL) first->prev=aux;
first=aux;
if (last==NULL) last=first;
}
void addLast(T x)
{
struct elem<T> *aux;
aux=new struct elem<T>;
aux->value=x;
aux->prev=last;
aux->next=NULL;
if (last!=NULL) last->next=aux;
last=aux;
if (first==NULL) first=last;
}
void removeFirst()
{
struct elem<T> *aux;
if (first!=NULL)
{
aux=first;
if (aux->next==NULL)
{
first=NULL;
last=NULL;
delete aux;
}
else
{
first=aux->next;
first->prev=NULL;
delete aux;
}
}
else cout<<"Linked list is empty\r\n" ;
}
void removeLast()
{
struct elem<T> *aux;
if (last!=NULL)
{
aux=last;
if (aux->prev==NULL)
{
first=NULL;
last=NULL;
delete aux;
}
else
{
last=aux->prev;
last->next=NULL;
delete aux;
}
}
else cout<<"Linked list is empty\r\n" ;
}
struct elem<T>* findFirstOccurrence(T x)
{
struct elem<T>* aux;
aux=first;
while (aux!=NULL)
{
if (aux->value==x) return aux;
aux=aux->next;
}
return NULL;
}
struct elem<T>* findLastOccurence(T x)
{
struct elem<T>* aux;
aux=last;
while (aux!=NULL)
{
if (aux->value==x) return aux;
aux=aux->prev;
}
return NULL;
}
void removeFirstOccurrence(T x)
{
struct elem<T>* aux;
aux=first;
while (aux!=NULL)
{
if (aux->value==x)
{
if (aux==first) {this .removeFirst(); break ;}
if (aux==last) {this .removeLast(); break ;}
aux->next->prev=aux->prev;
aux->prev->next=aux->next;
delete aux;
break ;
}
}
}
void removeLastOccurrence(T x)
{
struct elem<T>* aux;
aux=last;
while (aux!=NULL)
{
if (aux->value==x)
{
if (aux==first) {this ->removeFirst(); break ;}
if (aux==last) {this ->removeLast(); break ;}
aux->next->prev=aux->prev;
aux->prev->next=aux->next;
delete aux;
break ;
}
}
}
int isEmpty()
{
if (first==last==NULL) return 1;
else return 0;
}
void printList()
{
struct elem<T>* aux;
aux=first;
while (aux!=NULL)
{
cout<<aux->value<<" " ;
aux=aux->next;
}
}
};
int main()
{
LinkedList<int > list1;
list1.addFirst(89);
list1.addFirst(34);
list1.addLast(79);
list1.addLast(34);
list1.addFirst(89);
list1.printList();
list1.removeLastOccurrence(89);
list1.printList();
list1.removeLast();
list1.removeFirst();
list1.printList();
}
Last edited on May 10, 2013 at 9:30pm UTC
May 10, 2013 at 9:30pm UTC
Write constructors for elem and LinkedList .
May 10, 2013 at 10:01pm UTC
I WUB YOU keskiverto. Ty! Lol, so much derp. This was my first C++ program so I still forget things that i should not.Ty very much.
Last edited on May 10, 2013 at 10:02pm UTC
Topic archived. No new replies allowed.