单链表(C++实现)

list.h

#ifndef SINGLE_LIST_H
#define SINGLE_LIST_H

#include <cstdlib>
//template <class Item>
class node
{
public:
	typedef double valueType;

	node(const valueType& init_data = valueType( ), node *init_link = NULL)
	{
		data_field = init_data; link_field = init_link;
	}

	void set_data(const valueType& new_data)//the node now contain the specified new data
	{
		data_field = new_data;
	}

	void set_link(node *new_link)// the node now contain the specified new link
	{
		link_field = new_link;
	}
	valueType data( ) const {return data_field; } //the return value is the data form this node

	const node* link( ) const {return link_field ;}//   the return value is the  link form this node

	node *link {return link_field ;} //non-const

private:
	valueType data_field ;
	node *link_field ;
	
};

std::size_t list_length( const node *head_ptr) ; //the value returned is the number of nodes in the list

void list_head_insert(node*& head_ptr, const node::valueType& entry) ;//A new node containing the given entry has been
                                                                                                             //added at the head of the linked list 

void list_insert(node * previous, const node::valueType& entry);//A new node containing the given entry has been added 
                                                                                               //after the node that previous points to
node* list_search(node* head_ptr, const node::valueType& target ); //

const node* list_search(const node* head_ptr, const node::valueType& target);//返回与target值相同的node

node* list_locate(node* head_ptr, std::size_t position) ;//返回在poisition处的node

const node* list_locate(const node* head_ptr, std::size_t position) ;

void list_head_remove(node*& head_ptr );//去除头结点

void list_remove(node* previous);//去除previous后一个node

void list_clear(node*& head_ptr );//链表清空

void list_copy(const node* source_ptr, node*& head_ptr, node*& tail_ptr);//list 复制

};
#endif



list.cpp

#include "list.h"


size_t list_length(const node *head_ptr)
{
	const node *cursor ;
	size_t len ; 
	len = 0;

	for (cursor = head_ptr; cursor != NULL; cursor++)
	{
		++len;
	}

	return len ;
}

void list_head_insert(node*& head_ptr, const node::valueType& entry)
{
	head_ptr = new node(entry, head_ptr) ;
}

void list_insert(node* previous, const node::valueType& entry)
{
	node *insert_ptr ;
	insert_ptr = new node ;
	insert_ptr->set_data(entry) ;
	insert_ptr->set_link(previous->link_field) ;
	previous->set_link(insert_ptr) ;
}

node* list_search(node* head_ptr, const node::valueType& target)
{
	node *cursor ;

	for (cursor = head_ptr; cursor != NULL ; cursor)
	{
		if (target == cursor->data() )
		{
			return cursor ;
		}
	}

	return NULL ;
}

const node* list_search(const node* head_ptr, const node::valueType& target)
{
	const node *cursor ;

	for (cursor = head_ptr; cursor != NULL ; cursor)
	{
		if (target == cursor->data() )
		{
			return cursor ;
		}
	}

	return NULL ;
}

node* list_locate(node* head_ptr, std::size_t position)
{
	node * cursor = head_ptr ;

	for (int i =0; (i < position) &&(cursor != NULL); i++)
	{
		cursor = cursor->link_field ;
	}	
	  return cursor ;  
}

const node* list_locate(const node* head_ptr, std::size_t position)
{
	const node * cursor = head_ptr ;

	for (int i =0; (i < position) &&(cursor != NULL); i++)
	{
		cursor = cursor->link_field ;
	}	
	return cursor ;  
}

void list_head_remove( node*& head_ptr)
{
	node *remove_ptr ;

	remove_ptr = head_ptr ;
	head_ptr = head_ptr->link_field ; 
	delete remove_ptr ;
}

void list_clear(node*& head_ptr )
{
	while (head_ptr != NULL)
	{
		list_head_remove(head_ptr) ;
	}
}

void list_copy(const node* source_ptr, node*& head_ptr, node*& tail_ptr )
{
	head_ptr = NULL ;
	tail_ptr = NULL ;

	if (source_ptr == NULL)
	{
		return  ;
	}

	list_head_insert(head_ptr , source_ptr->data()) ;
	tail_ptr = head_ptr ;

	source_ptr = source_ptr->link_field ;

	while (source_ptr != NULL)
	{
		list_insert(tail_ptr , source_ptr->data( ));

		tail_ptr = tail_ptr->link_field ; 
		source_ptr = source_ptr->link_field ;
	}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简的C++程序,用于实现链表的基本操作(包括插入、删除和打印): ```cpp #include <iostream> // 定义链表节点 struct Node { int data; // 存储节点的数据 Node* next; // 指向下一个节点的指针 }; // 在链表末尾插入节点 void insert(Node*& head, int data) { Node* newNode = new Node(); // 创建新节点 newNode->data = data; newNode->next = nullptr; if (head == nullptr) { head = newNode; } else { Node* temp = head; while (temp->next != nullptr) { temp = temp->next; } temp->next = newNode; } } // 在链表中删除指定的节点 void remove(Node*& head, int data) { if (head == nullptr) { return; } Node* temp = head; Node* prev = nullptr; // 当找到要删除的节点时,将其从链表中移除 while (temp != nullptr && temp->data != data) { prev = temp; temp = temp->next; } if (temp == nullptr) { return; } if (prev == nullptr) { head = head->next; } else { prev->next = temp->next; } delete temp; // 释放内存 } // 打印链表中的所有节点 void printList(Node* head) { Node* temp = head; while (temp != nullptr) { std::cout << temp->data << " "; temp = temp->next; } std::cout << std::endl; } int main() { Node* head = nullptr; // 链表的头节点 // 插入节点 insert(head, 1); insert(head, 2); insert(head, 3); insert(head, 4); // 打印链表 std::cout << "链表中的节点:"; printList(head); // 删除节点 remove(head, 3); // 打印链表 std::cout << "删除后的链表:"; printList(head); return 0; } ``` 这个程序定义了一个`Node`结构体来表示链表的节点,其中`data`存储节点的数据,`next`指针指向下一个节点。通过`insert`函数可以在链表末尾插入新节点,通过`remove`函数可以删除链表中指定的节点,通过`printList`函数可以打印链表中的所有节点。在主函数中,我们可以使用这些函数来操作链表

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值