主题:我再来一贴,链表的操作--C++类封装.封装得不完善,也不好哈..
//List.h
#ifndef _LIST_H
#define _LIST_H
typedef int ElementType;
typedef struct tagList{
struct tagList* next;
ElementType Element;
}List,*pList,Node,*pNode;
enum Status{
OK=0,ERROR,FAILED};
class CList{
public:
pList pHead;
Status Create();
Status Add(ElementType Element);
Status Delete(int index,ElementType* Element);
ElementType GetAt(int index);
bool IsListEmpty();
int GetLength();
};
#endif
//List.cpp
#include "List.h"
Status CList::Create()
{
pHead=new List;
if(pHead!=0)
{
pHead->next=0;
return OK;
}
else
return FAILED;
}
Status CList::Add(ElementType Element)
{
pNode node;
pList p=pHead;;
if(pHead==0)
return ERROR;
node=new Node;
if(node==0)
return FAILED;
node->Element=Element;
node->next=0;
while(p->next!=0)
p=p->next;
p->next=node;
return OK;
}
bool CList::IsListEmpty()
{
if(pHead==0)
return true;
if(pHead->next!=0)
return false;
else
return true;
}
Status CList::Delete(int index,ElementType* Element)
{
pList p;
int i=0;
pNode temp;
if(pHead==0)
return ERROR;
if(i<0||i>GetLength()-1)
return ERROR;
p=pHead;
while(p->next!=0&&i<index)
{
p=p->next;
i++;
}
if(p!=0)
*Element=p->next->Element;
temp=p->next;
p->next=temp->next;
delete temp;
return OK;
}
int CList::GetLength()
{
int i=0;
pList p;
if(pHead == 0)
return 0;
p=pHead;
while(p->next!=0)
{
p=p->next;
i++;
}
return i;
}
ElementType CList::GetAt(int index)
{
pList p;
int i;
if(pHead==0)
return -10000;
if(index>GetLength())
return -10000;
p=pHead;
for(i=0;i<=index;i++)
p=p->next;
return p->Element;
}
//main.cpp
#include <iostream.h>
#include "List.h"
#include <stdlib.h>
#include <iomanip.h>
void main()
{
CList list;
int i=0;
ElementType Element;
list.Create();
///////////create
srand(100);
for(i=0;i<5;i++)
list.Add(rand()%100);
/////////////////////initlize
cout<<"Length:"<<endl;
cout<<list.GetLength()<<endl;
cout<<"Elements:"<<endl;
for(i=0;i<list.GetLength();i++)
cout<<setw(5)<<list.GetAt(i);
cout<<endl;
list.Delete(2,&Element);
for(i=0;i<list.GetLength();i++)
cout<<setw(5)<<list.GetAt(i);
return;
}
这也是我的作业..
我看到一天到黑有人问关于数据结构得一些基本问题..
#ifndef _LIST_H
#define _LIST_H
typedef int ElementType;
typedef struct tagList{
struct tagList* next;
ElementType Element;
}List,*pList,Node,*pNode;
enum Status{
OK=0,ERROR,FAILED};
class CList{
public:
pList pHead;
Status Create();
Status Add(ElementType Element);
Status Delete(int index,ElementType* Element);
ElementType GetAt(int index);
bool IsListEmpty();
int GetLength();
};
#endif
//List.cpp
#include "List.h"
Status CList::Create()
{
pHead=new List;
if(pHead!=0)
{
pHead->next=0;
return OK;
}
else
return FAILED;
}
Status CList::Add(ElementType Element)
{
pNode node;
pList p=pHead;;
if(pHead==0)
return ERROR;
node=new Node;
if(node==0)
return FAILED;
node->Element=Element;
node->next=0;
while(p->next!=0)
p=p->next;
p->next=node;
return OK;
}
bool CList::IsListEmpty()
{
if(pHead==0)
return true;
if(pHead->next!=0)
return false;
else
return true;
}
Status CList::Delete(int index,ElementType* Element)
{
pList p;
int i=0;
pNode temp;
if(pHead==0)
return ERROR;
if(i<0||i>GetLength()-1)
return ERROR;
p=pHead;
while(p->next!=0&&i<index)
{
p=p->next;
i++;
}
if(p!=0)
*Element=p->next->Element;
temp=p->next;
p->next=temp->next;
delete temp;
return OK;
}
int CList::GetLength()
{
int i=0;
pList p;
if(pHead == 0)
return 0;
p=pHead;
while(p->next!=0)
{
p=p->next;
i++;
}
return i;
}
ElementType CList::GetAt(int index)
{
pList p;
int i;
if(pHead==0)
return -10000;
if(index>GetLength())
return -10000;
p=pHead;
for(i=0;i<=index;i++)
p=p->next;
return p->Element;
}
//main.cpp
#include <iostream.h>
#include "List.h"
#include <stdlib.h>
#include <iomanip.h>
void main()
{
CList list;
int i=0;
ElementType Element;
list.Create();
///////////create
srand(100);
for(i=0;i<5;i++)
list.Add(rand()%100);
/////////////////////initlize
cout<<"Length:"<<endl;
cout<<list.GetLength()<<endl;
cout<<"Elements:"<<endl;
for(i=0;i<list.GetLength();i++)
cout<<setw(5)<<list.GetAt(i);
cout<<endl;
list.Delete(2,&Element);
for(i=0;i<list.GetLength();i++)
cout<<setw(5)<<list.GetAt(i);
return;
}
这也是我的作业..
我看到一天到黑有人问关于数据结构得一些基本问题..