Linked List: Insertion, Deletion..

Hi, what's wrong with my code? Why it isn't running?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166

#include "stdafx.h"
#include < iostream >
#include < conio.h >
#include < stdlib.h >
using namespace std;
struct node
                {
     int data;
     node *prev, *next;
                }*q, *temp, *start=NULL;
int c1, c2 ;
void create();
void display();
void insert();
void del();
void main()
{
    system("cls");
    while(1)
    {
                system("cls");
                cout << " \t\t ******* MAIN MENU ******\n" ;
                cout << " press 1 for adding data \n" ;
                cout << " press 2 for displaying data \n " ;
                cout << " press 3 for insertion \n " ;
                cout << " press 4 for deletion \n " ;
                cout << " press 0 for exit\n " ;
                char ch;
                ch=getch();
                switch(ch)
                {
                     case '1':
                                                system("cls");
                                                create();
                                                break;
                     case '2':
                                                system("cls");
                                                display();
                                                getch();
                                                break;
                     case '3':
                                                system("cls");
                                                insert();
                                                break;
                     case '4':
                                                system("cls");
                                                del();
                                                break;

                     case '0':
                                                exit(1);
                }
    }
}
 void create()
 {
    temp = new node;
    temp -> next = NULL;
    cout << "\nEnter data\n " ;
    cin >> temp -> data ;
    if(start == NULL)
    {
                start = temp;
                temp -> prev = NULL;
    }
    else
    {
                q= start;
                while(q->next != NULL)
                {
                    q = q->next;
                }
                q->next = temp;
                temp->prev = q;
    }
 }
 void display()
 {
       q=start;
       while(q!=NULL)
       {
                   cout<<q->data<<endl;
                   q = q->next;
       }
 }
 void insert()
 {
      cout << " Press 1 for insertion at start\n " ;
      cout << "Press 2 for insertion at middle\n " ;
      cout << "Press 3 for insertion at end\n " ;
      int choice;
      cin>>choice;
      switch(choice)
      {
                   case 1:
                                  system("cls");
                                  temp = new node;
                                  cout<<"Enter data \n";
                                  cin>>temp->data;
                                  start->prev =temp;
                                  temp->next = start;
                                  start =  temp;
                                  temp -> prev = NULL;
                                  break;
                   case 2:
                                  system("cls");
                                  cout<<"Enter the data aftre which u want to add this\n";
                                  int ch;
                                  cin>>ch;
                                  q= start;
                                  while(q->next!=NULL)
                                  {
                                       if(q->data == ch)
                                       {
                                                   temp = new node;
                                                   cout<<"Enter data \n";
                                                   cin>>temp->data;
                                                   q->next->prev = temp;
                                                   temp->next = q->next;
                                                   temp->prev = q;
                                                   q->next = temp;

                                       }
                                       q = q->next;
                                  }
                                  break;
                   case 3:
                                  system("cls");
                                  temp = new node;
                                  cout<<"Enter data\n";
                                  cin>> temp->data;
                                  temp->next = NULL;
                                  q =  start;
                                  while(q->next != NULL)
                                  {
                                      q= q->next;
                                  }
                                  q->next =  temp;
                                  temp->prev = NULL;
      }
 }
 void del()
 {
    system("cls");
    cout<<"Enter the data you want to delete \n";
    int num;
    cin>>num;
    q = start;
    if (start->data == num)
    start = start -> next;
    else
    {
    while(q != NULL)
    {
       if(q->next->data == num)
       {
                   temp = q->next;
                   q->next = temp->next;
                   temp->next->prev = q;
                   delete temp;
       }
       q = q->next;
    }
    }
 }
This is NOT HELPFUL! no one is helping me. this website is not so good. >:-/
First of all, it's only been an hour since you posted your code.

Second of all, it's 9am on a Tuesday morning. Most people are either at work or at school at this time of day (I am currently at work right now).

Third of all, delete the first line of code

#include "stdafx.h"

The code ran fine after this.
Yeah, i see that. Here isn't 9am on a tuesday morning but midnight wednesday. And after deleting that line, code didn't debug at all.

My problem is, the function for deleting isn't working at all. All it does is juts break and that's all. Why is that? Will you please help me and guide me? Thanks a lot.
This is probably because you are attempting to delete a value you that isn't in the list which will cause an access violation. You should try adding '5' to the list and then deleting '5' and it should work fine.

If you don't want the program to crash when a value is entered that is not in the list, I would highly suggest using a try catch block to catch an exception. A nice tutorial on how to add try catch blocks to your program can be found here:

http://www.cplusplus.com/doc/tutorial/exceptions/
What do you mean by adding 5 to the list and then deleting 5? I don't get it. Can you please further explain it? Like what would it look like? Thanks.
I mean when you run the program you should be shown 5 options:

1. Add
2. Display
3. Insert
4. Delete
0. Exit

You will need to either select the "Add" or "Insert" option before attempting to delete a value from the list, as nothing exists in the list when the program initially starts.
Last edited on
Ah, so you mean when one inputs data, the inputted isn't accepted or isn't saved? Something like that, you mean? How do i change that? What should i change, arrange, delete or add to my code? Please suggest what you think should be. Guide me please thanks.
I am getting the feeling that you have not programming a single line of the code above. I would suggest looking at some C++ tutorials to further your knowledge in linked lists and C++ in general.
So now, you get the feeling that I didn't code any of the codes' lines? If that is your choice, well then do so, I won't stop you really. I need not to look at some C++ tuts because I have done them so, before asking here in this forum. It seems like this isn't of any help, might as want to look others now. Thanks for your time dude. I was asking for your help to guide me to what you were saying but then this? Thanks alot.
Just wrote a big write up here but then realized something that is probably causing most of your frustration. You never check to make sure the current pointer isn't null before you access a pointer in the struct. Almost guaranteed to get a seg fault or access violation at some point if you don't error check for a null pointer. It's probably the most frustrating thing to learn when dealing with pointers in C/C++ and something that pretty much all C/C++ programmers struggle with initially.

Example, you do check for the current pointer to be null in your loop, but then you proceed to do q->next->data. If q is not null the loop will engage, but if the child is null, this will result in a seg fault/access violation (depending on compiler). Always check for null before accessing a member within a struct, and you have to treat internal pointers to the same type of struct as it's own struct.

I'm assuming this project forbids using an OOP approach or the class is pre-OOP, otherwise this whole design should be scrapped for an object based solution.
Last edited on
Topic archived. No new replies allowed.