线性表在我看来就是用一根线把所有数据穿起来,形象吧,他具体的定义自己上网查吧,我是不喜欢背定义的,很难记住。
线性表在内存中有两种存储方式,一种叫顺序存储,另一种叫链式存储,顺序存储说简单点就是在数组里存储,这种需要大量连续的空间,不太实用的。链式存储就不需要连续的空间,下一个结点在哪里都无所谓,反正当前结点中保存着下一个结点的地址,对吧。
下面是自己写的简单的关于链表的程序,写的很简单,就当是做个备份了。
/* linklist.h */
#ifndef LINKLIST_H
#define LINKLIST_H
#include <iostream>
using namespace std;
typedef struct _Node {
int data;
struct _Node* next;
} Node, *Pnode;
class LinkList
{
private:
Pnode head, tail;
Pnode curEnumPos;
public:
LinkList();
~LinkList();
void Debug();
bool CreateList(); // 1
void DestroyList(); // 2
bool InsertNode(int); // 3
void TraverseList(); // 4
Pnode EnumList(); // 5
};
#endif
/* linklist.cpp */
#include "linklist.h"
LinkList::LinkList() {
head = NULL;
tail = NULL;
curEnumPos = NULL;
}
LinkList::~LinkList() {
if (head) {
DestroyList();
}
}
void LinkList::Debug() {
cout<<"head is "<<head<<", tail is "<<tail<<", curEnumPos is "<<curEnumPos<<endl;
}
bool LinkList::CreateList() {
if (head) {
cout<<"alarm! link exist, not need to create"<<endl;
return false;
}
Pnode p = new Node;
if (p) {
head = p;
tail = p;
return true;
}
return false;
}
void LinkList::DestroyList() {
while(head != NULL) {
Pnode t = head->next;
delete head;
head = t;
}
tail = NULL;
curEnumPos = NULL;
return true;
}
bool LinkList::InsertNode(int idata) {
if (!head) {
cout<<"error! linklist not exist"<<endl;
return false;
}
Pnode inode = new Node;
if (inode) {
inode->data = idata;
tail->next = inode;
tail = inode;
return true;
}
return false;
}
void LinkList::TraverseList() {
if (!head) {
cout<<"alarm! linklist not exist"<<endl;
return;
}
Pnode t = head->next;
while(t != NULL) {
cout<<t->data<<" ";
t = t->next;
}
cout<<endl;
}
Pnode LinkList::EnumList() {
if (curEnumPos) {
curEnumPos = curEnumPos->next;
} else {
curEnumPos = head->next;
}
return curEnumPos;
}