Implement linked list to circular singly linked !

Hello , i want to implement this header file to get a simply linked circular list ,

i know that the last node need to be point at the first node , can you please implement this code with me ?

Thank you for your help

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
180
181
182
183
184
185
186
187
#include <stdio.h>
#include <stdlib.h>
#include <iostream.h>

using namespace 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;
		}
};
Last edited on
Look at C++'s List interface: http://www.cplusplus.com/reference/list/list/

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.
@MatthewRock Thank you so much for your reply ,

and thank you for your advices .

Well , i want to change the functions of addFirst() , addLast() , removefirst() , removeLast()

in order to make a singly linked circular list !

Thank you
Last edited on
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.
@doug4 , thank you so much for your explination , i've a bad english , can you please write the code for me ? to understand it better

Thank you so much !
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.
Topic archived. No new replies allowed.