menu driven program to delete in linked list

Solutions on MaxInterview for menu driven program to delete in linked list by the best coders in the world

showing results for - "menu driven program to delete in linked list"
Eduardo
23 Nov 2019
1#include <iostream>
2
3using namespace std;
4class node
5{
6public:
7    int data;
8    node*next;
9};
10node*head=NULL;
11void insertlist(int val)
12{
13    node*temp=new node;
14    temp->data=val;
15    if(head==NULL)
16    {
17        temp->next=head;
18        head=temp;
19    }
20    else
21    {
22        temp->next=head;
23        head=temp;
24    }
25}
26void delete_start()
27{
28    if(head==NULL)
29    {
30        cout<<"no element in the list :: nothing to be deleted:"<<endl;
31    }
32    else
33    {
34        node*temp=new node;
35        temp=head;
36        head=head->next;
37        delete(temp);
38    }
39}
40void delete_end()
41{
42    if(head==NULL)
43    {
44         cout<<"no element in the list :: nothing to be deleted:"<<endl;
45    }
46    else
47    {
48        node *temp=new node;
49        temp=head;
50        node*ptr=new node;
51        while(temp->next->next!=NULL)
52        {
53            temp=temp->next;
54        }
55        ptr=temp->next;
56        temp->next=NULL;
57        delete ptr;
58
59
60    }
61}
62void delete_any(int val)
63{
64    node*temp=new node;
65    if(head==NULL)
66    {
67         cout<<"no element in the list :: nothing to be deleted:"<<endl;
68    }
69    else
70    {
71        if(head->data==val)
72        {
73            temp=head;
74            head=head->next;
75            delete temp;
76        }
77        else
78        {
79            node*ptr=new node;
80            ptr=head;
81            while(ptr->next!=NULL)
82            {
83                if(ptr->next->data==val)
84                {
85                    temp=ptr->next;
86                    ptr->next=ptr->next->next;
87                    delete temp;
88                }
89                ptr=ptr->next;
90
91            }
92        }
93    }
94
95}
96void printf()
97{
98    node*temp=new node;
99    temp=head;
100    while(temp!=NULL)
101    {
102        cout<<temp->data<<" ";
103        temp=temp->next;
104    }
105    cout<<endl;
106}
107int main()
108{
109    int n;
110    int t;
111    cin>>t;
112    while(t--)
113    {
114    cout<<"enter the value to be inserted in the list:"<<endl;
115    cin>>n;
116    insertlist(n);
117    }
118    printf();
119    int s;
120    while(1)
121    {
122        cout<<"1-delete from the start"<<endl<<"2-delete from the end"<<endl<<"3-delete any element"<<endl<<"4-exit"<<endl;
123        cout<<"enter your choice:"<<endl;
124        cin>>s;
125        switch(s)
126            {
127            case 1:
128                {
129                   delete_start();
130                   printf();
131                   break;
132                }
133            case 2:
134                {
135                    delete_end();
136                    printf();
137                    break;
138                }
139            case 3:
140                {
141                    int a;
142                    printf();
143                    cout<<"enter the value to be deleted:"<<endl;
144                    cin>>a;
145                    delete_any(a);
146                    printf();
147                    break;
148                }
149            case 4:
150                {
151                    exit(0);
152                }
153            default:
154                {
155                    cout<<"invalid opertaion:"<<endl;
156                }
157            }
158
159    }
160    return 0;
161}
162