#include <stdio.h>
#include <stdlib.h>
#include <iostream.h>
usingnamespace std;
template <typename T>
struct Noeud { // structure pour un noeud de la liste; contient l'information et les 2 pointeurs
T info;
Noeud <T> *next; // pointeur au suivant noeud
Noeud <T> *prev; // pointeur au noeud precedent
};
template <typename T>
class LinkedList {
public:
Noeud <T> *pfirst; // pointeur au premier noeud
Noeud <T> *plast; // pointeur au dernier noeud
void addFirst(T x) { // fonction pour ajouter l'information x dans un nouveau noeud, sur la premiere position
Noeud <T> *paux; // le nouveau noeud
paux = new Noeud<T>;
paux->info = x;
paux->prev = NULL;
paux->next = pfirst;
if (pfirst != NULL)
pfirst->prev = paux;
pfirst = paux; // on fait la connexion entre l'ancien "first" et le nouveau noeud
if (plast==NULL)
plast=pfirst;
}
void addLast(T x) { // fonction pour ajouter l'information x dans un nouveau noeud, sur la derniere position
Noeud<T> *paux;
paux = new Noeud <T>;
paux->info = x;
paux->prev = plast;
paux->next = NULL;
if (plast != NULL)
plast->next = paux;
plast = paux;
if (pfirst == NULL)
pfirst = plast;
}
T getInfo (Noeud<T>* p){
return p->info;
}
void removeFirst() { // fonction pour supprimer le premier element
Noeud<T>* paux;
if (pfirst != NULL) {
paux = pfirst->next;
if (pfirst == plast) \
plast = NULL;
delete pfirst; // on efface le premier element
pfirst = paux; // le suivant devient le nouveau premier
if (pfirst != NULL)
pfirst->prev = NULL;
}
else cout<<"The list is empty"<<endl;
}
void removeLast() { // fonction pour supprimer le dernier element
Noeud <T> *paux;
if (plast != NULL) {
paux = plast->prev;
if (pfirst == plast) pfirst = NULL;
delete plast; // on efface le dernier element;
plast = paux; // le precedent devient le nouveau "last"
if (plast != NULL) plast->next = NULL;
}
else cout<<"The list is empty"<<endl;
}
Noeud <T>* findFirstOccurrence(T x) { // cherche la premiere apparition du noeud avec l'info x
Noeud<T> *paux;
paux = pfirst;
while (paux != NULL) {
if (paux->info == x)
return paux;
paux = paux->next;
}
return NULL;
}
Noeud <T>* findLastOccurrence(T x) { // cherche la derniere apparition du noeud avec l'info x
Noeud <T> *paux;
paux = plast;
while (paux != NULL) {
if (paux->info == x)
return paux;
paux = paux->prev;
}
return NULL;
}
void removeFirstOccurrence(T x) { // efface la premiere apparition du noeud avec l'info x
Noeud <T> *px;
px = findFirstOccurrence(x); // px est l'element qu'on doit effacer
if (px != NULL) {
// on doit refaire les connexions des pointeurs
if (px->prev != NULL)
px->prev->next = px->next;
if (px->next != NULL)
px->next->prev = px->prev;
if (px->prev == NULL) // si px == pfirst
pfirst = px->next;
if (px->next == NULL) // si px == plast
plast = px->prev;
delete px; // mainteneant on peut l'effacer
}
}
void removeLastOccurrence(T x) { // efface la derniere apparition du noeud avec l'info x
Noeud <T> *px;
px = findLastOccurrence(x);
if (px != NULL) {
if (px->prev != NULL)
px->prev->next = px->next;
if (px->next != NULL)
px->next->prev = px->prev;
if (px->prev == NULL) // si px == pfirst
pfirst = px->next;
if (px->next == NULL) // si px == plast
plast = px->prev;
delete px;
}
}
int isEmpty() { // verifie si la liste est vide
return (pfirst == NULL);
}
LinkedList() { // constructeur
pfirst = plast = NULL;
}
void printList() { // affiche le contenu de la liste
Noeud <T> *p;
p = pfirst;
while (p != NULL)
{
cout<< p->info <<" ";
p = p->next;
}
cout<<endl;
}
};
This should be your starting point. Don't try to copy whole functionality, because you don't need it: focus on what's most important.
For example, List commonly uses something like "Push" and "Pop"(commonly associated with stack), or "push_back", "pop_back"(another variant).
That's the first advice. Second advice is to write code in English - most people understand english, and it's easier to ask whole world than a specific country for help.
Third is to ask a question instead of providing a dump of your code. We don't have a time to look through the code and guess what's wrong, or what you're struggling with. Insert only the interesting snippet(s) of code, ask a clear question about a single thing(or a few, but don't make it "implement this for me"). This will make others(like me) much easier to help you.
i know that the last node need to be point at the first node
Because you are writing a doubly linked, circular list, the first node also needs to point to the last node.
When you have an empty list, pfirst and plast are both NULL.
When you have a list with a single element, both prev and next of the element point to itself, and both pfirst and plast point to the element.
If there is at least one element in the list, when you add at the front, the old pfirst->prev and plast->next will now point to the new element. The new element will point to the old pfirst (next) and plast(prev). pfirst will now point to the new element. Similar happens when adding to the end
When deleting, just work backwards. The new pfirst and plast should point to each other. When deleting the last node just set pfirst and plast to NULL.
No, I won't write the code for you. That's how you learn. If you take a crack at it and still have problems, then you can come back with more questions.
My recommendation to you is to draw some pictures. Draw an empty list and an element that needs to be added. Figure out what needs to be done to add the node. Then draw a list with 1 element and a new node to be added. Then a list with 2 nodes and a node to be added. Each time determine which links need to change and what they need to change to. If you take your time, it is actually pretty straight forward.