#include<malloc.h>
#include<iostream>
using namespace std;
/* typedef struct SLink
{
ElemType data;
struct SLink *next;
} SLink;*/
class Node
{
public:
int data;
Node *next;
int printNode()
{
return data;
}
};
class List
{
private:
Node *m_pList;
int m_iLength;
public:
List()
{
m_pList=new Node;
m_pList->data=0;
m_pList->next=NULL;
m_iLength=0;
}
~List()
{
ClearList();
delete m_pList;
}
void ClearList()
{
Node *node=m_pList->next; //赋值头节点指针域
while(node!=NULL)
{
Node *temp=node->next; //第一个节点
delete node;
node=temp;
}
m_pList->next=NULL;//m_pList->next=*node;
}
bool EmptyList()
{
if(m_iLength==0) return true;
else return false;
// return m_iLength==0?true:false;
}
int GetLength()
{
return m_iLength;
}
bool GetElem(int i,Node *node)
{
if(i<0||i>m_iLength)
{
return false;
}
Node *temp=m_pList;
for(int j=0;j<i;j++)
{
temp=temp->next;
}
node->data=temp->data;
return true;
}
int Locate(Node *node)
{
Node *temp=m_pList;
for(int i=0;i<m_iLength;i++)
{
temp=temp->next;
if(temp->data==node->data)
{
return (i+1);
break;
}
}
return -1;
}
bool Prior(Node *node,Node *pr)
{
int i=Locate(node);
if(i<0||i>m_iLength)
{
return false;
}
Node *temp=m_pList;
Node *tempbefore=NULL;
for(int j=0;j<i;j++)
{
tempbefore=temp;
temp=temp->next;
}
pr->data=tempbefore->data;
return true;
}
bool Next(Node *node,Node *ne)
{
int i=Locate(node);
if(i<0||i>=m_iLength)
{
return false;
}
Node *tempafter=NULL;
Node *temp=m_pList;
for(int j=0;j<i;j++)
{
temp=temp->next;
tempafter=temp->next;
}
ne->data=tempafter->data; //ne->data=temp->next->data;
return true;
}
bool InsertListHead(Node *node)
{
Node *temp=m_pList->next;
Node *newnode=new Node;
if(newnode==NULL) return false;
newnode->next=temp;
m_pList->next=newnode;
newnode->data=node->data;
m_iLength++;
return true;
}
bool InsertListTail(Node *node)
{
Node *newnode=new Node;
if(newnode==NULL) return false;
Node *temp=m_pList;
while(temp->next!=NULL)
{
temp=temp->next;
}
newnode->data=node->data;
newnode->next=NULL;
temp->next=newnode;
m_iLength++;
return true;
}
bool InsertList(int i,Node *node)//插入第i个节点后
{
if(i<0||i>m_iLength)
{
return false;
}
Node *newnode=new Node;
if(newnode==NULL) return false;
Node *temp=m_pList;
for(int j=0;j<i;j++)
{
temp=temp->next;
}
newnode->data=node->data;
newnode->next=temp->next;
temp->next=newnode;
m_iLength++;
return true;
}
bool DeleteList(int i,Node *node)//删除第i个节点
{
if(i<0||i>m_iLength)
{
return false;
}
Node *temp=m_pList;
Node *tempbefore=NULL;
for(int j=0;j<i;j++)
{
tempbefore=temp;
temp=temp->next;
}
tempbefore->next=temp->next;
node->data=temp->data;
delete temp;
m_iLength--;
return true;
}
void ListTraverse()
{
Node *newnode=new Node;
Node *node=m_pList->next;
while(node!=NULL)
{
cout<<node->printNode()<<" ";
node=node->next;
}
cout<<endl;
// for(int i=0;i<m_iLength;i++)
// {
// temp=temp->next;
// cout<<temp->printNode();
// }
}
};
int main()
{
Node e1,e2,e3,e4,e5,e6,e7,temp;
e1.data=3;e2.data=6;e5.data=2;
e3.data=2;e6.data=1;
e4.data=3;e7.data=0;
List *list1=new List();
list1->InsertListHead(&e1);
list1->InsertListTail(&e2);
list1->InsertList(2,&e3);
list1->InsertList(3,&e4);
list1->InsertList(4,&e5);
list1->InsertList(5,&e6);
list1->InsertList(6,&e7);
cout<<"表长:"<<list1->GetLength()<<endl;
cout<<"表的顺序:";
list1->ListTraverse();
list1->DeleteList(1,&temp);
cout<<"删除表中的数:"<<temp.data<<endl;
cout<<"表长:"<<list1->GetLength()<<endl;
cout<<"删除数据后表的顺序:";
list1->ListTraverse();
list1->GetElem(3,&temp);
cout<<"得到的值:"<<list1->Locate(&temp)<<endl;
list1->Prior(&e3,&temp);
cout<<"前驱:"<<temp.data<<endl;
list1->Next(&e3,&temp);
cout<<"后继:"<<temp.data<<endl;
delete list1;
return 0;
}
#include<iostream>
using namespace std;
/* typedef struct SLink
{
ElemType data;
struct SLink *next;
} SLink;*/
class Node
{
public:
int data;
Node *next;
int printNode()
{
return data;
}
};
class List
{
private:
Node *m_pList;
int m_iLength;
public:
List()
{
m_pList=new Node;
m_pList->data=0;
m_pList->next=NULL;
m_iLength=0;
}
~List()
{
ClearList();
delete m_pList;
}
void ClearList()
{
Node *node=m_pList->next; //赋值头节点指针域
while(node!=NULL)
{
Node *temp=node->next; //第一个节点
delete node;
node=temp;
}
m_pList->next=NULL;//m_pList->next=*node;
}
bool EmptyList()
{
if(m_iLength==0) return true;
else return false;
// return m_iLength==0?true:false;
}
int GetLength()
{
return m_iLength;
}
bool GetElem(int i,Node *node)
{
if(i<0||i>m_iLength)
{
return false;
}
Node *temp=m_pList;
for(int j=0;j<i;j++)
{
temp=temp->next;
}
node->data=temp->data;
return true;
}
int Locate(Node *node)
{
Node *temp=m_pList;
for(int i=0;i<m_iLength;i++)
{
temp=temp->next;
if(temp->data==node->data)
{
return (i+1);
break;
}
}
return -1;
}
bool Prior(Node *node,Node *pr)
{
int i=Locate(node);
if(i<0||i>m_iLength)
{
return false;
}
Node *temp=m_pList;
Node *tempbefore=NULL;
for(int j=0;j<i;j++)
{
tempbefore=temp;
temp=temp->next;
}
pr->data=tempbefore->data;
return true;
}
bool Next(Node *node,Node *ne)
{
int i=Locate(node);
if(i<0||i>=m_iLength)
{
return false;
}
Node *tempafter=NULL;
Node *temp=m_pList;
for(int j=0;j<i;j++)
{
temp=temp->next;
tempafter=temp->next;
}
ne->data=tempafter->data; //ne->data=temp->next->data;
return true;
}
bool InsertListHead(Node *node)
{
Node *temp=m_pList->next;
Node *newnode=new Node;
if(newnode==NULL) return false;
newnode->next=temp;
m_pList->next=newnode;
newnode->data=node->data;
m_iLength++;
return true;
}
bool InsertListTail(Node *node)
{
Node *newnode=new Node;
if(newnode==NULL) return false;
Node *temp=m_pList;
while(temp->next!=NULL)
{
temp=temp->next;
}
newnode->data=node->data;
newnode->next=NULL;
temp->next=newnode;
m_iLength++;
return true;
}
bool InsertList(int i,Node *node)//插入第i个节点后
{
if(i<0||i>m_iLength)
{
return false;
}
Node *newnode=new Node;
if(newnode==NULL) return false;
Node *temp=m_pList;
for(int j=0;j<i;j++)
{
temp=temp->next;
}
newnode->data=node->data;
newnode->next=temp->next;
temp->next=newnode;
m_iLength++;
return true;
}
bool DeleteList(int i,Node *node)//删除第i个节点
{
if(i<0||i>m_iLength)
{
return false;
}
Node *temp=m_pList;
Node *tempbefore=NULL;
for(int j=0;j<i;j++)
{
tempbefore=temp;
temp=temp->next;
}
tempbefore->next=temp->next;
node->data=temp->data;
delete temp;
m_iLength--;
return true;
}
void ListTraverse()
{
Node *newnode=new Node;
Node *node=m_pList->next;
while(node!=NULL)
{
cout<<node->printNode()<<" ";
node=node->next;
}
cout<<endl;
// for(int i=0;i<m_iLength;i++)
// {
// temp=temp->next;
// cout<<temp->printNode();
// }
}
};
int main()
{
Node e1,e2,e3,e4,e5,e6,e7,temp;
e1.data=3;e2.data=6;e5.data=2;
e3.data=2;e6.data=1;
e4.data=3;e7.data=0;
List *list1=new List();
list1->InsertListHead(&e1);
list1->InsertListTail(&e2);
list1->InsertList(2,&e3);
list1->InsertList(3,&e4);
list1->InsertList(4,&e5);
list1->InsertList(5,&e6);
list1->InsertList(6,&e7);
cout<<"表长:"<<list1->GetLength()<<endl;
cout<<"表的顺序:";
list1->ListTraverse();
list1->DeleteList(1,&temp);
cout<<"删除表中的数:"<<temp.data<<endl;
cout<<"表长:"<<list1->GetLength()<<endl;
cout<<"删除数据后表的顺序:";
list1->ListTraverse();
list1->GetElem(3,&temp);
cout<<"得到的值:"<<list1->Locate(&temp)<<endl;
list1->Prior(&e3,&temp);
cout<<"前驱:"<<temp.data<<endl;
list1->Next(&e3,&temp);
cout<<"后继:"<<temp.data<<endl;
delete list1;
return 0;
}