一、線性表的順序實現
#include <iostream>
#include <cstdlib>
#define OK 1
#define ERROR 0
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
typedef int Elemtype;
typedef int Status;
using namespace std;
struct SqList{
Elemtype *elem;
int length;
int LIST_SIZE;
};
Status InitList(SqList &L); //初始化線性表
Status InsertList(SqList &L, Elemtype e); //線性表的插入操作
Status ChectList(SqList &L, Elemtype e); //線性表的查找操作
Status DeleteList(SqList &L, Elemtype e); //線性表的刪除操作
int Getlength(const SqList &L); //獲取線性表的內容長度
void TraverseList(const SqList &L); //便利線性表
int main()
{
SqList L;
InitList(L);
for(int i = 0; i < 10; ++i)
InsertList(L,i+1);
cout << "當前表長"<< Getlength(L)<< endl;
TraverseList(L);
cout << "請輸入要刪除的數據:";
Elemtype e;
cin >> e;
DeleteList(L,e);
cout << "當前表長"<< Getlength(L)<< endl;
TraverseList(L);
return 0;
}
Status InitList(SqList &L){
L.elem = (Elemtype *)malloc(LIST_INIT_SIZE * sizeof(Elemtype));
if(!L.elem)
return ERROR;
for(int i = 0; i < LIST_INIT_SIZE; ++i)
L.elem[i] = 0;
L.length = 0;
L.LIST_SIZE = LIST_INIT_SIZE;
return OK;
}
Status InsertList(SqList &L, Elemtype e){
if(L.length >= LIST_INIT_SIZE){
L.elem = (Elemtype *)realloc(L.elem,(LISTINCREMENT+L.LIST_SIZE)*sizeof(Elemtype));
L.elem = L.elem + LISTINCREMENT;
L.LIST_SIZE = LIST_INIT_SIZE + LISTINCREMENT;
}
L.elem[L.length] = e;
L.length++;
return OK;
}
Status ChectList(SqList &L, Elemtype e){
for(int i = 0; i < L.length; ++i){
if(L.elem[i] == e)
return OK;
}
return ERROR;
}
Status DeleteList(SqList &L, Elemtype e){
int i;
for(i = 0; i < L.length; ++i){
if(L.elem[i] == e)
break;
}
if(i == L.length)
return ERROR;
for(i; i < L.length; ++i){
L.elem[i] = L.elem[i+1];
}
L.length--;
return OK;
}
int Getlength(const SqList &L){
return L.length;
}
void TraverseList(const SqList &L){
for(int i = 0; i < L.length; ++i){
cout << L.elem[i]<< " ";
}
cout << endl;
}
二、線性表的鏈式實現
指針動態申請空間時類型必須相互匹配
#include <iostream>
#include <cstdlib>
#define OK 1
#define ERROR 0
typedef int Elemtype;
typedef int Status;
using namespace std;
typedef struct LinkNode{
Elemtype data;
struct LinkNode *next;
}*LNode,LinkNode;
typedef struct {
LinkNode *head, *tail;
int length;
}LinkList;
Status InitList(LinkList &L); //初始化線性表
Status InsertList(LinkList &L, Elemtype e); //線性表的插入操作
Status ChectList(LinkList &L, Elemtype e); //線性表的查找操作
Status DeleteList(LinkList &L, Elemtype e); //線性表的刪除操作
int Getlength(const LinkList &L); //獲取線性表的內容長度
void TraverseList(const LinkList &L); //遍歷線性表
Status DestroyList(LinkList &L); //銷燬線性表
int main()
{
LinkList L;
InitList(L);
for(int i = 0; i < 10; ++i){
InsertList(L,i+1);
}
cout << "當前表長:"<< Getlength(L)<< endl;
TraverseList(L);
cout << "請輸入要刪除的數據:";
Elemtype e;
cin >> e;
DeleteList(L,e);
cout << "當前表長"<< Getlength(L)<< endl;
TraverseList(L);
DestroyList(L);
return 0;
}
Status InitList(LinkList &L){
L.head = new LinkNode;
L.tail = L.head;
L.head->next = NULL;
L.length = 0;
return OK;
}
Status InsertList(LinkList &L, Elemtype e){
LNode s = new LinkNode;
s->data = e;
s->next = L.tail->next;
L.tail->next = s;
L.tail = s;
L.length++;
return OK;
}
Status ChectList(LinkList &L, Elemtype e){
LNode p;
p = L.head->next;
while(p){
if(p->data == e)
return OK;
p = p->next;
}
return ERROR;
}
Status DeleteList(LinkList &L, Elemtype e){
LNode p;
p = L.head;
while(p){
if(p->next->data == e){
p->next = p->next->next;
free(p->next);
L.length--;
return OK;
}
p = p->next;
}
return ERROR;
}
int Getlength(const LinkList &L){
return L.length;
}
void TraverseList(const LinkList &L){
LNode p;
p = L.head->next;
while(p){
cout << p->data<< " ";
p = p->next;
}
cout << endl;
}
Status DestroyList(LinkList &L){
LNode p;
p = L.head->next;
while(p){
L.head->next = p->next;
free(p);
p = L.head->next;
}
L.head->next = NULL;
L.length = 0;
return OK;
}