Errors while removing node from list

I am writing a program that appends values to a linked list
and removes a particular value from the linked list.
The append function works.
But the remove function is failing at execution time.
Can you please help fix this bug?
Thanks a lot!

[code]

#include <iostream>
using namespace std;


class Node
{
private:
Node* _pNext;
public:
float _value;
float data;
Node *next;
};

class List
{
private:
Node* head;
Node* _pTail;
public:
List() {head=NULL;}
void remove(float);
void appendNode (float);
/*void sort();*/
void display(void);
/*void merge(List &, List&);*/
};

void List::appendNode(float num)
{
Node* newnode = new Node();
Node* nodeptr;
newnode->data = num;
newnode->next = NULL;

if(head==NULL)
{
head = newnode;
}
else
{
nodeptr = head;
while(nodeptr->next!=NULL)
{
nodeptr = nodeptr->next;
}
nodeptr->next = newnode;
nodeptr->next->data = num;
}
}

void List::remove(float val)
{
Node *pPre = NULL, *pDel = NULL;
Node* _pHead;
// Node* _pTail;
//Node* _pNext;

/* Check whether it is the head node?
if it is, delete and update the head node */
cout << _pHead->data;
if (_pHead->data == val) {
/* point to the node to be deleted */
pDel = _pHead;
/* update */
_pHead = pDel->next;
delete pDel;
return;
}

pPre = _pHead;
pDel = _pHead->next;

/* traverse the list and check the value of each node */
while (pDel != NULL) {
if (pDel->data == val) {
/* Update the list */
pPre->next = pDel->next;
/* If it is the last node, update the tail */
if (pDel == _pTail) {
_pTail = pPre;
}
delete pDel; /* Here only remove the first node with the given value */
break; /* break and return */
}
pPre = pDel;
pDel = pDel->next;
}
}
void List::display()
{
int count = 0;
Node* currNode = head;
while (currNode!=NULL)
{
cout << currNode->data << " ";
currNode = currNode->next;
count++;
}
cout << "\nNumber of nodes in the list: " << count << endl;
}

int main()
{
List list1;
list1.appendNode(9.5);
list1.appendNode(15.5);
list1.remove(15.5);
list1.display();
return 0;
}
[/code
You were close on the code tags, the closing tag needs a final bracket.

Edit: I'm sorry, I'm out.
Please use code tags.
There are dozens of great tutorials online with a simple google search, and I'm too worried about giving bad advice while I'm this tired.
Last edited on
I have rewritten the deleteNode function
But this doesn't work.
Here's my complete program - all other modules - add, find, display, count work fine


#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
/*
* Node Declaration
*/
struct node
{
double info;
struct node *next;
}*start;

/*
* Class Declaration
*/
class single_llist
{
public:
node* create_node(double);
void appendNode(double);
void deleteNode(double);
void findNode(double);
int countList();
void displayList();
single_llist()
{
start = NULL;
}
};

/*
* Main :contains menu
*/
int main()
{
int choice, nodes, element, position, i, size;
double value;
single_llist sl;
start = NULL;
while (1)
{
cout<<endl<<"---------------------------------"<<endl;
cout<<endl<<"Operations on singly linked list"<<endl;
cout<<endl<<"---------------------------------"<<endl;
cout<<"1.Insert Node at beginning"<<endl;
cout<<"2.Insert node at last"<<endl;
cout<<"3.Insert node at position"<<endl;
cout<<"4.Sort Link List"<<endl;
cout<<"5.Delete a Particular Node"<<endl;
cout<<"6.Update Node Value"<<endl;
cout<<"7.Search Element"<<endl;
cout<<"8.Display Linked List"<<endl;
cout<<"9.Size of list "<<endl;
cout<<"10.Exit "<<endl;
cout<<"Enter your choice : ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Inserting Node at Beginning: "<<endl;
//sl.insert_begin();
cout<<endl;
break;
case 2:
cout<<"Inserting Node at Last: "<<endl;
cin >> value;
sl.appendNode(value);
cout<<endl;
break;
case 3:
cout<<"Inserting Node at a given position:"<<endl;
//sl.insert_pos();
cout<<endl;
break;
case 4:
cout<<"Sort Link List: "<<endl;
//sl.sort();
cout<<endl;
break;
case 5:
cout<<"Delete a particular node: "<<endl;
cin >> value;
sl.deleteNode(value);
break;
case 6:
cout<<"Update Node Value:"<<endl;
//sl.update();
cout<<endl;
break;
case 7:
cout<<"Enter value to be searched "<<endl;
cin >> value;
sl.findNode(value);
cout<<endl;
break;
case 8:
cout<<"Display elements of link list"<<endl;
sl.displayList();
cout<<endl;
break;
case 9:
cout<<"Show the size of the list"<<endl;
size=sl.countList();
cout << size;
// cout << pos;
cout<<endl;
break;
case 10:
cout<<"Exiting..."<<endl;
exit(1);
break;
default:
cout<<"Wrong choice"<<endl;
}
}
}

/*
* Creating Node
*/
node *single_llist::create_node(double value)
{
struct node *temp, *s;
temp = new(struct node);
if (temp == NULL)
{
cout<<"Memory not allocated "<<endl;
return 0;
}
else
{
temp->info = value;
temp->next = NULL;
return temp;
}
}

/*
* Inserting Node at last
*/
void single_llist::appendNode(double value)
{

struct node *temp, *s,*p;
temp = create_node(value);
s = start;
if (start == NULL)
{
p = start;
start = temp;
start->next = p;
}
else {
while (s->next != NULL) {
s = s->next;
}
//s->next = temp;
temp->next = NULL;
s->next = temp;
}
cout<<"Element Inserted at last"<<endl;
}

/*
* Delete element at a given position
*/
void single_llist::deleteNode(double value)
{
new (struct node);
if (start == NULL)
{
cout<<"List is empty"<<endl;
return;
}
struct node *s, *ptr;
s = start;
s->info=s->next->info;
// free(s);
cout<<"Element Deleted"<<endl;
}


/*
* Searching an element
*/
void single_llist::findNode(double value)
{
int pos = 0;
bool flag = false;
if (start == NULL)
{
cout<<"List is empty"<<endl;
return;
}

struct node *s;
s = start;
while (s != NULL)
{
pos++;
if (s->info == value)
{
flag = true;
cout<<"Element "<<value<<" is found at position "<<pos<<endl;
}
s = s->next;
}
if (!flag)
cout<<"Element "<<value<<" not found in the list"<<endl;
}
/*
* Searching an element
*/
int single_llist::countList()
{
int pos = 0;
// bool flag = false;

struct node *s;
s = start;
while (s != NULL)
{
pos++;
s = s->next;
}
//cout << pos;
//cout << "\n";
return pos;
}

/*
* Display Elements of a link list
*/
void single_llist::displayList()
{
struct node *temp;
if (start == NULL)
{
cout<<"The List is Empty"<<endl;
return;
}
temp = start;
cout<<"Elements of list are: "<<endl;
while (temp != NULL)
{
cout<<temp->info<<"->";
temp = temp->next;
}
cout<<"NULL"<<endl;
}

Topic archived. No new replies allowed.