基于C++类模板的链表

一、基于C++类模板定义一个链表,管理学生成绩信息( 学号和成绩),进行排序后再输出。

#include
    
    
     
     
#include
     
     
      
      
using namespace std;
template
      
      
       
        class list{
	struct node{
		T data;
		node *next;
	}
	*head, *tail;
public:
	list(){
		head = tail = NULL;
	};
	void Insert(T *item){
		node *pn;
		pn = new node;
		pn->next + head;
		pn->data = *item;
		head = pn;
		if (tail == NULL){
			tail = pn;
		}
	};
	void Append(T *item){
		node *pn;
		pn = new node;
		pn->next = NULL;
		pn->data = *item;
		if (tail == NULL){
			head = tail = pn;
		}
		else{
			tail->next = pn;
			tail = pn;
		}
	};
	T Get(){
		if (head == NULL){
			return 0;
		}
		T tmp = head->data;
		node *pn = head;
		if (head->next == NULL){
			head = tail = NULL;
		}
		else{
			head = head->next;
		}
		delete(pn);
		return tmp;
	}
};
class Student{
public:
	int num;
	int score;
};
int main(){
	Student s;
	list
       
       
        
        link1;
	list
        
        
          link2; for (int i = 0; i < 5; i++){ cin >> s.num >> s.score; link2.Insert(&s); link1.Append(&s.score); } cout<<"result"< 
          
        
       
       
      
      
     
     
    
    

程序结果



二、编写程序,从键盘输入若干整形数据,以单链表的形式存储并按升序排列,再向其中插入任一整形数据,重新按照升序方式输出。

#include 
    
    
     
     
using namespace std;
class List{
protected:
	int length;
public:
	int GetLength(){
		return length;
	}
	virtual int Get(int i) = 0;
	virtual bool Set(int x, int i) = 0;
	virtual void MakeEmpty() = 0;
	virtual bool Insert(int value, int i) = 0;
	virtual int Remove(int i) = 0;
	virtual int RemoveValue(int value) = 0;
};
class Node{
public:
	Node(){
		next = NULL;
	}
	Node(const int &item, Node *next1 = NULL){
		data = item;
		next = next1;
	}
	int data;
	Node *next;
};
class Linklist :public List{
protected:
	Node *head;
public:
	Node *GetHead(){
		return head;
	}
	Node *GetNext(Node &n){
		if (n.next == head){
			n.next->next;
		}
		else{
			return n.next;
		}
	}
	int Get(int i){
		Node *p = Find(i);
		return p->data;
	}
	bool Set(int x, int i){
		Node *p = Find(i);
		if (p == NULL || p == head){
			return false;
		}
		else{
			p->data = x;
		}
		return true;
	}
	Node *Find(int i){
		if (i<0 || i>length){
			return NULL;
		}
		if (i=0){
			return head;
		}
		Node *p = head->next;
		int j = 1;
		while(p!=NULL&&j<1){
			p = p->next;
			j++;
		}
		return p;
	}
	Node *FindValue(int value){
		Node *p = head->next;
		int i = 1;
		while (i++ <= length&&p->data!=value){
			p = p->next;
		}
		return p;
	}
	void MakeEmpty(){
		Node *q = head->next;
		int i = 1;
		while (i++ <= length){
			head->next=q->next;
			delete q;
			q = head->next;
		}
		length = 0;
	}
	virtual bool Insert(int value, int i) = 0;
	virtual int Remove(int i) = 0;
	virtual int RemoveValue(int value) = 0;
};
class Singlelist:public Linklist{
public:
	Singlelist(){
		head = new Node;
		length = 0;
	}
	Singlelist(Singlelist &l){
		Copy(l);
	}
	~Singlelist(){
		MakeEmpty();
		delete head;
	}
	Singlelist &Copy(Singlelist &l){
		Node *p, *q, *r;
		head = NULL;
		length = l.length;
		if (!l.head){
			return *this;
		}
		head = new Node;
		if (!l.head){
			return *this;
		}
		head->data = (l.head)->data;
		head-> next = NULL;
		r = NULL;
		p = head;
		q = l.head->next;
		while (q){
			r = new Node;
			if (!r){
				return *this;
			}
			r->data = q->data;
			r->next = NULL;
			p->next = r;
			p = p->next;
			q = q->next;
		}
		return *this;
	}
	bool Insert(int value, int i){
		Node *p = Find(i - 1);
		if (p == NULL){
			return false;
		}
		Node *newnode = new Node(value, p->next);
		p->next = newnode;
		length++;
		return true;
	}
	int Remove(int i){
		Node *p = Find(i - 1),*q;
		q = p->next;
		p->next = q->next;
		int value = q->data;
		delete q;
		length--;
		return value;
	}
	int RemoveValue(int value){
		Node *p = head, *q;
		while (p->next != NULL&&p->next->data != value){
			p = p->next;
		}
		q = p->next;
		p->next = q->next;
		delete q;
		length--;
		return value;
	}
	Singlelist &operator=(Singlelist &l){
		if (head){
			MakeEmpty();
		}
		Copy(l);
		return *this;
	}
	friend ostream &operator << (ostream &out, Singlelist &l){
		Node *p = l.head->next;
		while (p){
			out << p->data << " ";
			p->next;
		}
		out << endl;
		return out;
	}
	void sort(){
		Node *tmp = head;
		while (tmp != NULL){
			Node *q = tmp->next;
		    while (q != NULL){
				if (q->data > tmp->data){
					int r = tmp->data;
					tmp->data = q->data;
					q->data = r;
				}
		    }
		tmp = tmp->next;
	    }
	}
};
int main(){
	Singlelist a;
	int i,t,x,length;
	cout << "input length"<
     
     
      
      > length;
	cout << "input data" << endl;
	for (i = 0; i <= length; i++){
		cin >> t;
		a.Insert(t, i);
	}
	a.sort();
	cout << "after sort" << endl;
	cout << a << endl;
	cout << "input another one" << endl;
	cin >> x;
	a.Insert(x, length++);
	cout << "after sort" << endl;
	cout<
      
      <
       
       
     
     
    
    

结果




面向对象程序设计课程作业 1. 请创建一个数据类型为T的链表类模板List,实现以下成员函数: 1) 默认构造函数List(),将该链表初始化为一个空链表(10分) 2) 拷贝构造函数List(const List& list),根据一个给定的链表构造当前链表(10分) 3) 析构函数~List(),释放链表中的所有节点(10分) 4) Push_back(T e)函数,往链表最末尾插入一个元素为e的节点(10分) 5) operator<<()友元函数,将链表的所有元素按顺序输出(10分) 6) operator=()函数,实现两个链表的赋值操作(10分) 7) operator+()函数,实现两个链表的连接,A=B+C(10分) 2. 请编写main函数,测试该类模板的正确性: 1) 用List模板定义一个List类型的模板类对象int_listB,从键盘读入m个整数,调用Push_back函数将这m个整数依次插入到该链表中;(4分) 2) 用List模板定义一个List类型的模板类对象int_listC,从键盘读入n个整数,调用Push_back函数将这n个整数依次插入到该链表中;(4分) 3) 用List模板定义一个List类型的模板类对象int_listA,调用List的成员函数实现A = B + C;(4分) 4) 用cout直接输出int_listA的所有元素(3分) 5) 用List模板定义List类型的模板类对象double_listA, double_listB, double_listC,重复上述操作。(15分) 3. 输入输出样例: 1) 输入样例 4 12 23 34 45 3 56 67 78 3 1.2 2.3 3.4 4 4.5 5.6 6.7 7.8 2) 输出样例 12 23 34 45 56 67 78 1.2 2.3 3.4 4.5 5.6 6.7 7.8
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值