Linked List Class

Run Settings
LanguageC++
Language Version
Run Command
#include <iostream> using namespace std; struct item{ int num; item* nxt; }; class LinkedList{ private: item* head; item* tail; public: LinkedList(){ head = NULL; tail = NULL; } void insert(int x){ item *N = new item; //create item N->num = x; //assign the number N->nxt = NULL; //point it to nxt if(head == NULL) //nothing in the list { head = N; tail = N; return; } item* ptr = head; while(ptr->nxt) ptr = ptr->nxt; ptr->nxt = N; return; } void printList(){ item* ptr = head; cout << endl; while(ptr){ cout << ptr->num << "->"; ptr = ptr->nxt; } cout << endl; } void reverse(){ item * rev = head->nxt; item* R = rev->nxt; tail = head; head->nxt = NULL; while(rev) { rev->nxt = head; head = rev; rev = R; if(R) R=R->nxt; } cout << "Reversed Below:" << endl; printList(); } void deleteItem(int x){ item* d = head; item* b = d->nxt; if(head->num == x)//if its at head; { head = head->nxt; delete d; return; } while(b->num != x) { d=d->nxt; b=b->nxt; } d->nxt = b->nxt; delete b; d = NULL; delete d; printList(); return; } }; int main() { LinkedList B; B.insert(8); B.insert(7); B.insert(1); B.insert(56); B.insert(82); B.insert(71); B.printList(); B.reverse(); B.reverse(); B.deleteItem(56); return 0; }
Editor Settings
Theme
Key bindings
Full width
Lines