数据结构之C++ 实现线性表_数组_链表_顺序存储结构_链式存储结构
数组
#include <iostream>
#include "stdlib.h"
using namespace std;
#define MAXSIZE 20
#define OK 1
#define ERROR 0
typedef int ElemType;
typedef int Status;
class list1
{
public:
list1();
Status GetElem(int i,ElemType &e);
Status ListInsert(int i,ElemType e);
Status show();
Status ListDelete(int i,ElemType &e);
private:
ElemType data[MAXSIZE];
int length;
};
list1::list1() {
length = 0;
}
Status list1::GetElem(int i,ElemType &e) {
if(length==0||i<1||i>length) return ERROR;
e=data[i-1];
return 0;
}
Status list1::ListInsert(int i,ElemType e) {
int k;
if(length==MAXSIZE||i<1||i>length+1)
return ERROR;
if(i<=length) {
for(k=length-1;k>=i-1;k--) {
data[k+1]=data[k];
}
}
data[i-1]=e;
length++;
return OK;
}
Status list1::ListDelete(int i,ElemType &e) {
int k;
if(length==0||i<1||i>length) {
cout<<"ERROR"<<endl;
return ERROR;
}
e = data[i-1];
if(i<length) for(k=i;k<length;k++) data[k-1]=data[k];
length--;
return OK;
}
Status list1::show() {
if(length == 0) {
cout<<"data is empty"<<endl;
return ERROR;
}
else {
cout<<"length is "<<length<<endl;
cout<<"data is ";
for(int i=0;i<length;i++) cout<<data[i]<<" ";
cout<<endl;
return OK;
}
}
int main()
{
list1 q;
q.show();
for(int i=0;i<=10;i++) q.ListInsert(i,i+1);
q.show();
int e;
q.GetElem(2,e);
cout<<"e is "<<e<<endl;
q.show();
q.ListDelete(3,e);
q.show();
return 0;
}
静态链表
#include <iostream>
using namespace std;
#define MAXSIZE 1000
struct Node
{
int data;
int cur;
};
class list3_oneself
{
public:
list3_oneself();
list3_oneself(int a[],int n);
void show();
void show_real();
int length();
void Insert(int n, int x);
void Delete(int n);
int Get(int n);
int Locate(int x);
private:
Node StaticLinkList[MAXSIZE];
};
list3_oneself::list3_oneself() {
for(int i=0;i<MAXSIZE-1;i++) StaticLinkList[i].cur = i+1;
StaticLinkList[MAXSIZE-1].cur = 0;
}
list3_oneself::list3_oneself(int a[],int n) {
for(int i=0;i<MAXSIZE-1;i++) StaticLinkList[i].cur = i+1;
for(int i=1;i<n+1;i++) StaticLinkList[i].data = a[i-1];
StaticLinkList[0].cur = n+1;
StaticLinkList[MAXSIZE-1].cur = 1;
StaticLinkList[n].cur=0;
}
void list3_oneself::show() {
int len = length();
cout<<"StaticLinkList's length is "<<len<<endl;
cout<<"StaticLinkList is ";
int k=StaticLinkList[MAXSIZE-1].cur;
for(int i=1;i<=len;i++) {
cout<<StaticLinkList[k].data<<" ";
k=StaticLinkList[k].cur;
}
cout<<endl;
}
void list3_oneself::show_real() {
int len = length();
cout<<"StaticLinkList's length is "<<len<<endl;
cout<<"real StaticLinkList is ";
for(int i=1;i<=len;i++) {
cout<<StaticLinkList[i].data<<" ";
}
cout<<endl;
}
int list3_oneself::length() {
int k=StaticLinkList[MAXSIZE-1].cur;
int num=0;
while(k) {
k=StaticLinkList[k].cur;
num++;
}
return num;
}
void list3_oneself::Insert(int m,int x) {
if(m>length()+1 || m<1 || m>MAXSIZE-2) {
cout<<"overstep the boundary!!!"<<endl;
exit(1);
}
int Cur=StaticLinkList[0].cur;
++StaticLinkList[0].cur;
StaticLinkList[Cur].data = x;
int k=MAXSIZE-1;
for(int i=1;i<m;i++) {
k=StaticLinkList[k].cur;
}
StaticLinkList[Cur].cur=StaticLinkList[k].cur;
StaticLinkList[k].cur=Cur;
}
void list3_oneself::Delete(int m) {
if(m>length() || m<1) {
cout<<"overstep the boundary!!!"<<endl;
exit(1);
}
int k=MAXSIZE-1;
for(int i=1;i<m;i++) {
k=StaticLinkList[k].cur;
}
int Cur=StaticLinkList[k].cur;
StaticLinkList[k].cur = StaticLinkList[Cur].cur;
StaticLinkList[Cur].cur = StaticLinkList[0].cur;
StaticLinkList[0].cur = Cur;
}
int main()
{
int arry[30];
for(int i=0;i<30;i++) arry[i]=i;
list3_oneself *StaticList = new list3_oneself(arry,30);
StaticList->show();
StaticList->Insert(1,100);
StaticList->show();
StaticList->Insert(32,101);
StaticList->show();
StaticList->show_real();
StaticList->Delete(1);
StaticList->show();
StaticList->show_real();
return 0;
}
链式存储结构
#include <iostream>
using namespace std;
template<class T>
struct Node
{
T data;
Node<T> *next;
};
template<class T>
class LinkList
{
public:
LinkList();
LinkList(T a[],int n);
~LinkList();
int Length();
T Get(int i);
int Locate(T x);
void Insert(int i, T x);
T Delete(int i);
void PrintList();
private:
Node<T>* first;
};
template<class T>
inline LinkList<T>::LinkList()
{
first = new Node<T>;
first->next = NULL;
}
template<class T>
LinkList<T>::LinkList(T a[], int n)
{
first = new Node<T>;
first->next = NULL;
for (int i = 0; i < n; i++)
{
Node<T>* S = new Node<T>;
S->data = a[i];
S->next = first->next;
first->next = S;
}
}
template<class T>
LinkList<T>::~LinkList()
{
while (first != NULL)
{
Node<T>* p = first;
first = first->next;
delete p;
}
}
template<class T>
int LinkList<T>::Length()
{
int count = 0;
Node<T>* p = first->next;
while (p != NULL)
{
count++;
p = p->next;
}
return count;
}
template<class T>
T LinkList<T>::Get(int i)
{
int count = 0;
Node<T>* p = first->next;
while (p != NULL)
{
count++;
if (count == i)
return p->data;
p = p->next;
}
return -1;
}
template<class T>
int LinkList<T>::Locate(T x)
{
int count = 0;
Node<T>* p = first->next;
while (p != NULL)
{
count++;
if (p->data == x)
return count;
p = p->next;
}
return 0;
}
template<class T>
void LinkList<T>::Insert(int i, T x)
{
int count = 0;
Node<T>* p = first;
while (p != NULL)
{
if (count == i - 1)
{
Node<T>* S = new Node<T>;
S->data = x;
S->next = p->next;
p->next = S;
}
p = p->next;
count++;
}
if (p == NULL)
throw "位置越界";
}
template<class T>
T LinkList<T>::Delete(int i)
{
int count = 0;
Node<T>* p = first;
while (p != NULL)
{
if (count == i - 1)
{
Node<T>* q = p->next;
T x = q->data;
p->next = q->next;
delete q;
return x;
}
p = p->next;
count++;
}
return -1;
}
template<class T>
void LinkList<T>::PrintList()
{
Node<T>* p = first->next;
while (p != NULL)
{
cout << p->data << " ";
p = p->next;
}
}
int main()
{
int arry[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
LinkList<int>* linklist = new LinkList<int>(arry, 10);
cout << linklist->Length() << endl;
cout << linklist->Get(5) << endl;
cout << linklist->Locate(6) << endl;
linklist->Insert(3, 11);
linklist->Delete(10);
linklist->PrintList();
return 0;
}
链式存储结构
#include <iostream>
using namespace std;
struct Node
{
int data;
Node *next;
};
class list2_oneself
{
public:
list2_oneself();
list2_oneself(int a[],int n);
void PrintList();
int length();
int Get(int n);
int Locate(int x);
void Insert(int n, int x);
void Delete(int n);
private:
Node *first;
};
list2_oneself::list2_oneself() {
first = new Node;
first->next = NULL;
}
list2_oneself::list2_oneself(int a[],int n) {
first = new Node;
first->next = NULL;
Node* r = first;
for(int i=0;i<n;i++)
{
Node* S = new Node;
S->data = a[i];
r->next = S;
r = S;
}
r->next = NULL;
}
void list2_oneself::PrintList() {
Node* p = first->next;
while(p != NULL)
{
cout << p->data << " ";
p = p->next;
}
cout<<endl;
}
int list2_oneself::length() {
int count =0;
Node* p=first->next;
while(p!=NULL) {
count++;
p=p->next;
}
return count;
}
int list2_oneself::Get(int n) {
int count = 0;
Node* p=first->next;
while(p!=NULL) {
count++;
if(count == n) return p->data;
p=p->next;
}
return -1;
}
int list2_oneself::Locate(int n) {
int count =0;
Node* p=first->next;
while(p!=NULL) {
count++;
if(p->data == n) return count;
p=p->next;
}
return 0;
}
void list2_oneself::Insert(int n,int x) {
int count=0;
Node* p=first->next;
while(p!=NULL) {
count++;
if(count == n) {
Node* S = new Node;
S->data = x;
S->next = p->next;
p->next = S;
}
p=p->next;
}
}
void list2_oneself::Delete(int n) {
int count=0;
Node* p=first->next;
while(p!=NULL) {
if(count == n-1) {
Node* q=p->next;
p->next=q->next;
delete(q);
}
count++;
p=p->next;
}
}
int main()
{
int arry[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
list2_oneself *linklist = new list2_oneself(arry, 10);
linklist->PrintList();
cout<<"length is "<<linklist->length()<<endl;
cout<<"data[5] is "<<linklist->Get(6)<<endl;
cout<<"5 is on "<<linklist->Locate(5)<<endl;
linklist->Insert(5,226);
linklist->PrintList();
cout<<"length is "<<linklist->length()<<endl;
cout<<"delete 4"<<endl;
linklist->Delete(4);
linklist->PrintList();
cout<<"length is "<<linklist->length()<<endl;
return 0;
}