Rafael Arce Nazario 5 years ago
commit
bd745abc56
4 changed files with 160 additions and 0 deletions
  1. 119
    0
      Llist.cpp
  2. 2
    0
      Makefile
  3. 1
    0
      README.md
  4. 38
    0
      main.cpp

+ 119
- 0
Llist.cpp View File

@@ -0,0 +1,119 @@
1
+#include "Llist.h"
2
+
3
+void Llist::append(ElementType d){
4
+  Node *n = new Node(d);
5
+
6
+  // in case of empty list
7
+  if (!head) {
8
+    head = n;
9
+  }
10
+  // otherwise, non empty list...
11
+  else {
12
+    tail->next = n;
13
+  }
14
+  tail = n;
15
+  length++;
16
+}
17
+
18
+void Llist::prepend(ElementType d) {
19
+  Node *n = new Node(d, head);
20
+  
21
+  head = n;
22
+  // in case of empty list the tail = head = n
23
+  if (length == 0) tail = n;
24
+
25
+  length++;
26
+}
27
+
28
+void Llist::display(ostream &out) const {
29
+  Node *p = head;
30
+  while(p) {
31
+    out << p->data << " ";
32
+    p = p->next;
33
+  }
34
+}
35
+
36
+void Llist::insertAfter(Node *p, ElementType d) {
37
+  Node *n;
38
+  if (p == NULL) {
39
+    n = new Node(d, head);
40
+    head = n;
41
+    if (!tail) tail = n;
42
+  }
43
+  else {
44
+    n = new Node (d, p->next);
45
+    p->next = n;
46
+    if (tail == p) tail = n;
47
+  }
48
+  length++;
49
+}
50
+
51
+Node* Llist::search(ElementType d) const {
52
+  Node *p = head;
53
+  while(p) {
54
+    if (p->data == d) return p;
55
+    p = p->next;
56
+  }
57
+  return NULL;
58
+}
59
+
60
+ostream& operator<< (ostream &out, const Llist& L) {
61
+  L.display(out);
62
+  return out;
63
+} 
64
+
65
+void Llist::printRev() const {
66
+  printRev(head); 
67
+}
68
+void Llist::printRev(Node *p) const {
69
+  if (p != NULL) {
70
+    printRev(p->next);
71
+    cout << p->data << endl;
72
+  } 
73
+}
74
+
75
+void Llist::removeAfter(Node *cur) {
76
+  Node *suc;
77
+  if (cur == NULL and head != NULL) {
78
+    suc = head->next;
79
+    delete head;
80
+    head = suc;
81
+
82
+    if (suc == NULL) tail = NULL;
83
+  }
84
+  else if (cur->next != NULL) {
85
+    suc = cur->next->next;
86
+    delete cur->next;
87
+    cur->next = suc;    
88
+    if (suc == NULL) tail = cur;
89
+  }
90
+  length--;
91
+}
92
+
93
+Llist& Llist::operator=(const Llist &L) {
94
+  if (this != &L) {
95
+    while(head != NULL) removeAfter(NULL);
96
+    Node *cur = L.head;
97
+    while (cur != NULL) {
98
+      append(cur->data);
99
+      cur = cur->next;
100
+    }
101
+  }
102
+  return *this;
103
+}
104
+
105
+Llist::Llist(const Llist &L) {
106
+  head = tail = NULL;
107
+  length = 0;
108
+  if (this != &L) {
109
+    Node *cur = L.head;
110
+    while (cur != NULL) {
111
+      append(cur->data);
112
+      cur = cur->next;
113
+    }
114
+  }
115
+}
116
+
117
+Llist::~Llist() {
118
+  while(head != NULL) removeAfter(NULL);
119
+}

+ 2
- 0
Makefile View File

@@ -0,0 +1,2 @@
1
+all: main.cpp Llist.h Llist.cpp
2
+	g++ -o llist main.cpp Llist.cpp

+ 1
- 0
README.md View File

@@ -0,0 +1 @@
1
+List ADT implemented using single linked list

+ 38
- 0
main.cpp View File

@@ -0,0 +1,38 @@
1
+ #include <iostream>
2
+#include "Llist.h"
3
+using namespace std;
4
+int main() {
5
+  Llist L;
6
+  L.insertAfter(NULL, 9);
7
+  L.insertAfter(NULL, 5);
8
+  // L.append(9);
9
+  L.append(42);
10
+  L.prepend(3);
11
+  L.prepend(1);
12
+
13
+  L.insertAfter(L.search(9),55);
14
+  L.insertAfter(L.search(55),85);
15
+  L.insertAfter(L.search(3),3);
16
+  L.insertAfter(L.search(3),7);
17
+  cout << L << endl;
18
+  // L.printRev();
19
+  L.removeAfter(L.search(9));
20
+  cout << L << endl;
21
+
22
+  Llist M(L);
23
+  // Llist M;
24
+  // M = L;
25
+
26
+  cout << M << endl;
27
+  M.append(19);
28
+  M.prepend(119);
29
+  M.insertAfter(M.search(3),59);
30
+  cout << M << endl;
31
+  cout << L << endl;
32
+
33
+  while (L.getLength() > 0) { 
34
+    L.removeAfter(NULL);
35
+    cout << L << endl;
36
+  }
37
+
38
+}