zoukankan      html  css  js  c++  java
  • 数据结构-链表-C++实现

    单链表

    • 单链表:数据域+指针(最后一个指针指向NULL);
    • 双链表:指针+数据域+指针
    • 循环链表:数据域+指针(最后一个指针指向头指针)NULL;
    单链表需要的函数
    * class Node
        元素:data,next;
    * class List
         List();
    	~List();
    	bool get_add_head(Node* e);//在头节点后面插入新元素
    	bool get_add_tail(Node* e);//在尾节点后面插入新元素
    	void get_traverse();//遍历链表
    	int get_length();//获取链表长度
    	bool get_empty();//判断链表是否为空
    	void get_clear();//清空链表
    	int get_ele_num(Node* p);//获取与传入参数值相同的链表中元素的序号并返回
    	bool get_i_ele(int e, Node* value);//获指定位序的元素,并赋值给传入的Node元素
    	void get_delete(int location);//删除指定位置元素	
    	bool get_add(int location, Node* e);//将新元素e添加在指定位置
    	bool get_pre(Node* e, Node* pre);//获取指定元素前驱
    	bool get_post(Node* e, Node* pos);//获取指定元素后继
    

    链表代码如下:
    另外写这个的时候必须要知道一些堆和栈的知识,可看我的博客中有转载,非常棒,程序员必读

    //Node.h
    #pragma once
    using namespace std;
    #include<iostream>
    class Node
    {
    public:
    	Node(int a);
    	Node();
    	~Node();
    	int date=-100;
    	Node* next;
    private:
    
    };
    //Node.cpp
    #include"Node.h"
    Node::Node(int a):date(a)
    {
    }
    Node::Node()
    {
    }
    
    Node::~Node()
    {
    }
    
    //List.h
    #pragma once
    #include<iostream>
    using namespace std;
    #include"Node.h"
    #define __debug__
    #ifdef __debug__
    #endif
    class List
    {
    public:
    	List();//
    	~List();//
    	bool get_add_head(Node* e);//在头节点后面插入
    	bool get_add_tail(Node* e);//在尾节点后面插入
    	void get_traverse();//遍历
    	int get_length();//获取长度
    	bool get_empty();//判空
    	void get_clear();//清空线性表
    	int get_ele_num(Node* p);//获取与传入参数值相同的链表中元素的序号
    	bool get_i_ele(int e, Node* value);//获指定位序的元素
    	void get_delete(int location);//删除某位置元素	
    	bool get_add(int location, Node* e);//添加在某位置元素
    	bool get_pre(Node* e, Node* pre);//获取前驱
    	bool get_post(Node* e, Node* pos);//获取后继
    private:
    	Node* node;//头节点
    	int length;
    };
    
    //List.cpp
    #include"List.h"
    List::List()
    {
    	length = 0;
    	node = new Node;
    	node->date = 0;
    	node->next = NULL;
    }
    bool List::get_add_head(Node* e)
    {
    	Node* c_node = new Node;
    	c_node=node->next;
    	Node* new_node = new Node;
    	if (new_node == NULL)
    	{
    		return false;
    	}
    	new_node->date = e->date;
    	new_node->next = c_node;
    	node->next = new_node;
    	length++;
    #ifdef __debug__
    	cout << node->next->date << endl;
    #endif
    	return true;
    }
    bool List::get_add_tail(Node* e)
    {
    	Node* c_node = new Node;
    	c_node = node;
    	while (c_node->next!=NULL)
    	{
    		c_node = c_node->next;
    	}
    	Node* new_node = new Node;
    	if (new_node == NULL)
    	{
    		return false;
    	}
    	new_node->date = e->date;
    	new_node->next = NULL;
    	c_node->next = new_node;
    	length++;
    #ifdef __debug__
    	cout << c_node->next->date << endl;
    #endif
    	return true;
    }
    void List::get_traverse()
    {
    	Node* c_node = new Node;
    	c_node = node->next;
    	while (c_node != NULL)
    	{
    		cout << c_node->date << endl;
    		c_node = c_node->next;
    	}
    }//遍历
    int List::get_length()
    {
    	return length;
    }//获取长度
    bool List::get_empty()
    {
    	return length == 0 ? true : false;
    }
    void List::get_clear()
    {
    	Node* c_node = new Node;
    	c_node = node->next;
    	while(c_node->next != NULL)
    	{
    		Node* new_node = new Node;
    		new_node=c_node->next;
    		delete c_node;//清除指向的内容
    		c_node = new_node;
    	}	
    	length = 0;
    	node->next = NULL;//易错,勿忘!
    }//清空线性表
    int List::get_ele_num(Node* p)//获取元素的位序
    {
    	Node* c_node = new Node;
    	c_node=node;
    	int count = 1;
    	while (c_node->next != NULL)
    	{
    		c_node = c_node->next;
    		if (c_node->date == p->date)
    		{
    			return count;
    		}
    		else
    		{	
    			count++;
    		}
    	}
    	return -1;
    }
    
    bool List::get_i_ele(int e, Node* value)
    {
    	if (e <1 || e > length)
    	{
    		cout << "Invalid num!" << endl;
    		return false;
    	}
    	Node* c_node = new Node;
    	c_node = node;
    	for (int i = 0; i <e; i++)
    	{
    		c_node = c_node->next;
    	}
    	value->date = c_node->date;
    #ifdef __debug__
    	cout << c_node->date << endl;
    #endif
    	return true;
    }//获指定位序元素的值
    
    void List::get_delete(int location)
    {
    	if (location<1 || location>length )
    	{
    		cout << "invalid num!!" << endl;
    	}
    	else
    	{
    		Node* c_node = new Node;
    		c_node = node;
    		Node* c_node_before = new Node;
    		for (int i = 0; i < location; i++)//遍历到了location对应的位置,与增加元素有所不同,需要注意!
    		{
    			c_node_before = c_node;
    			c_node = c_node->next;
    		}
    		c_node_before->next = c_node->next;
    		length--;
    	}	
    }//某位置删除元素
    
    bool List::get_add(int location, Node* e)
    {
    	if (location<1 || location>length)
    	{
    		cout << "Invalid num!" << endl;
    		return false;
    	}
    	else
    	{
    		Node* c_node = new Node;
    		c_node=node;
    		for (int i = 0; i < (location-1); i++)
    		{
    			c_node = c_node->next;
    		}//此时是遍历到了(location-1)那个元素的位置,然后在其后面插入新元素则是第location个元素。
    		Node* new_node = new Node;
    		if (new_node == NULL)
    		{
    			return false;
    		}
    		new_node->date = e->date;
    		new_node->next = c_node->next;
    		c_node->next = new_node;
    		length++;
    		return true;
    	}
    }//在某位置插入元素
    
    bool  List::get_pre(Node* e, Node* pre)
    {
    	Node* c_node = new Node;
    	c_node=node;
    	Node* pre_node = new Node;
    	while (c_node->next != NULL)
    	{
    		pre_node = c_node;
    		c_node = c_node->next;
    		if (c_node->date == e->date)
    		{	
    			if (pre_node==node)
    			{
    				cout << "No pre!" << endl;
    				return false;
    			}
    			else
    			{
    				pre->date = pre_node->date;
    				return true;
    			}	
    		}
    	}
    	cout << "No this value!" << endl;
    	return false;
    }//获取前驱
    bool  List::get_post(Node* e, Node* pos)
    {
    	Node* c_node = new Node;
    	c_node=node;
    	Node* pos_node = new Node;
    	while (c_node->next != NULL)
    	{
    		c_node = c_node->next;
    		if (c_node->date == e->date)
    		{
    			if (c_node->next== NULL)
    			{
    				
    				return false;
    			}
    			else
    			{
    				pos->date = c_node->next->date;
    				return true;
    			}
    		}
    	}
    	return false;
    }//获取后继
    
    List::~List()
    {
    	//get_clear();
    	delete node;
    	node = NULL;
    }
    
    
    //main.cpp 主要是一些验证代码
    #include"List.h"
    int main(int *argc, char* argv[])
    {
    	List* m = new List;
    	Node e1(3);
    	Node e2(5);
    	Node e3(6);
    	
    	m->get_add_head(&e1);
    	m->get_add_head(&e2);
    	m->get_add_tail(&e3);
    	m->get_add_head(&e3);
    	cout << endl;
    	//m->get_clear();
    	//cout << m->get_length() << endl;
    	//cout << m->get_empty() << endl;
    	m->get_traverse();
    	cout << endl;
    	Node* e4 = new Node;
    	Node* e5 = new Node;
    	cout << e4->date << endl;
    	m->get_pre(&e3, e4);
    	m->get_post(&e3, e5);
    	cout << e4->date << endl;
    	cout << e5->date << endl;
    	//m->get_add(3, &e2);
    	//m->get_delete(0);
    	//m->get_traverse();
    	//Node* e4 = new Node;
    	//m->get_i_ele(6, e4);
    	//cout<<m->get_ele_num(&e1)<<endl;
    	delete m;
    	m = NULL;
    	return 0;
    }
    
    Higher you climb, more view you will see.
  • 相关阅读:
    深入理解java虚拟机笔记Chapter12
    深入理解java虚拟机笔记Chapter11
    深入理解java虚拟机笔记Chapter8
    深入理解java虚拟机笔记Chapter7
    深入理解java虚拟机笔记补充-JVM常见参数设置
    深入理解java虚拟机笔记Chapter4
    深入理解java虚拟机笔记Chapter3-内存分配策略
    Java从Txt文本进行数据提取
    C语言-格式输入输出中“%d,%o,%x,%e,%f,%s,%p”
    嵌入式学习书籍
  • 原文地址:https://www.cnblogs.com/yyfighting/p/12500614.html
Copyright © 2011-2022 走看看