hello guys
I want to make a c++ program which is designed to receive some elements of a linked list by the user in a (while switch formula) and then the user can delete a specific value that he can designate .

my problem is in delval function which delete the integer.

it cant be done in this shape.

/****************************************
*Doubly Linked List                    
*24-12-2009                             
*By ME                         
****************************************/

//-----------------------------------------------\\

#include <iostream.h>
#include<conio.h>
#include<stdlib.h>

//-----------------------------------------------\\

struct node{
node *next;
node *prev;
int data;
};
//-----------------------------------------------\\
struct dblist{
node *p;
void insert();
void print();
void delval(int);
};
//-----------------------------------------------\\
void main()
{
dblist s;
s.p=NULL;
s.p->prev=NULL;
int k,f;
while (1)
{
cout<<"enter \n1 for insert \n2 for remove spesific value \n3 to print te list \n4 to exit \n";
cin>>k;

switch (k)
{
case 1:
s.insert();
break;

case 2:
cout<<"The value you want to Delete IZ";
cin>>f;
s.delval(f);
break;

case 3:
s.print();
break;


default:
exit(0);
}
}


getch();
}

//-----------------------------------------------\\
void dblist::insert()
{int i;
node *temp=new node;
temp->next=NULL;
temp->prev=NULL;
cout<<"INSERT\n";
cin>>i;
temp->data=i;

if (p==NULL)
p=temp;


else
{
temp->prev=p;
p->next=temp;
p=temp;
}

}
//-----------------------------------------------\\
void dblist::print()
{node *tmp=p;
cout<<"The element are\n";
while(p!=NULL)
{cout<<p->data<<endl;
p=p->prev;
}
p=tmp;
}
//-----------------------------------------------\\
void dblist::delval(int x)
{node *tmp=p;

while(p!=NULL)
{
if (p->data==x)
{p->prev.next=p->next; // the program give an error flag on this line
p=p->prev;
}
else
p=p->prev;
}
p=tmp;
}

p->prev looks to be a pointer: p->prev.next did you intend to do this instead? p->prev->next

Edited 6 Years Ago by Bench: n/a

I did and it works
but it do nothing on the list

Edited 6 Years Ago by lucky_43: n/a

This article has been dead for over six months. Start a new discussion instead.