双链表改错!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 哥们我非要把指针弄明白!
#include <iostream>
using namespace std;
struct Node
{
int data;
Node *left,*right;
};
class Dlist
{
public:
Dlist()
{
head=NULL;
};
void Create();
void Find();
void Display();
void Insert();
void Delete();
private:
Node *head;
};
void Dlist::Create()
{
int x;
head = new Node;
Node *p = head;
bool cycle=true;
cout<< "please input num:"<<endl;
while(cycle)
{
cin>>x;
if(x!=0)
{
Node *s = new Node;
s->data = x;
p->right = s;
s->left = p;
p=s;
}
else
cycle = false;
}
p = head;
head = head->right;
head->left = NULL;
p->right = NULL;
delete p;
}
void Dlist::Display()
{
Node *p = head;
while ( p != NULL )
{
cout <<" "<<p->data;
p=p->right;
}
cout <<endl;
delete p;
}
void Dlist::Find()
{
Node *p = head;
int x;
cout << "please input you want find num: "<<endl;
cin >>x;
while ( p != NULL && p->data != x )
p=p->right;
if( p != NULL )
cout << "find !"<<endl;
else
cout <<" no find! "<<endl;
delete p;
}
void Dlist::Insert()
{
int position ,num ,i = 1;
Node *p = NULL;
Node *s = new Node;
cout <<"please input insert position: "<<endl;
cin >>position;
cout <<"please input insert num: "<<endl;
cin >>num;
s->data = num;
if ( position == 0 )
{
s->right=head;
head->left = s;
head = s;
head->left = NULL;
}
else
{
p = head;
while ( p != NULL && i < position-1 )
{
p = p->right;
i++;
}
if ( p != NULL )
{
if ( p->right == NULL )
{
p->right = s;
s->left = p;
s->right = NULL;
}
else
{
s->right = p->right;
p->right->left = s;
p->right = s;
s->left = p;
}
cout << "insert success!"<<endl;
}
else
cout << "insert failed!"<<endl;
}
}
void Dlist::Delete()
{
int num;
Node *p = NULL;
Node *q = NULL;
cout << "please input delete num:"<<endl;
cin >>num;
if ( head == NULL )
cout << "the list overflow!"<<endl;
if ( head->data == num )
{
p = head;
head = head->right;
head->left = NULL;
cout << "the num was delete at head!"<<endl;
}
else
{
q = head;
p = head->right;
while ( p != NULL && p->data != num )
{
q = p;
p = p->right ;
}
if ( p != NULL )
{
if ( p->right == NULL )
{
p->left->right = NULL;
delete p;
}
else
{
p->left->right = p->right;
p->right->left = p->left;
delete p;
}
cout << "the num was delete!"<<endl;
}
else
cout <<" not find num !"<<endl;
}
}
void main()
{
Dlist DL;
DL.Create();
DL.Display();
DL.Find();
DL.Display();
DL.Insert();
DL.Display();
DL.Delete();
DL.Display();
}
不知道是哪错了!请教!