• C++ 实现链表常用功能


    首先头文件,定义了链表的常用函数:

    typedef struct node 
    {
    	int data;
    	struct node* next;
    } Node;
    
    class LinkListUtil
    {
    public:
    	LinkListUtil();
    	~LinkListUtil();
    
    	//create
    	Node* createByArray(int arr[], int len);
    
    	int getLenght(Node *head);
    
    	void println(Node *head);
    
    	Node* remove(Node *head, int num);
    
    	//Insert element into the link list by the index.
    	//if index is -1 then insert the num into the link list end.
    	Node* insert(Node *head, int num, int index = -1);
    
    	Node* sort(Node *head);
    
    	Node* reserve(Node *head);
    
    	Node* removeHead(Node *head);
    };
    

    然后是实现:

    #include "stdafx.h"
    #include "LinkListUtil.h"
    
    LinkListUtil::LinkListUtil()
    {
    }
    
    
    LinkListUtil::~LinkListUtil()
    {
    }
    
    
    Node* LinkListUtil::createByArray(int arr[], int len)
    {
    	//Declare head node, previous node, new node.
    	Node *head, *p, *n;
    
    	//Init head node.
    	head = new Node();
    
    	p = head;
    
    	for (int i = 0; i < len; i++)
    	{
    		n = new Node();
    		n->data = arr[i];
    		p->next = n;
    		p = n;
    	}
    	head = head->next;
    	//head->next = nullptr;	//?
    	return head;
    }
    
    int LinkListUtil::getLenght(Node *node)
    {
    	int len = 0;
    	Node *temp = node;
    	while (temp != nullptr)
    	{
    		temp = temp->next;
    		len++;
    	}
    	return len;
    }
    
    void LinkListUtil::println(Node *node)
    {
    	Node *temp = node;
    	while (temp != nullptr)
    	{
    		printf("the node data is : %d
    ", temp->data);
    		temp = temp->next;
    	}
    }
    
    Node* LinkListUtil::remove(Node *head, int num)
    {
    	Node *temp = head;
    	Node *pre = nullptr;
    	//find the node
    	while (temp != nullptr)
    	{
    		if (temp->data != num)
    		{
    			pre = temp;
    			temp = temp->next;
    		}
    		else 
    		{
    			break;
    		}
    	}
    	if (pre == nullptr)
    	{
    		head = head->next;
    	}
    	else {
    		pre->next = temp->next;
    	}
    	delete temp;
    	return head;
    }
    
    Node* LinkListUtil::insert(Node *head, int num, int index)
    {
    	int len = getLenght(head);
    	if (index >= len) 
    	{
    		printf("The index is greater than the length of link list!
    "); 
    		return head;
    	}
    	Node *temp = head;
    	Node *pre = nullptr;
    	//if index is -1 then insert the num into the link list end.
    	int tempIndex = 0;
    	if (index <= -1)
    		index = len;
    	//find the insert point.
    	while (temp != nullptr && index != tempIndex)
    	{
    		tempIndex++;
    		pre = temp;
    		temp = temp->next;
    	}
    	//insert the num.
    	Node *nd;
    	nd = new Node();
    	nd->data = num;
    	if (pre == nullptr)
    	{
    		nd->next = head;
    		delete pre, temp;
    		return nd;
    	}
    	else {
    		nd->next = temp;
    		pre->next = nd;
    		return head;
    	}
    }
    
    Node* LinkListUtil::sort(Node *head)
    {
    	//if only has one element or none elements
    	if (head == nullptr || head->next == nullptr)
    		return head;
    
    	Node *p0, *p1;
    	int temp = 0;
    	p0 = head;
    	//bubble sort
    	while (p0 != nullptr && p0->next != nullptr)
    	{
    		p1 = p0->next;
    		while (p1 != nullptr)
    		{
    			if (p0->data > p1->data)
    			{
    				temp = p0->data;
    				p0->data = p1->data;
    				p1->data = temp;
    			}
    			p1 = p1->next;
    		}
    		p0 = p0->next;
    	}
    	return head;
    }
    
    Node* LinkListUtil::reserve(Node *head)
    {
    	//if only has one element or none elements
    	if (head == nullptr || head->next == nullptr)
    		return head;
    
    	Node *p0, *p1, *p2;
    	p0 = head;
    	p1 = p0->next;
    	while (p1 != nullptr)
    	{
    		p2 = p1->next;
    		p1->next = p0;
    		p0 = p1;
    		p1 = p2;
    	}
    	head->next = nullptr;
    	return p0;
    }
    
    Node* LinkListUtil::removeHead(Node *head)
    {
    	//if only has one element or none elements
    	if (head == nullptr || head->next == nullptr)
    		return head;
    	Node *p0;
    	p0 = head->next;
    	head->next = p0->next;
    	head = p0;
    	return head;
    }
    
    最后是main测试:

    // Win32Project2.cpp 
    //
    
    #include "stdafx.h"
    #include <iostream>  
    #include <string>  
    #include <assert.h>
    
    #include "LinkListUtil.h"
    
    using namespace std;
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	//////////////////////////////////////////////////////////////////////////
    
    	int arr[5] {3, 1, 9, 4, 6};
    
    	auto linkList = new LinkListUtil();
    	auto head = linkList->createByArray(arr, 5);
    	//Get linkList size;
    	auto len = linkList->getLenght(head);
    	printf("LinkList's length:%d
    ", len);
    
    	printf("Print LinkList:
    ");
    	linkList->println(head);
    
    	printf("Remove element 1 and print LinkList:
    ");
    	head = linkList->remove(head, 1);
    	linkList->println(head);
    
    	printf("Insert element 5 into the index 1 and print LinkList:
    ");
    	head = linkList->insert(head, 5, 1);
    	linkList->println(head);
    
    	printf("Insert element 2 into the end and print LinkList:
    ");
    	head = linkList->insert(head, 2);
    	linkList->println(head);
    
    	printf("Bubble sort:
    ");
    	head = linkList->sort(head);
    	linkList->println(head);
    
    	printf("reserve:
    ");
    	head = linkList->reserve(head);
    	linkList->println(head);
    
    	printf("remove head:
    ");
    	head = linkList->removeHead(head);
    	linkList->println(head);
    	//clear
    	delete linkList;
    	//////////////////////////////////////////////////////////////////////////
    	system("pause");
    	return 0;
    }
    



  • 相关阅读:
    1.13 开源软件是什么?有哪些?
    1.10 Linux桌面环境(桌面系统)大比拼[附带优缺点
    Bracket Seuence deletion (找规律,2个元素分别分析一下)
    web学习笔记
    计算机结构 week2
    Alice and the Cake (CF1654C 模拟题,逆续想法)
    star MST (dp)
    For gamers. BY GAMERS (dp预处理+二分)
    Preorder (树贡献问题, 这题关键重复情况的如何判断)
    python 笔记
  • 原文地址:https://www.cnblogs.com/SunnyYue/p/3970264.html
Copyright © 2020-2023  润新知