linked_list.cpp 3.5 KB
Newer Older
Y
yanglbme 已提交
1
#include <iostream>
M
Marian Gusatu 已提交
2 3

struct node {
M
Marian Gusatu 已提交
4 5
        int val;
        node *next;
C
Christian Bender 已提交
6 7 8 9
};

node *start;

M
Marian Gusatu 已提交
10
void insert(int x) {
M
Marian Gusatu 已提交
11
        node *t = start;
C
Christian Clauss 已提交
12 13 14
        node *n = new node;
        n->val = x;
        n->next = NULL;
M
Marian Gusatu 已提交
15 16
        if (start != NULL) {
                while (t->next != NULL) {
M
Marian Gusatu 已提交
17 18
                        t = t->next;
                }
19
                t->next = n;             
M
Marian Gusatu 已提交
20
        } else {
M
Marian Gusatu 已提交
21 22
                start = n;
        }
C
Christian Clauss 已提交
23
        
C
Christian Bender 已提交
24 25
}

M
Marian Gusatu 已提交
26 27
void remove(int x) {
        if (start == NULL) {
M
Marian Gusatu 已提交
28
                std::cout << "\nLinked List is empty\n";
M
Marian Gusatu 已提交
29
                return;
M
Marian Gusatu 已提交
30
        } else if (start->val == x) {
M
Marian Gusatu 已提交
31 32 33 34 35
                node *temp = start;
                start = start->next;
                delete temp;
                return;
        }
M
Marian Gusatu 已提交
36

M
Marian Gusatu 已提交
37
        node *temp = start, *parent = start;
A
Ashwek Swamy 已提交
38

M
Marian Gusatu 已提交
39
        while (temp != NULL && temp->val != x) {
M
Marian Gusatu 已提交
40 41 42
                parent = temp;
                temp = temp->next;
        }
A
Ashwek Swamy 已提交
43

M
Marian Gusatu 已提交
44
        if (temp == NULL) {
M
Marian Gusatu 已提交
45
                std::cout << std::endl << x << " not found in list\n";
M
Marian Gusatu 已提交
46 47
                return;
        }
M
Marian Gusatu 已提交
48 49

        parent->next = temp->next;
M
Marian Gusatu 已提交
50
        delete temp;
C
Christian Bender 已提交
51 52
}

M
Marian Gusatu 已提交
53
void search(int x) {
M
Marian Gusatu 已提交
54 55
        node *t = start;
        int found = 0;
M
Marian Gusatu 已提交
56 57
        while (t != NULL) {
                if (t->val == x) {
M
Marian Gusatu 已提交
58
                        std::cout << "\nFound";
M
Marian Gusatu 已提交
59 60 61 62 63
                        found = 1;
                        break;
                }
                t = t->next;
        }
M
Marian Gusatu 已提交
64
        if (found == 0) {
M
Marian Gusatu 已提交
65
                std::cout << "\nNot Found";
M
Marian Gusatu 已提交
66
        }
C
Christian Bender 已提交
67 68
}

M
Marian Gusatu 已提交
69
void show() {
M
Marian Gusatu 已提交
70
        node *t = start;
M
Marian Gusatu 已提交
71
        while (t != NULL) {
M
Marian Gusatu 已提交
72
                std::cout << t->val << "\t";
M
Marian Gusatu 已提交
73 74
                t = t->next;
        }
C
Christian Bender 已提交
75 76
}

M
Marian Gusatu 已提交
77
void reverse() {
M
Marian Gusatu 已提交
78
        node *first = start;
M
Marian Gusatu 已提交
79
        if (first != NULL) {
M
Marian Gusatu 已提交
80
                node *second = first->next;
M
Marian Gusatu 已提交
81
                while (second != NULL) {
M
Marian Gusatu 已提交
82 83 84 85 86
                        node *tem = second->next;
                        second->next = first;
                        first = second;
                        second = tem;
                }
M
Marian Gusatu 已提交
87 88
                start->next = NULL;
                start = first;
M
Marian Gusatu 已提交
89
        } else {
M
Marian Gusatu 已提交
90
                std::cout << "\nEmpty list";
M
Marian Gusatu 已提交
91
        }
K
Khavin Shankar 已提交
92 93
}

M
Marian Gusatu 已提交
94
int main() {
M
Marian Gusatu 已提交
95
        int choice, x;
M
Marian Gusatu 已提交
96
        do {
M
Marian Gusatu 已提交
97 98 99 100 101 102 103 104
                std::cout << "\n1. Insert";
                std::cout << "\n2. Delete";
                std::cout << "\n3. Search";
                std::cout << "\n4. Print";
                std::cout << "\n5. Reverse";
                std::cout << "\n0. Exit";
                std::cout << "\n\nEnter you choice : ";
                std::cin >> choice;
M
Marian Gusatu 已提交
105
                switch (choice) {
M
Marian Gusatu 已提交
106
                case 1:
M
Marian Gusatu 已提交
107 108
                        std::cout << "\nEnter the element to be inserted : ";
                        std::cin >> x;
M
Marian Gusatu 已提交
109 110 111
                        insert(x);
                        break;
                case 2:
M
Marian Gusatu 已提交
112 113
                        std::cout << "\nEnter the element to be removed : ";
                        std::cin >> x;
M
Marian Gusatu 已提交
114 115 116
                        remove(x);
                        break;
                 case 3:
M
Marian Gusatu 已提交
117 118
                        std::cout << "\nEnter the element to be searched : ";
                        std::cin >> x;
M
Marian Gusatu 已提交
119 120 121 122
                        search(x);
                        break;
                 case 4:
                        show();
M
Marian Gusatu 已提交
123
                        std::cout << "\n";
M
Marian Gusatu 已提交
124 125
                        break;
                 case 5:
M
Marian Gusatu 已提交
126
                        std::cout << "The reversed list: \n";
M
Marian Gusatu 已提交
127 128
                        reverse();
                        show();
M
Marian Gusatu 已提交
129
                        std::cout << "\n";
M
Marian Gusatu 已提交
130 131 132
                        break;
               }
        } while (choice != 0);
C
Christian Bender 已提交
133

M
Marian Gusatu 已提交
134
        return 0;
C
Christian Bender 已提交
135
}