题目:实现一个递增排序的单链表(不允许重复),即每次向链表中插入一个或多个元素后都能保证链表仍是递增有序的,在此基础上实现一些基本操作,如插入元素、清空元素、判断两个链表是否相等及查找某个指定的元素。
为了实现该功能,首先定义一个头文件linked_list.h声明单链表的基本操作:
#ifndef LINKED_LIST_H_
#define LINKED_LIST_H_
#define _INVALID 0xFFFFFFFF
class linked_list {
// value
unsigned int data;
// pointer to the next node in list
linked_list *next;
public:
// instantiated a linked list with the element value. If value is not specified
// an empty list is instantiated
linked_list(unsigned int value = _INVALID);
// destructor
~linked_list();
// check if the list is empty
bool empty();
// insert a value into the list.
void insert(unsigned int value);
// insert all the values from list into the current linked list.
void add(linked_list *list);
// get the size of the linked list.
int size();
// is the current set equal to s (does it contain

本文介绍如何实现一个递增排序且不允许重复的单链表,包括插入元素、清空、比较链表相等和查找元素等操作。关键在于插入元素时,根据链表状态判断插入位置,利用递归处理复杂情况。
最低0.47元/天 解锁文章
770

被折叠的 条评论
为什么被折叠?



