Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- using namespace std;
- class Numb {
- private:
- int value;
- Numb *next;
- Numb *prev;
- public:
- Numb(): value(0), next(NULL), prev(NULL) {}
- Numb(int a): value(a), next(NULL), prev(NULL) {}
- ~Numb() {}
- void read() {
- cout << "Enter value: ";
- cin >> value;
- }
- void show() {
- cout << "Value is : " << value << endl;
- }
- int getValue() { return value; }
- void setNext(Numb *N) { next = N; }
- void setPrev(Numb *P) { prev = P; }
- Numb * getNext() { return next; }
- Numb * getPrev() { return prev; }
- };
- int main() {
- Numb *Head = new Numb();
- Numb *Tail;
- Numb *Current;
- Numb *Previous = Head;
- int choice;
- while(true) {
- cout << "Enter Choice: " << endl;
- cout << "1. Insert (After Head)" << endl;
- cout << "2. Show All" << endl;
- cout << "3. Search (Search for a specefic integer data)" << endl;
- cout << "4. Delete (Find it and Delete it)" << endl;
- cout << "5. Length (Current length of the list)" << endl;
- cout << "0. Exit" << endl;
- cout << "Choice: ";
- cin >> choice;
- if(choice == 0)
- exit(1);
- else if(choice == 1) {
- while(true) {
- Current = new Numb();
- Current -> read();
- Previous -> setNext(Current);
- Current -> setPrev(Previous);
- char cont;
- cout << "Do you want to continue (y/n)? ";
- cin >> cont;
- if(cont == 'n' || cont == 'N') {
- Tail = Current;
- break;
- }
- Previous = Current;
- }
- }
- else if(choice == 2)
- for(Current = Head; Current; Current = Current -> getNext())
- Current -> show();
- else if(choice == 3) {
- int searchValue;
- cout << "Enter Value to Find: ";
- int i;
- cin >> searchValue;
- for(i = 1, Current = Head; Current; i++, Current = Current -> getNext())
- if(Current -> getValue() == searchValue) {
- cout << "Found in position: " << i << endl;
- break;
- }
- }
- else if(choice == 4) {
- int searchValue;
- cout << "Enter Value to Delete: ";
- cin >> searchValue;
- for(Current = Head; Current; Current = Current -> getNext()) {
- if(Current -> getValue() == searchValue) {
- if(Current == Head) {
- Head = Current -> getNext();
- Head -> setPrev(NULL);
- }
- else if(Current == Tail) {
- Tail = Current -> getPrev();
- Tail -> setNext(NULL);
- }
- else {
- Previous -> setNext(Current -> getNext());
- (Current -> getNext()) -> setPrev(Previous);
- }
- delete Current;
- cout << "Deletion Confirmed" << endl;
- break;
- }
- else
- Previous = Current;
- }
- }
- else if(choice == 5) {
- int len = 0;
- for(Current = Head; Current; Current = Current -> getNext())
- len++;
- cout << "Length of the list is: " << len << endl;
- }
- int bak = 1;
- cout << "Enter 0 to go back: ";
- while(bak)
- cin >> bak;
- cout << endl << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement