数据结构与算法 --- 链表

正在学习严薇敏的《数据结构》,把本书涉及到的代码都实现一遍,并以博客的形式记录。

单链表

#include <iostream>
#include <fstream>
using namespace std;

struct Node {
	int data;   // 数据域
	struct Node * next;     // 指针域
};

class ChainNode
{
public:
	ChainNode() { head = NULL; listsize = 0; }
	int GetList() { return listsize; }      // 返回listsize
	void CreateList();                      // 前插法创表
	void CreateList(bool switchs);          // 后插法创表
	bool GetElem(int lo, int & element);    // 获取第lo个元素
	int  LocateElem(int values);            // 获取values值的位置
	bool Insert(int index, int element);    // 在index的位置插入element
	bool Delete(int index);                 // 删除index位置上的节点
	void Display();                         // 显示函数
private:
	Node * head;    // 头节点
	int listsize;   // 表长
};

void ChainNode::CreateList()
{
	ifstream fin;
	fin.open("data.txt", ios::in);  // 数据存储在data.txt文本中

	head = new Node;
	head->next = NULL;              // 创建头节点
	int temp;
	Node * p = head;
	while (fin >> temp)
	{
		Node * q = new Node;
		q->data = temp;
		q->next = p->next;
		p->next = q;
		listsize++;                 // 计数
	}
	fin.close();
}

void ChainNode::CreateList(bool switchs)
{
	ifstream fin;
	fin.open("data.txt", ios::in);

	head = new Node;
	head->next = NULL;
	int temp;
	Node * p = head;
	while (fin >> temp)
	{
		Node * q = new Node;
		q->data = temp;
		q->next = NULL;
		p->next = q;
		p = q;
		listsize++;
	}
	fin.close();
}

bool ChainNode::GetElem(int lo, int & element)
{
	if (lo > listsize)
	{
		cout << "Out of the maximum range" << endl;
		return false;
	}
	if (lo <= 0)
	{
		cout << "Wrong index" << endl;
		return false;
	}

	Node * p = head->next;
	for (int i = 0; i < lo - 1; ++i)
		p = p->next;
	element = p->data;
	return true;
}

int ChainNode::LocateElem(int values)
{
	int ret = 0;
	Node * p = head;
	while (p && p->data != values)
	{
		p = p->next;
		ret++;
	}
	if (ret > listsize + 1)
		return -1;
	return ret;
}

bool ChainNode::Insert(int index, int element)
{
	if (index < 0)
	{
		cout << "Wrong index" << endl;
		return false;
	}
	if (index > listsize + 1)
	{
		cout << "Out of maximum range" << endl;
		return false;
	}

	Node * p = head;
	int ret = 1;
	while (p && ret < index)
	{
		p = p->next;
		ret++;
	}

	Node * q = new Node;
	q->data = element;
	q->next = p->next;
	p->next = q;
	listsize++;
	return true;
}

bool ChainNode::Delete(int index)
{
	if (index < 0)
	{
		cout << "Wrong index" << endl;
		return false;
	}
	if (index > listsize)
	{
		cout << "Out of maximum range" << endl;
		return false;
	}

	Node * p = head;
	int ret = 0;

	while (p->next && ret < index - 1)
	{
		p = p->next;
		ret++;
	}

	Node * q = p->next;
	p->next = q->next;
	delete q;
	listsize--;
	return true;
}

void ChainNode::Display()
{
	Node * p = head->next;
	while (p != NULL)
	{
		cout << p->data << " ";
		p = p->next;
	}
}

int main()
{
	return 0;
}

 

循环单链表

#include <iostream>
#include <fstream>

using namespace std;

struct Node {
	int data;	// 数据域
	struct Node * next;	// 指针域
};

class CircuNode
{
public:
	CircuNode() { head = NULL; listsize = 0; }
	int GetList() { return listsize; }
	void CreateList();						// 创表
	bool GetElem(int lo, int & element);    // 获取第lo个元素
	int  LocateElem(int values);            // 获取values值的位置
	bool Insert(int index, int element);    // 插入
	bool Delete(int index);                 // 删除
	void Display();						    // 显示
private:
	Node * head;
	int listsize;
};

void CircuNode::CreateList()
{
	ifstream fin;
	fin.open("data.txt", ios::in);

	head = new Node;
	head->next = head;
	int temp;
	Node * p = head;
	while (fin >> temp)
	{
		Node * q = new Node;
		q->data = temp;
		q->next = head;
		p->next = q;
		p = p->next;
		listsize++;
	}
	fin.close();
}

bool CircuNode::GetElem(int lo, int & element)
{
	if (lo > listsize)
	{
		cout << "Out of the maximum range" << endl;
		return false;
	}
	if (lo <= 0)
	{
		cout << "Wrong index" << endl;
		return false;
	}

	Node * p = head->next;
	for (int i = 0; i < lo - 1; ++i)
		p = p->next;
	element = p->data;
	return true;
}

int CircuNode::LocateElem(int values)
{
	int ret = 1;
	Node * p = head->next;
	while (p != head && p->data != values)
	{
		p = p->next;
		ret++;
	}
	if (ret > listsize + 1)
		return -1;
	return ret;
}

bool CircuNode::Insert(int index, int element)
{
    if (index < 0)
	{
		cout << "Wrong index" << endl;
		return false;
	}
	if (index > listsize + 1)
	{
		cout << "Out of maximum range" << endl;
		return false;
	}

	Node * p;
	if(index == 1)
        p = head;
    else
        p = head->next;

	int ret = 1;
	while(p != head && ret < index - 1)
    {
        p = p->next;
        ret++;
    }
    Node * q = new Node;
    q->data = element;
    q->next = p->next;
    p->next = q;
    listsize++;
    return true;
}

bool CircuNode::Delete(int index)
{
	if (index < 0)
	{
		cout << "Wrong index" << endl;
		return false;
	}
	if (index > listsize)
	{
		cout << "Out of maximum range" << endl;
		return false;
	}

	Node * p = head;
	int ret = 0;

	while (p->next != head && ret < index - 1)
	{
		p = p->next;
		ret++;
	}

	Node * q = p->next;
	p->next = q->next;
	delete q;
	listsize--;
	return true;
}

void CircuNode::Display()
{
	Node * p = head->next;
	while(p != head)
    {
        cout << p->data << " ";
        p = p->next;
    }
}

int main()
{
    return 0;
}

双向循环链表(鉴于本人能力有限难以对双向循环链表抽象出较好的通用功能,因此只写出了删除,插入,建表,显示功能,如有具体的应用,可以继承该基类)

#include <iostream>
#include <fstream>
using namespace std;

struct DulNode{
    int data;
    DulNode * prior;
    DulNode * next;
};

class DulChain
{
public:
    DulChain() { head = NULL; listsize = 0; }
    int GetList() { return listsize; }      // 返回listsize
    
    void CreateList();                      // 建表
    bool Insert(int index, int element);    // 插入
    bool Delete(int index);                 // 删除
    void Display(bool state = true);        // 显示
private:
    DulNode * head;
    int listsize;
};

void DulChain::CreateList()
{
    ifstream fin;
	fin.open("data.txt", ios::in);  // 数据存储在data.txt文本中

	head = new DulNode;
	head->prior = NULL;
	head->next = NULL;
	int temp;
	DulNode * p = head;

	while(fin >> temp)
    {
        DulNode *q = new DulNode;
        q->data = temp;
        q->prior = p;
        q->next = head;
        p->next = q;
        p = p->next;
        listsize++;
    }
    head->prior = p;
	fin.close();
}


void DulChain::Display(bool state)
{
    if(state == true)                // next遍历
    {
        DulNode * p = head->next;
        while(p != head)
        {
            cout << p->data << " ";
            p = p->next;
        }
    }
    else
    {
        DulNode * p = head->prior;  // prior遍历
        while(p != head)
        {
            cout << p->data << " ";
            p = p->prior;
        }
    }
}

bool DulChain::Insert(int index, int element)
{
    if (index < 0)
	{
		cout << "Wrong index" << endl;
		return false;
	}
	if (index > listsize + 1)
	{
		cout << "Out of maximum range" << endl;
		return false;
	}

	DulNode * p;
	if(index == 1)
        p = head;
    else
        p = head->next;

	int ret = 1;
	while(p != head && ret < index - 1)
    {
        p = p->next;
        ret++;
    }
    DulNode * q = new DulNode;
    q->data = element;
    q->prior = p;           // 相对于循环链表改动地方
    q->next = p->next;
    p->next->prior = q;     // 相对于循环链表改动地方
    p->next = q;
    listsize++;
    return true;
}

bool DulChain::Delete(int index)
{
    if (index < 0)
	{
		cout << "Wrong index" << endl;
		return false;
	}
	if (index > listsize)
	{
		cout << "Out of maximum range" << endl;
		return false;
	}

	DulNode * p = head;
	int ret = 0;

	while (p->next != head && ret < index - 1)
	{
		p = p->next;
		ret++;
	}

	DulNode * q = p->next;
	q->next->prior = p;     //相对于循环链表改动地方
	p->next = q->next;

	delete q;
	listsize--;
	return true;
}


int main()
{
    return 0;
}

猜你喜欢

转载自blog.csdn.net/adorkable_thief/article/details/83961423