Skip to content

Commit c73724d

Browse files
Merge pull request #744 from picolloo/master
subdirectory added
2 parents 7a7aed8 + d16c050 commit c73724d

File tree

2 files changed

+254
-0
lines changed

2 files changed

+254
-0
lines changed
Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
#include "LinkedList.h"
2+
#include <string>
3+
#include <sstream>
4+
#include <algorithm> //swap
5+
6+
LinkedList::Element* LinkedList::seekIndex(int index)
7+
{
8+
if ((index < 0) || (index >= size))
9+
return nullptr;
10+
11+
Element* a= first;
12+
for (int i= 0 ; i < index ; i++)
13+
a= a->next;
14+
15+
return a;
16+
}
17+
18+
void LinkedList::addEnd(int cod)
19+
{
20+
Element* e= new Element(cod);
21+
22+
if (size != 0) {
23+
Element* a= last;
24+
a->next= e;
25+
e->prev= a;
26+
}
27+
else
28+
first= e;
29+
30+
last= e;
31+
size++;
32+
}
33+
34+
void LinkedList::addBeggin(int cod)
35+
{
36+
Element* e= new Element(cod);
37+
38+
if (size != 0) {
39+
Element* a= first;
40+
e->next= a;
41+
a->prev= e;
42+
}
43+
else
44+
last= e;
45+
46+
first= e;
47+
size++;
48+
}
49+
50+
bool LinkedList::addIndex(int cod, int index)
51+
{
52+
Element* aIndex= nullptr;
53+
54+
if (index <= 0) {
55+
addBeggin(cod);
56+
return true;
57+
}
58+
else if (index >= size) {
59+
addEnd(cod);
60+
return true;
61+
}
62+
else {
63+
aIndex= seekIndex(index);
64+
if (!aIndex)
65+
return false;
66+
}
67+
68+
Element* e = new Element(cod);
69+
Element* aPrevious= aIndex->prev;
70+
e->next= aIndex;
71+
e->prev= aPrevious;
72+
aPrevious->next= e;
73+
74+
size++;
75+
return true;
76+
}
77+
78+
bool LinkedList::deleteElement(int index)
79+
{
80+
Element* aIndex= seekIndex(index);
81+
82+
if (!aIndex)
83+
return false;
84+
85+
Element* aPrev= aIndex->prev;
86+
Element* aNext= aIndex->next;
87+
88+
if (aPrev)
89+
aPrev->next= aNext;
90+
if (aNext)
91+
aNext->prev= aPrev;
92+
93+
if (first == aIndex)
94+
first= aNext;
95+
if (last == aIndex)
96+
last= aPrev;
97+
98+
delete aIndex;
99+
size--;
100+
101+
return true;
102+
}
103+
104+
void LinkedList::deleteAllElements()
105+
{
106+
Element* a= first;
107+
108+
while (a) {
109+
Element* e= a->next;
110+
delete a;
111+
a= e;
112+
}
113+
114+
size= 0;
115+
first= last= nullptr;
116+
}
117+
118+
bool LinkedList::setElement(int cod, int index)
119+
{
120+
Element* aIndex= seekIndex(index);
121+
122+
bool ok= (aIndex != nullptr);
123+
if (ok)
124+
aIndex->cod= cod;
125+
126+
return ok;
127+
}
128+
129+
std::string LinkedList::listAll()
130+
{
131+
int i= 0;
132+
std::ostringstream aux;
133+
134+
for (Element* a= first ; a ; a= a->next) {
135+
aux << "Position: " << i++ << " --- Cod: " << a->cod;
136+
if (a->next)
137+
aux << "\n";
138+
}
139+
140+
if (!size)
141+
aux << "Linked list is empty.";
142+
143+
return aux.str();
144+
}
145+
146+
std::string LinkedList::listAllReverse()
147+
{
148+
Element* a= last;
149+
int i= size;
150+
std::ostringstream aux;
151+
152+
while (a) {
153+
Element* e= a->prev;
154+
155+
aux << "Position: " << --i << " --- Cod: " << a->cod;
156+
if (e)
157+
aux << "\n";
158+
159+
a= e;
160+
}
161+
162+
if (!size)
163+
aux << "Linked list is empty.";
164+
165+
return aux.str();
166+
}
167+
168+
void LinkedList::orderByCode()
169+
{
170+
Element* b;
171+
Element* a= first;
172+
173+
for (a= first ; a ; a= a->next) {
174+
for (b= a->next ; b ; b= b->next) {
175+
if (a->cod > b->cod)
176+
std::swap(a->cod, b->cod);
177+
}
178+
}
179+
}
180+
181+
void LinkedList::concatenateList(LinkedList& l1)
182+
{
183+
if (last)
184+
last->next= l1.first;
185+
if (l1.first)
186+
l1.first->prev= last;
187+
if (!first)
188+
first= l1.first;
189+
last= l1.last;
190+
191+
size += l1.size;
192+
193+
l1.size= 0;
194+
l1.first= l1.last = nullptr;
195+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#pragma once
2+
#ifndef INCLUDED_LINKEDLIST_H
3+
#define INCLUDED_LINKEDLIST_H
4+
5+
#include <string>
6+
7+
class LinkedList {
8+
private:
9+
struct Element {
10+
int cod;
11+
Element* next;
12+
Element* prev;
13+
14+
Element(): cod(0), next(nullptr), prev(nullptr) {}
15+
Element(int c): cod(c), next(nullptr), prev(nullptr) {}
16+
17+
int getCod() { return cod; }
18+
void setCod(int c) { cod= c; }
19+
};
20+
std::string name;
21+
Element* first;
22+
Element* last;
23+
int size;
24+
25+
// returns the element at index when index is valid or nullptr otherwise
26+
Element* seekIndex(int index);
27+
28+
public:
29+
~LinkedList() { if (size > 0) deleteAllElements(); }
30+
LinkedList(std::string n) {
31+
name= n;
32+
size= 0;
33+
first= last= nullptr;
34+
}
35+
36+
void addEnd(int value);
37+
void addBeggin(int value);
38+
bool addIndex(int cod, int index);
39+
40+
void deleteAllElements();
41+
bool deleteElement(int index);
42+
43+
void orderByCode();
44+
45+
int getSize() { return size; }
46+
47+
Element* getLast(){ return last; }
48+
Element* getFirst(){ return first; }
49+
50+
bool setElement(int cod, int index);
51+
std::string getName() { return name; }
52+
53+
std::string listAll();
54+
std::string listAllReverse();
55+
56+
void concatenateList(LinkedList&);
57+
};
58+
59+
#endif // INCLUDED_LINKEDLIST_H

0 commit comments

Comments
 (0)