关闭

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

标签: C++数据结构
579人阅读 评论(0) 收藏 举报
分类:

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





0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:10252次
    • 积分:252
    • 等级:
    • 排名:千里之外
    • 原创:11篇
    • 转载:2篇
    • 译文:0篇
    • 评论:10条
    关于我
    文章分类
    最新评论