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

刚开始接触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,运行结果如下图





  • 6
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值