Please use code tags. Highlight your code and click the <> button to the right of the edit window. This makes your code look like this: (more comments below)
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
|
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
using namespace std;
class List
{
protected:
struct node
{
int info;
struct node *next;
};
typedef struct node *NODEPTR;
NODEPTR listptr;
public:
List()
{
listptr = 0;
}
~List()
{
NODEPTR p, q;
if (emptyList());
for (p = listptr, q = p->next; p != 0; p = q, q = p->next)
delete p;
}
int emptyList()
{
return (listptr == 0);
}
void insertAfter(int oldvalue, int newvalue)
{
NODEPTR p, q;
for (p = listptr; p != 0 && p->info != oldvalue; p = p->next);
if (p == 0)
perror("ERROR: No value of this sought");
q = new node;
q->info = newvalue;
q->next = p->next;
q = listptr;
}
void push(int newvalue)
{
NODEPTR p;
p = new node;
p->info = newvalue;
p->next = listptr;
listptr = p;
}
void del(int oldvalue)
{
NODEPTR p;
NODEPTR q;
for (p = listptr, q = 0; p != 0 && p->info != oldvalue; q = p, p = p->next);
if (p == 0)
perror("ERROR:no element in the list with thie value");
if (q == 0)
listptr = p->next;
else
q->next = p->next;
delete p;
}
int pop()
{
NODEPTR p;
int x;
if (emptyList())
perror("ERROR:List is empty");
p = listptr;
listptr = p;
x = p->info;
delete p;
return x;
}
void display()
{
NODEPTR p;
for (p = listptr; p != 0; p->next) {
cout << p->info;
}
}
};
//runner program :
int
main()
{
List l1;
for (int i = 1; i <= 5; i++) {
l1.push(i * 5);
}
l1.pop();
l1.pop();
l1.insertAfter(20, 20);
l1.insertAfter(20, 25);
l1.del(5);
l1.del(15);
l1.display();
return 0;
}
|
Line 28: This code does nothing.
Line 29: When you get the end of the list,
p=q
will set p to NULL and
q = p->next
will dereference a null pointer. The destructor could be written much easier as:
1 2 3 4 5
|
while (listptr) {
NODEPTR tmp = listptr->next;
delete listptr;
listptr = tmp;
}
|
Line 43: If the value isn't found, the rest of the code still executes, and since p == NULL in that case, line 46 is accessing an NULL pointer.
Line 43: perror() prints the value of errno, which is inappropriate here. It would be better to print the message to the cerr stream:
cerr << "Error: No value of this sought\n";
LIne 47: You still need to link q into the list, so this should be
p->next = q;
LIne 65: Same as above. If p == NULL then you still execute the code below it.
Line 80: Sound be listp
tr = p->next;
Line 89: The last part of the for loop should be
p = p->next