高分跪求ADT形式链表,级由链表派生的队列。

woyingjie 2004-06-30 11:38:29
rt 课程设计急用,谢谢各位了。
...全文
101 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
aixuer 2004-07-01
  • 打赏
  • 举报
回复
吼吼,我的是不是清除一些? 给分,给分。
aixuer 2004-07-01
  • 打赏
  • 举报
回复
/*
* list.h: the template of list class
*
* copyright (c) 2004 Bjarne.Ying
*
*/


#ifndef LIST_H
#define LIST_H

#include<cstdlib>
#include<cassert>
#include<iostream>


template<class T>
class list
{
public:

// constructor
list();

// copy constructor
list(const list<T>& other);

// overload assignment operator
const list<T> &operator=(const list<T> &other);

// destructor
virtual ~list();

// destory
void destory();

// insert a element
void push_back(const T &element);
void push_front(const T &element);

// get a element
void pop_front(T &box);

// print
void print(std::ostream &os,char c);

// delete a node
void deleteNode(const T &element);

// count
int count()const;

protected:

struct node
{
T data;
node *link;

// constructor of list<T>::node
node(const T &element)
:data(element),link(NULL){}
}
*first,
*last;

int counter;
};

// _____________________________ implementation _______________________________


template<class T>
list<T>::list()
{
first = last = NULL;
counter = 0;
}



template<class T>
void list<T>::destory()
{
node *current = first;

while(first != NULL)
{
current = first;
first = first->link;

delete current;
}

first = last = NULL;
counter = 0;
}



template<class T>
list<T>::~list()
{
destory();
}



template<class T>
void list<T>::push_front(const T& element)
{
node *newNode = new node(element);
assert(newNode);
counter++;

if(!first)
first = last = newNode;
else
{
newNode->link = first;
first = newNode;
}
}



template<class T>
void list<T>::push_back(const T &element)
{
node *newNode = new node(element);
assert(newNode);
counter++;

if(!first)
first = last = newNode;
else
{
last->link = newNode;
last = newNode;
}
}


template<class T>
void list<T>::pop_front(T &box)
{
if(first == NULL)
return;

box = first->data;

node *current = first;
first = first->link;
delete current;
}


template<class T>
list<T>::list(const list<T> &other)
{
if(!other.first)
first = NULL;
else
{
node *current = other.first;
node *newNode = new node(current->data);
assert(newNode);

first = last = newNode;

while(current->link)
{
current = current->link;
newNode = new node(current->data);
assert(newNode);

last->link = newNode;
last = newNode;
}

counter = other.counter;
}
}



template<class T>
const list<T> &list<T>::operator =(const list<T> &other)
{
if(this != &other)
{
destory();

if(other.first == NULL)
return *this;

node *current = other.first;
node *newNode = new node(current->data);
assert(newNode);

first = last = newNode;

while(current->link)
{
current = current->link;
newNode = new node(current->data);
assert(newNode);

last->link = newNode;
last = newNode;
}
counter = other.counter;
}
}



template<class T>
void list<T>::print(std::ostream &os,char c)
{
node *current = first;

for(; current ; current = current->link)
os<< current->data <<c;
}


template<class T>
void list<T>::deleteNode(const T &element)
{
if(first == NULL)
return;
else
{
node *current = first;
node *trail = NULL;
bool found = false;

while(!found && current != NULL)
if(current->data == element)
found = true;
else
{
trail = current;
current = current->link;
}

if(current == NULL)
return;

else if(current == first)
{
first = first->link;
delete current;
}
else
{
trail->link = current->link;
delete current;
}
}
}



template<class T>
int list<T>::count()const
{
return counter;
}

#endif // LIST_H




// queue.hpp: the template of the queue class
// copyright (c) 2004 CA|Ying


#ifndef QUEUE_H
#define QUEUE_H

#include"list.h"


template<class T>
class queue:public list<T>
{
public:
// 插入结点
void insert(const T &element);

};



void queue<T>::insert(const T &element)
{
push_last(element);
}


#endif // QUEUE_H
step_by_step 2004-06-30
  • 打赏
  • 举报
回复
跪求?我鄙视你。不像个男儿。
姜林 2004-06-30
  • 打赏
  • 举报
回复

// Move fence one step left; no change if left is empty
template <class Elem> void LList<Elem>::prev() {
Link<Elem>* temp = head;
if (fence == head) return; // No previous Elem
while (temp->next!=fence) temp=temp->next;
fence = temp;
leftcnt--; rightcnt++;
}

// Set the size of left partition to pos
template <class Elem> bool LList<Elem>::setPos(int pos) {
if ((pos < 0) || (pos > rightcnt+leftcnt)) return false;
fence = head;
for(int i=0; i<pos; i++) fence = fence->next;
return true;
}

template <class Elem> void LList<Elem>::print() const {
Link<Elem>* temp = head;
cout << "< ";
while (temp != fence) {
cout << temp->next->element << " ";
temp = temp->next;
}
cout << "| ";
while (temp->next != NULL) {
cout << temp->next->element << " ";
temp = temp->next;
}
cout << ">\n";
}

// This is the file to include in your code if you want access to the
// complete LQueue template class

// Include the link class
#include "link.h"

// First, get the declaration for the base stack class
#include "queue.h"

// Linked queue implementation
template <class Elem> class LQueue: public Queue<Elem> {
private:
Link<Elem>* front; // Pointer to front queue node
Link<Elem>* rear; // Pointer to rear queue node
int size; // Number of elements in queue
public:
LQueue(int sz =DefaultListSize) // Constructor
{ front = NULL; rear = NULL; size = 0; }
~LQueue() { clear(); } // Destructor
void clear() { // Clear queue
while(front != NULL) { // Delete each link node
rear = front;
front = front->next;
delete rear;
}
rear = NULL;
size = 0;
}
bool enqueue(const Elem& it) {
if (rear == NULL) // Empty queue
front = rear = new Link<Elem>(it, NULL);
else { // Append new node
rear->next = new Link<Elem>(it, NULL);
rear = rear->next;
}
size++;
return true;
}
bool dequeue(Elem& it) { // Remove Elem from front
if (size == 0) return false; // Empty
it = front->element; // Store dequeued value
Link<Elem>* ltemp = front; // Hold dequeued link
front = front->next; // Advance front
delete ltemp; // Delete link
if (front == NULL) rear = NULL; // Dequeued last element
size --;
return true; // Return element value
}
bool frontValue(Elem& it) const {
if (size == 0) return false;
it = front->element;
return true;
}
virtual int length() const { return size; }
};

// This is the file to include in your code if you want access to the
// complete LStack template class

// Include the link class
#include "link.h"

// First, get the declaration for the base stack class
#include "stack.h"

// Link list-based stack implementation
template <class Elem> class LStack: public Stack<Elem> {
private:
Link<Elem>* top; // Pointer to first element
int size; // Count number of elements
public:
LStack(int sz =DefaultListSize) { top = NULL; size = 0; }
~LStack() { clear(); } // Destructor
void clear() {
while (top != NULL) { // Delete link nodes
Link<Elem>* temp = top;
top = top->next;
size = 0;
delete temp;
}
}
bool push(const Elem& item) {
top = new Link<Elem>(item, top);
size++;
return true;
}
bool pop(Elem& it) {
if (size == 0) return false;
it = top->element;
Link<Elem>* ltemp = top->next;
delete top;
top = ltemp;
size--;
return true;
}
bool topValue(Elem& it) const {
if (size == 0) return false;
it = top->element;
return true;
}
int length() const { return size; }
};

你要的都有了,散分吧
姜林 2004-06-30
  • 打赏
  • 举报
回复
// Singly-linked list node
template <class Elem> class Link {
public:
Elem element; // Value for this node
Link *next; // Pointer to next node in list
Link(const Elem& elemval, Link* nextval =NULL)
{ element = elemval; next = nextval; }
Link(Link* nextval =NULL) { next = nextval; }
}

// List abstract class
template <class Elem> class List {
public:
// Reinitialize the list. The client is responsible for
// reclaiming the storage used by the list elements.
virtual void clear() = 0;
// Insert an element at the front of the right partition.
// Return true if successful, false if the list is full.
virtual bool insert(const Elem&) = 0;
// Append an element at the end of the right partition.
// Return true if successful, false if the list is full.
virtual bool append(const Elem&) = 0;
// Remove the first element of right partition. Return
// true if successful, false if right partition is empty.
// The element removed is returned in the parameter.
virtual bool remove(Elem&) = 0;
// Place fence at list start, making left partition empty
virtual void setStart() = 0;
// Place fence at list end, making right partition empty
virtual void setEnd() = 0;
// Move fence one step left; no change if already at start
virtual void prev() = 0;
// Move fence one step right; no change if already at end
virtual void next() = 0;
// Return length of left partition
virtual int leftLength() const = 0;
// Return length of right partition
virtual int rightLength() const = 0;
// If pos or more elements are in the list, set the size
// of left partition to pos and return true. Otherwise,
// do nothing and return false.
virtual bool setPos(int pos) = 0;
// Return in first parameter the first element of the
// right partition. Return true if successful, false
// if the right partition is empty.
virtual bool getValue(Elem&) const = 0;
// Print the contents of the list
virtual void print() const = 0;
};

// This is the file to include in your code if you want access to the
// complete LList template class

// First, get the declaration for the base list class
#include "list.h"

// Linked list implementation
template <class Elem> class LList: public List<Elem> {
private:
Link<Elem>* head; // Pointer to list header
Link<Elem>* tail; // Pointer to last Elem in list
Link<Elem>* fence; // Last element on left side
int leftcnt; // Size of left partition
int rightcnt; // Size of right partition
void init() { // Intialization routine
fence = tail = head = new Link<Elem>;
leftcnt = rightcnt = 0;
}
void removeall() { // Return link nodes to free store
while(head != NULL) {
fence = head;
head = head->next;
delete fence;
}
}
public:
LList(int size=DefaultListSize) { init(); }
~LList() { removeall(); } // Destructor
void clear() { removeall(); init(); }
bool insert(const Elem&);
bool append(const Elem&);
bool remove(Elem&);
void setStart()
{ fence = head; rightcnt += leftcnt; leftcnt = 0; }
void setEnd()
{ fence = tail; leftcnt += rightcnt; rightcnt = 0; }
void prev();
void next() {
if (fence != tail) // Don't move fence if right empty
{ fence = fence->next; rightcnt--; leftcnt++; }
}
int leftLength() const { return leftcnt; }
int rightLength() const { return rightcnt; }
bool setPos(int pos);
bool getValue(Elem& it) const {
if(rightLength() == 0) return false;
it = fence->next->element;
return true;
}
void print() const;
};

template <class Elem> // Insert at front of right partition
bool LList<Elem>::insert(const Elem& item) {
fence->next = new Link<Elem>(item, fence->next);
if (tail == fence) tail = fence->next; // New tail
rightcnt++;
return true;
}

template <class Elem> // Append Elem to end of the list
bool LList<Elem>::append(const Elem& item) {
tail = tail->next = new Link<Elem>(item, NULL);
rightcnt++;
return true;
}

// Remove and return first Elem in right partition
template <class Elem> bool LList<Elem>::remove(Elem& it) {
if (fence->next == NULL) return false; // Empty right
it = fence->next->element; // Remember value
Link<Elem>* ltemp = fence->next; // Remember link node
fence->next = ltemp->next; // Remove from list
if (tail == ltemp) tail = fence; // Reset tail
delete ltemp; // Reclaim space
rightcnt--;
return true;
}

64,646

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧