线性表的链式存储结构

    线性表在我看来就是用一根线把所有数据穿起来,形象吧,他具体的定义自己上网查吧,我是不喜欢背定义的,很难记住。

 

    线性表在内存中有两种存储方式,一种叫顺序存储,另一种叫链式存储,顺序存储说简单点就是在数组里存储,这种需要大量连续的空间,不太实用的。链式存储就不需要连续的空间,下一个结点在哪里都无所谓,反正当前结点中保存着下一个结点的地址,对吧。

 

    下面是自己写的简单的关于链表的程序,写的很简单,就当是做个备份了。

 

/* 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;
}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值