Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Updated Linked Lists - 5/10/2017 V3.0
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
//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 { n->next = tail; 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; } ~List() { Node *temp=head; while (temp!=0) { delete temp; temp=temp->next; } delete temp; head=tail=0; } }; int main() { List l; l.Add2Tail(8); l.Add2Head(9); l.Add2Head(5); l.Add2Head(10); l.Add2Head(9); l.~List(); int length; cout << l.IndicesOf(9,length)[0] << "\t" << l.IndicesOf(9,length)[1] << endl; /* cout << l.Count() << endl; cout << "--------------" << endl; l.Print(); cout << "--------------" << endl; //l.DeleteNodeAt(2); //l.SortList(); l.InsertNodeAt(1, 112); cout << "--------------" << endl; //l.DeleteHead(); //l.DeleteTail(); l.Print(); cout << "--------------" << endl; l.SortList(); l.Print(); cout << l.KeyExist(10) << endl; //cout << (l[])->next << endl; Node *test; l.Access(2,test); cout << (test->data) << "\t" << (test->next) << endl; */ //system("pause"); }
g++
Show compiler warnings
[
+
] Compiler args
[
+
]
Show input
Compilation time: 0.32 sec, absolute running time: 0.12 sec, cpu time: 0.07 sec, memory peak: 3 Mb, absolute service time: 0,45 sec
edit mode
|
history
|
discussion