How to implement Multi-Linked List?
I am trying to implement Multi-Linked list for reading the information of files in a directory. The visulation of the project is below. There will be 3 linked lists. One for the size of the files, one for the name of the files and one for the extension of the files.
The first thing I did was, I seperated the informations. For example (.txt, .exe, .cpp) and (500bytes, 1000bytes, 200bytes) and also (name1, name2, name3). Then I created 3 seperate linked lists but it didnt work for me because I need to implement functions such as findInfo which finds all the informations about a given name and displays. Since, 3 linked lists are seperate from each other, I couldnt access the data. So I was wrong.
I have the following header file:
#include <iostream>
using namespace std;
template <class T>
class LinkedSLLNode {
public:
LinkedSLLNode() {
next = 0;
}
LinkedSLLNode(const T& el, LinkedSLLNode<T> *ptr = 0) {
info = el; next = ptr;
}
T info;
LinkedSLLNode<T> *next;
};
template<class T>
class LinkedSLList {
public:
LinkedSLList() {
head = tail = 0;
}
~LinkedSLList();
int isEmpty() {
return head == 0;
}
void addToHead(const T&);
void addToTail(const T&);
T deleteFromHead();
T deleteFromTail();
void deleteNode(const T&);
bool isInList(const T&) const;
void printAll() const;
private:
LinkedSLLNode<T> *head, *tail;
};
template<class T>
LinkedSLList<T>::~LinkedSLList() {
for (LinkedSLLNode<T> *p; !isEmpty(); ) {
p = head->next;
delete head;
head = p;
}
}
template<class T>
void LinkedSLList<T>::addToHead(const T& el) {
head = new LinkedSLLNode<T>(el,head);
if (tail == 0)
tail = head;
}
template<class T>
void LinkedSLList<T>::addToTail(const T& el) {
if (tail != 0) { // if list not empty;
tail->next = new LinkedSLLNode<T>(el);
tail = tail->next;
}
else head = tail = new LinkedSLLNode<T>(el);
}
template<class T>
T LinkedSLList<T>::deleteFromHead() {
int el = head->info;
LinkedSLLNode<T> *tmp = head;
if (head == tail) // if only one node on the list;
head = tail = 0;
else head = head->next;
delete tmp;
return el;
}
template<class T>
T LinkedSLList<T>::deleteFromTail() {
int el = tail->info;
if (head == tail) { // if only one node on the list;
delete head;
head = tail = 0;
}
else { // if more than one node in the list,
LinkedSLLNode<T> *tmp; // find the predecessor of tail;
for (tmp = head; tmp->next != tail; tmp = tmp->next);
delete tail;
tail = tmp; // the predecessor of tail becomes tail;
tail->next = 0;
}
return el;
}
template<class T>
void LinkedSLList<T>::deleteNode(const T& el) {
if (head != 0) { // if non-empty list;
if (head == tail && el == head->info) { // if only one
delete head; // node on the list;
head = tail = 0;
}
else if (el == head->info) { // if more than one node on the list
LinkedSLLNode<T> *tmp = head;
head = head->next;
delete tmp; // and old head is deleted;
}
else { // if more than one node in the list
LinkedSLLNode<T> *pred, *tmp;
for (pred = head, tmp = head->next; // and a non-head node
tmp != 0 && !(tmp->info == el); // is deleted;
pred = pred->next, tmp = tmp->next);
if (tmp != 0) {
pred->next = tmp->next;
if (tmp == tail) {
tail = pred;
}
delete tmp;
}
}
}
}
template<class T>
bool LinkedSLList<T>::isInList(const T& el) const {
LinkedSLLNode<T> *tmp;
for (tmp = head; tmp != 0 && !(tmp->info == el); tmp = tmp->next);
return tmp != 0;
}
template<class T>
void LinkedSLList<T>::printAll() const {
for (LinkedSLLNode<T> *tmp = head; tmp != 0; tmp = tmp->next) {
cout << tmp->info << " ";
}
cout << endl;
}
I can create separete linked lists without any bug with this header file. But I dont know how to create multi level linked list with this file. Since, size and name will have different variable types, I dont know how to do it.
do you know?
how many words do you know