数据结构:基于带头结点单链表实现链队列以及链栈(考研)

一.带头结点单链表类

#include <bits/stdc++.h>
using namespace std;


class LinkedList {
private:
	struct node {
		int val;
		node *next;
	
		node(int x, node *next) {
			this->val = x;
			this->next = next; 
		}
	};
	
	
	node *head;
	int size;
	
public:
	LinkedList() {
		head = new node(0, NULL);
		size = 0;
	}
	
	int getSize() {return size;}
	bool isEmpty() {return size == 0;}
	//[0,size] 
	void insert(int index, int x) {
		if (index<0 || index>size) {
			cout << "the index is invalid!" << endl;
			return;
		}
	
		node *p = head;
		for (int i=0; i<index; ++i) p = p->next;
		node *q = new node(x, p->next);
		p->next = q;
		size ++;	
	}
	
	void insertTail(int x) {
		insert(size, x);
	}
	void insertHead(int x) {
		insert(0, x);
	}
	
	
	//[0,size-1]
	int del(int index) {
		if (index<0 || index>=size) {
			cout << "the index is invalid" << endl;
			return -1; 
		}
	
		if (isEmpty()) {
			cout << "the LinkedList is null!" << endl;
			return -1;
		}
	
		node *p = head;
		for (int i=0; i<index; ++i) p = p->next;
		node *del = p->next;
		p->next = del->next;
		int res = del->val;
		delete del;
		size --;
		
		return res;
	}
	int delFirst() {return del(0);}
	int delLast() {return del(size-1);}


	int get(int index) {
		if (index<0 || index>size) {
			cout << "the index is invalid!" << endl;
			return -1; 
		}
	
		if (isEmpty()) {
			cout << "the LinkedList is null!" << endl;
			return -1;
		}
		node *p = head->next;
		for (int i=0; i<index; ++i) p = p->next;
		return p->val;
	}
	int getLast() {return get(size-1);}
	int getFirst() {return get(0);}

	void show() {
		node *p = head->next;
		while (p) {
			cout << p->val << " ";
			p = p->next;
		}
		cout << endl;
	} 
}; 

二.链栈

#include "LinkedList.h"



class LinkedListStack {
private:
	LinkedList list;
	
public:
	stack() {
		list = LinkedList();
	}
	
	bool isEmpty() {
		return list.isEmpty();
	}
	
	void push(int x) {
		list.insertHead(x);
	}
	
	int pop() {
		return list.delFirst();
	}
	
	int top() {
		return list.getFirst();
	}
};




int main() {
	int a[] = {1,2,3,4,5};
	int n = sizeof(a)/sizeof(int);
	LinkedListStack s;
	for (int i=0; i<n; ++i) s.push(a[i]);
	while (!s.isEmpty()) {
		cout << s.top() << " ";
		s.pop();
	} 
	return 0;
}

三.链队列

#include "LinkedList.h"


class LinkedListQueue {
private:
	LinkedList list;
	
	
public:
	LinkedListQueue() {
		list = LinkedList();
	}
	
	bool isEmpty() {return list.isEmpty();}
	
	void push(int x) {
		list.insertTail(x);
	}
	
	int front() {
		return list.getFirst();
	}
	
	int pop() {
		return list.delFirst();
	}
};


int main() {
	int a[] = {1,2,3,4,5};
	int n = sizeof(a)/sizeof(int);
	
	LinkedListQueue q;
	for (int i=0; i<n; ++i) q.push(a[i]);
	while (!q.isEmpty()) {
		cout << q.front() << " ";
		q.pop();
	}
	cout << endl;
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值