Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Updated Linked Lists - 5/10/2017 V4.0
//g++ 5.4.0 #include <iostream> using namespace std; class Node { public: int data; Node *next; Node(int value, Node *nxt = 0) { data = value; next = nxt; } Node() { data = 0; next = 0; } }; class List { private: Node *head, *tail; public: List() { head = tail = 0; } void Add2Head(int value) { Node *n = new Node(value, 0); if (head == 0) { head = tail = n; } else { n->next = head; head = n; } } void Add2Tail(int value) { Node *n = new Node(value, 0); if (head == 0) { head = tail = n; } else { tail->next = n; tail = n; } } int Count() { int s = 0; Node *n = head; while (n != 0) { s++; n = n->next; } return s; } void Print() { int i = 0; Node *n = head; while (n != 0) { cout << i << ". " << (n->data) << endl; n = n->next; i++; } } void PrintInReverse() { Node *n; for (int i = (this->Count() - 1); i >= 0; i--) { n = head; for (int j = 0; j<i; j++) { n = n->next; } cout << i << ". " << (n->data) << endl; } } Node* operator[](int index) { Node *n = head; int i = 0; while (i<index) { n = n->next; i++; } return n; } Node* Access(int index) { Node *n = head; int i = 0; while (i<index) { n = n->next; i++; } return n; } void DeleteHead() { Node *temp = new Node(); if (head == 0) { return; } else if (head == tail) { head = tail = 0; } else { temp = head->next; delete head; head = temp; } } void DeleteTail() { if (head == 0) { return; } else if (head == tail) { delete head; head = tail = 0; } else { Node *n = this->Access(this->Count() - 2); n->next = 0; delete tail; tail = n; /* Node *n=head; while (n->next!=tail) { n=n->next; } n->next=0; delete tail; tail=n; */ } } void DeleteNodeAt(int index) { Node *before = this->Access(index - 1); Node *after = this->Access(index + 1); before->next = after; } void InsertNodeAt(int index, int value) { Node *before = this->Access(index - 1); Node *temp = this->Access(index); Node *n = new Node(value, temp); before->next = n; } void SortList() { Node *n = new Node(); for (int i = 0; i < this->Count(); i++) { for (int j = i; j < this->Count(); j++) { if (i == j) { continue; } else { if (this->Access(i)->data > this->Access(j)->data) { n->data = this->Access(i)->data; this->Access(i)->data = this->Access(j)->data; this->Access(j)->data = n->data; } } } } } bool KeyExist(int key) { int i = 0; while (i < this->Count()) { if (this->Access(i)->data == key) { return true; } i++; } return false; } int IndexOf(int key) { int i = 0; int index = -1; while (i < this->Count()) { if (this->Access(i)->data == key) { index = i; break; } i++; } return index; } int LastIndexOf(int key) { int i = 0; int index = -1; while (i< this->Count()) { if (this->Access(i)->data == key) { index = i; } i++; } return index; } int* IndicesOf(int key, int &length) { length = 0; int i = 0; while (i< this->Count()) { if (this->Access(i)->data == key) { length++; } i++; } int *indices = new int[length]; int index = 0; i = 0; while (i< this->Count()) { if (this->Access(i)->data == key) { indices[index] = i; index++; } i++; } return indices; } bool IsSorted() { int i = 0; bool __IsSorted = true; while (i < this->Count() - 1) { if (!(this->Access(i)->data <= this->Access(i + 1)->data)) { __IsSorted = false; break; } i++; } return __IsSorted; } void InsertNodeInSortedList(int value) { int i = 0; int index = 0; if (this->IsSorted()) { while (i < this->Count()) { if (this->Access(i)->data <= value) { index++; } i++; } this->InsertNodeAt(index, value); } else { this->SortList(); while (i < this->Count()) { if (this->Access(i)->data <= value) { index++; } i++; } this->InsertNodeAt(index, value); } } void DeleteNodeByKey(int value) { int i = 0; while (i < this->Count()) { if (this->Access(i)->data == value) { this->DeleteNodeAt(i); } i++; } } /* ~List() { Node *temp = head; while (temp != 0) { delete temp; temp = temp->next; } delete temp; head = tail = 0; } */ }; int main() { /* 9 5 10 8 5 9 9 9 8 10 */ List l; l.Add2Head(9); l.Add2Head(5); l.Add2Head(10); l.Add2Head(9); l.Add2Tail(8); //l.SortList(); l.InsertNodeInSortedList(7); for (int i = 0; i < l.Count(); i++) { cout << (l[i])->data << "\t" << (l[i])->next << endl; } cout << "--------------------------" << endl; l.DeleteNodeByKey(9); for (int i = 0; i < l.Count(); i++) { cout << (l[i])->data << "\t" << (l[i])->next << endl; } //system("pause"); }
run
|
edit
|
history
|
help
0
Dejalo a la Suerte
Meeting_Leandro
Cpp update 1
stl_sizeof.cc
11 და 16 აპრილს დამუსავებული
Kth smallest element
Lockable static queue
Вывод элементов массива
FindKthElementDivideConquer
Sort 0's 1's 2's