删除线性表指定位置的元素(单链表实现)

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/gonghuiyun19910610/article/details/49906809

刚开始接触C++和数据结构,写的第一个C++作业题,程序短小却调了快两天。万事开头难啊,纪录一下下。

课本是《数据与算法分析》(C++版)(第三版)Clifford A. Shaffer


4.1假设一个线性表包含下列元素:<|2,23,15,5,9>,使用图4.1的List ADT编写一些C++语句,删除值为15的元素。

#include "stdafx.h"
#include <iostream>
#include <stdlib.h>
#include <assert.h>
#include <windows.h>
using namespace std;

template <typename E> 
class List{
private:
	void operator =(const List&) {}
	List(const List&) {}
public:
	List() {}
	virtual ~List() {}
	virtual void clear() = 0;
	virtual void insert(const E& item) = 0;
	virtual void append(const E& item) = 0;
	virtual E remove() = 0;
	virtual void moveToStart() = 0;
	virtual void moveToEnd() = 0;
	virtual void prev() = 0;
	virtual void next() = 0;
	virtual int length() const  = 0;
	virtual int currPos() = 0;
	virtual void moveToPos(int pos) = 0;
	virtual const E& getValue() const = 0;
};

template <typename E>
class Link{
public:
	E element;
	Link *next;
	Link(const E& elemval, Link* nextval = NULL) {
		element = elemval;
		next = nextval;
	}
	Link(Link* nextval = NULL){
		next = nextval;
	}
};

template<typename E> 
class LList : public List<E>{
private:
	Link<E>* head;
	Link<E>* tail;
	Link<E>* curr;
	int cnt;

	void init(){
		curr = tail = head = new Link<E>;
		cnt = 0;
	}
	void removeall(){
		while (head != NULL){
			curr = head;
			head = head->next;
			delete curr;
		}
	}
public:
	LList(int size = defaultSize){init();}
	~LList() { removeall(); }
	void print(){
		int cnt2;
		cnt2 = cnt;
		curr = head->next;
		while (cnt2) {
			cout << curr->element<<" ";
			curr = curr->next;
			cnt2--;
		}
	}
	void clear() {
		removeall();
		init();
	}
	void insert(const E& it){
		curr->next = new Link<E>(it, curr->next);
			if (tail == curr)
				tail = curr->next;
		cnt++;
	}
	void append(const E& it) {
		tail = tail->next = new Link<E>(it,NULL);
		cnt++;
	}
	E remove() {
		assert(curr->next!=NULL, "No element");
		E it = curr->next->element;
		Link<E>* ltemp = curr->next;
		curr->next = curr->next->next;
		delete ltemp;
		cnt--;
		return it;
	}
	void moveToStart() { curr = head; }
	void moveToEnd() { curr = tail; }
	void prev() {						//find curr before
		if (curr == head) return;
		Link<E>* temp = head;
		while (temp != curr)
			temp = temp->next;
		curr = temp;
	}
	
	void next() {
		if(curr->next!=tail)
		curr = curr->next;
	}

	int length() const{
		return cnt;
	}

	int currPos(){
		Link<E>* temp = head;
		int i;
		for (i = 0; curr != temp; i++)
		temp = temp->next;
		return i;
	}		
	void moveToPos(int pos) {
		assert((pos>=0) && (pos <= cnt), "Position out of range");
		curr = head;
		for (int i = 0; i< pos; i++)
			curr = curr->next;
	}

	const E& getValue() const {
		assert(curr->next != NULL, "No Value");
			return curr->next->element;
	}
};

int main()
{
	int num;
	int i;
	LList<int>L1(100);
	L1.clear();
	cout << "请输入5个数:"<<endl;
	for (i = 0; i < 5; i++) {
		cin >> num;
		L1.append(num);	//在线性表的尾部插入节点
	}
	cout << "线性表包含下列元素:" << endl;
	L1.print();			//打印线性表的所有元素
	L1.moveToPos(2);	//将curr移动到要删除的元素前
	L1.remove();		//删除curr的后一个节点
	cout << endl<<"删除第三个元素后,线性表包含下列元素:" << endl;
	L1.print();	
	system("pause");
}


输入:2 23 15 5 9,运行结果如下图





展开阅读全文

单链表删除指定元素

05-30

我在单链表类中定义了head,tail指针,现在我想实现删除单链表中指定的元素,重复出现的也需要删除,我老是想不出怎么写,自己写的却输出不正确,把指定元素后面的给删除了。rn[img=https://forum.csdn.net/PointForum/ui/scripts/csdn/Plugin/003/onion/1.gif][/img]大家让我偷懒下吧,这个函数,我想了好久!rn定义的类如下rn[code=C/C++]rntemplaternclass Nodernrn public:rn T element;rn Node *next;rnrn Node() //构造函数rn rn next = NULL;rn rnrn Node(T element)rn rn this->element = element;rn next = NULL;rn rn;rnrntemplaternclass LinkedList rnrn public:rn LinkedList(); //创建一个默认的链表rn void addFirst(T element); rn void addLast(T element);rn T getFirst();rn T getLast();rn T removeFirst() throw(runtime_error);rn T removeLast();rn void add(T element); //在链尾添加元素rn void add(int index, T element); //在指定位置添加元素rn void clear();rn bool contains(T element); //如果列表中包含指定元素,则返回为真rn T get(int index); //返回列表中指定的元素rn int indexOF(T element); //返回列表中第一个匹配元素的节点下标rn bool isEmpty(); rn int lastIndexOF(T element); //返回列表中最后一个匹配元素的节点下标rn void remove(T element);rn int getSize();rn T removeAt(int index); //删除指定位置元素,并将其返回rn T set(int index, T element); //将指定位置元素设置为新值,并返回原址rnrn private:rn Node *head, *tail;rn int size;rn;rn[/code]rn函数如下rn[code=C/C++]rntemplaternvoid LinkedList::remove(T element)rn//从列表中删除指定元素rn if(size == 0)rn throw runtime_error("链表长度为0\n");rn elsern rn Node *temp = head;rn for(int i = 0; i < size - 1; i++)rn rn if(temp->element == element)rn rn temp = temp->next->next;rn --size;rn rn elsern temp = temp->next;rn rn if(temp->element == element)rn rn removeLast();rn --size;rn rn rnrn[/code]rn这个函数想不出如何用单链表弄,大家帮忙下,我真的想早点解决掉!rn[img=https://forum.csdn.net/PointForum/ui/scripts/csdn/Plugin/003/onion/1.gif][/img]rn积分献上,谢谢了! 论坛

没有更多推荐了,返回首页