basic lists
unknown
c_cpp
10 months ago
4.4 kB
12
Indexable
#include <iostream>
using namespace std;
// Singly Linked list node.
struct NodeSL {
int value;
NodeSL *next;
};
// Doubly linked list node.
struct Node {
int value;
Node *prev, *next;
};
struct SingleLinkedList {
NodeSL *head, *tail;
};
struct DoubleLinkedList {
Node *head, *tail;
};
void printSL(NodeSL *head) {
if (head != nullptr) {
cout << head->value << " ";
printSL(head->next);
} else {
cout << endl;
}
}
void printSL(SingleLinkedList l) {
printSL(l.head);
}
void printSLBackwards(NodeSL *head) {
if (head != nullptr) {
printSLBackwards(head->next);
cout << head->value << " ";
}
}
void printSLBackwards(SingleLinkedList l) {
printSLBackwards(l.head);
cout << endl;
}
void printL(DoubleLinkedList l) {
Node *node = l.head;
while (node != nullptr) {
cout << node->value << " ";
node = node->next;
}
cout << endl;
}
void printLBackwards(DoubleLinkedList l) {
Node *node = l.tail;
while (node != nullptr) {
cout << node->value << " ";
node = node->prev;
}
cout << endl;
}
void printEverySecond(SingleLinkedList l) {
// Implement
}
void printEverySecondBackwards(SingleLinkedList l) {
// Implement
}
void removeLastNode(SingleLinkedList& l) {
// Implement
}
void removeLastNode(DoubleLinkedList& l) {
// Implement
}
int sizeOfList(DoubleLinkedList l) {
// Implement
return 0;
}
Node* insertAfter(DoubleLinkedList& l, Node *n, int value) {
// Implement
return nullptr;
}
Node* insertAtBegining(DoubleLinkedList& l, int value) {
// Implement using insertAfter
return nullptr;
}
Node* insertAtEnd(DoubleLinkedList& l, int value) {
// Implement using insertAfter
return nullptr;
}
void removeNode(DoubleLinkedList& l, Node *n) {
// Implement
}
int main() {
SingleLinkedList single_linked_list;
{
NodeSL *a, *b, *c;
a = new NodeSL();
b = new NodeSL();
c = new NodeSL();
a->next = b;
b->next = c;
c->next = nullptr;
a->value = 4;
b->value = 2;
c->value = 0;
single_linked_list.head = a;
}
printSL(single_linked_list);
printSLBackwards(single_linked_list);
DoubleLinkedList double_linked_list;
{
Node *a, *b, *c, *d;
a = new Node();
b = new Node();
c = new Node();
d = new Node();
a->next = b;
a->prev = nullptr;
b->next = c;
b->prev = a;
c->next = d;
c->prev = b;
d->next = nullptr;
d->prev = c;
a->value = 2;
b->value = 1;
c->value = 3;
d->value = 7;
double_linked_list.head = a;
double_linked_list.tail = d;
}
printL(double_linked_list);
printLBackwards(double_linked_list);
cout << "print every second: \n";
// Should print 4 0
printEverySecond(single_linked_list);
cout << "print every second backwards: \n";
// Should print 0 4
printEverySecondBackwards(single_linked_list);
cout << "size of dll: ";
// Should print 4
cout << sizeOfList(double_linked_list) << endl;
cout << "remove last node from dll, now it's backwards: ";
removeLastNode(double_linked_list);
// Should print 3 1 2
printLBackwards(double_linked_list);
cout << "insert 5 at begining of dll, now it's: ";
insertAtBegining(double_linked_list, 5);
printL(double_linked_list);
cout << "insert 7 at end of dll, now it's: ";
insertAtEnd(double_linked_list, 7);
printL(double_linked_list);
{
Node *second = double_linked_list.head->next;
// Should be 2
cout << "second node of dll: " << second->value << endl;
cout << "inserting 17 after the second node, now dll is: ";
Node* n17 = insertAfter(double_linked_list, second, 17);
printL(double_linked_list);
cout << "inserting 11 after 17, now dll is: ";
insertAfter(double_linked_list, n17, 19);
cout << "removing second node, now dll is: ";
removeNode(double_linked_list, second);
printL(double_linked_list);
}
cout << "removing first node from dll, now it's: ";
removeNode(double_linked_list, double_linked_list.head);
printL(double_linked_list);
return 0;
}Editor is loading...
Leave a Comment