需要链表考试题的答案 [英] Need answer for linked list exam question
本文介绍了需要链表考试题的答案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是完整程序中的示例链接列表之一,但我仍然无法解决,希望那里的任何人都可以帮助我
这个任务只需要在"//"符号上回答,希望对我有帮助,这就像一个问题会在我的考试中出现,这个问题是班级分类中的链表之一.
this the one of example linked list in full program, but i still cant solve it, hope any people there can help me
this quest wee need to answer on "//" sign only, hope please help me, this like one question it will come out in my exam, this question one of linked list in class classified.
#ifndef H_LinkedListType
#define H_LinkedListType
#include <iostream>
#include <cassert>
using namespace std;
//Definition of the node
template <class type="">
struct nodeType {
Type info;
nodeType<type> *link;
};
template <class type="">
class linkedListIterator {
public:
linkedListIterator();
//Default constructor
//Postcondition: current = NULL;
linkedListIterator(nodeType<type> *ptr);
//Constructor with a parameter.
//Postcondition: current = ptr;
Type operator*();
//Function to overload the dereferencing operator *.
//Postcondition: Returns the info contained in the node.
linkedListIterator<type> operator++();
//Overload the pre-increment operator.
//Postcondition: The iterator is advanced to the next
// node.
bool operator==(const linkedListIterator<type>& right) const;
//Overload the equality operator.
//Postcondition: Returns true if this iterator is equal to
// the iterator specified by right,
// otherwise it returns the value false.
bool operator!=(const linkedListIterator<type>& right) const;
//Overload the not equal to operator.
//Postcondition: Returns true if this iterator is not
// equal to the iterator specified by
// right; otherwise it returns the value
// false.
private:
nodeType<type> *current; //pointer to point to the current
//node in the linked list
};
template <class type="">
linkedListIterator<type>::linkedListIterator() {
current = NULL;
}
template <class type="">
linkedListIterator<type>::
linkedListIterator(nodeType<type> *ptr) {
current = ptr;
}
template <class type="">
Type linkedListIterator<type>::operator*() {
return current->info;
}
template <class type="">
linkedListIterator<type> linkedListIterator<type>::operator++() {
current = current->link;
return *this;
}
template <class type="">
bool linkedListIterator<type>::operator==
(const linkedListIterator<type>& right) const {
return (current == right.current);
}
template <class type="">
bool linkedListIterator<type>::operator!=
(const linkedListIterator<type>& right) const {
return (current != right.current);
}
//***************** class linkedListType ****************
template <class type="">
class linkedListType {
public:
const linkedListType<type>& operator=
(const linkedListType<type>&);
//Overload the assignment operator.
void initializeList();
//Initialize the list to an empty state.
//Postcondition: first = NULL, last = NULL, count = 0;
bool isEmptyList() const;
//Function to determine whether the list is empty.
//Postcondition: Returns true if the list is empty,
// otherwise it returns false.
void print() const;
//Function to output the data contained in each node.
//Postcondition: none
int length() const;
//Function to return the number of nodes in the list.
//Postcondition: The value of count is returned.
void destroyList();
//Function to delete all the nodes from the list.
//Postcondition: first = NULL, last = NULL, count = 0;
Type front() const;
//Function to return the first element of the list.
//Precondition: The list must exist and must not be
// empty.
//Postcondition: If the list is empty, the program
// terminates; otherwise, the first
// element of the list is returned.
Type back() const;
//Function to return the last element of the list.
//Precondition: The list must exist and must not be
// empty.
//Postcondition: If the list is empty, the program
// terminates; otherwise, the last
// element of the list is returned.
virtual bool search(const Type& searchItem) const = 0;
//Function to determine whether searchItem is in the list.
//Postcondition: Returns true if searchItem is in the
// list, otherwise the value false is
// returned.
virtual void insertFirst(const Type& newItem) = 0;
//Function to insert newItem at the beginning of the list.
//Postcondition: first points to the new list, newItem is
// inserted at the beginning of the list,
// last points to the last node in the list,
// and count is incremented by 1.
virtual void insertLast(const Type& newItem) = 0;
//Function to insert newItem at the end of the list.
//Postcondition: first points to the new list, newItem
// is inserted at the end of the list,
// last points to the last node in the list,
// and count is incremented by 1.
virtual void deleteNode(const Type& deleteItem) = 0;
//Function to delete deleteItem from the list.
//Postcondition: If found, the node containing
// deleteItem is deleted from the list.
// first points to the first node, last
// points to the last node of the updated
// list, and count is decremented by 1.
linkedListIterator<type> begin();
//Function to return an iterator at the begining of the
//linked list.
//Postcondition: Returns an iterator such that current is
// set to first.
linkedListIterator<type> end();
//Function to return an iterator one element past the
//last element of the linked list.
//Postcondition: Returns an iterator such that current is
// set to NULL.
linkedListType();
//default constructor
//Initializes the list to an empty state.
//Postcondition: first = NULL, last = NULL, count = 0;
linkedListType(const linkedListType<type>& otherList);
//copy constructor
~linkedListType();
//destructor
//Deletes all the nodes from the list.
//Postcondition: The list object is destroyed.
void reversePrint() const;
protected:
int count; //variable to store the number of
//elements in the list
nodeType<type> *first; //pointer to the first node of the list
nodeType<type> *last; //pointer to the last node of the list
private:
void copyList(const linkedListType<type>& otherList);
//Function to make a copy of otherList.
//Postcondition: A copy of otherList is created and
// assigned to this list.
void recursiveReversePrint(nodeType<type> *current) const;
};
template <class type="">
bool linkedListType<type>::isEmptyList() const {
return(first == NULL);
}
template <class type="">
linkedListType<type>::linkedListType() { //default constructor
first = NULL;
last = NULL;
count = 0;
}
template <class type="">
void linkedListType<type>::destroyList() {
nodeType<type> *temp; //pointer to deallocate the memory
//occupied by the node
while (first != NULL) { //while there are nodes in the list
temp = first; //set temp to the current node
first = first->link; //advance first to the next node
delete temp; //deallocate the memory occupied by temp
}
last = NULL; //initialize last to NULL; first has already
//been set to NULL by the while loop
count = 0;
}
template <class type="">
void linkedListType<type>::initializeList() {
destroyList(); //if the list has any nodes, delete them
}
template <class type="">
void linkedListType<type>::print() const {
nodeType<type> *current; //pointer to traverse the list
current = first; //set current so that it points to
//the first node
while (current != NULL) { //while more data to print
cout << current->info << " ";
current = current->link;
}
}//end print
template <class type="">
int linkedListType<type>::length() const {
return count;
} //end length
template <class type="">
Type linkedListType<type>::front() const {
assert(first != NULL);
return first->info; //return the info of the first node
}//end front
template <class type="">
Type linkedListType<type>::back() const {
assert(last != NULL);
return last->info; //return the info of the last node
}//end back
template <class type="">
linkedListIterator<type> linkedListType<type>::begin() {
linkedListIterator<type> temp(first);
return temp;
}
template <class type="">
linkedListIterator<type> linkedListType<type>::end() {
linkedListIterator<type> temp(NULL);
return temp;
}
template <class type="">
void linkedListType<type>::copyList
(const linkedListType<type>& otherList) {
nodeType<type> *newNode; //pointer to create a node
nodeType<type> *current; //pointer to traverse the list
if (first != NULL) //if the list is nonempty, make it empty
destroyList();
if (otherList.first == NULL) { //otherList is empty
first = NULL;
last = NULL;
count = 0;
} else {
current = otherList.first; //current points to the
//list to be copied
count = otherList.count;
//copy the first node
first = new nodeType<type>; //create the node
first->info = current->info; //copy the info
first->link = NULL; //set the link field of
//the node to NULL
last = first; //make last point to the
//first node
current = current->link; //make current point to
//the next node
//copy the remaining list
while (current != NULL) {
newNode = new nodeType<type>; //create a node
newNode->info = current->info; //copy the info
newNode->link = NULL; //set the link of
//newNode to NULL
last->link = newNode; //attach newNode after last
last = newNode; //make last point to
//the actual last node
current = current->link; //make current point
//to the next node
}//end while
}//end else
}//end copyList
template <class type="">
linkedListType<type>::~linkedListType() { //destructor
destroyList();
}//end destructor
template <class type="">
linkedListType<type>::linkedListType
(const linkedListType<type>& otherList) {
first = NULL;
copyList(otherList);
}//end copy constructor
//overload the assignment operator
template <class type="">
const linkedListType<type>& linkedListType<type>::operator=
(const linkedListType<type>& otherList) {
if (this != &otherList) { //avoid self-copy
copyList(otherList);
}//end else
return *this;
}
#endif
推荐答案
您的问题有两个问题:
1.显然是功课.为什么不尝试自己解决呢?谁知道,你可能会学到一些东西.
2.这是难以理解的.我不知道你在问什么.显然是关于"//"的事情?
Your question has two problems:
1. It is obviously homework. Why not try to solve it for yourself? Who knows, you might learn something.
2. It is unintelligible. I haven''t a clue what you''re asking; apparently something about "//"???
好的,谢谢您的浏览,谢谢您的建议,也许在这个网站上没有人可以帮我,是我的家庭作业,我已经尝试解决它,但是我仍然找不到如何启动它的线索,在这个家庭作业中,lec给了我这个问题,我已经填写了"//"(注释),这个关于链表的问题在数据结构中,ni不知道从哪里开始. cos我也没有输出的打印屏幕,我在这个任务中遇到的问题n不知道如何启动它.
it ok thanks for ur view all, thanks for ur suggestion, maybe on this site no one can help me, yap this my home work, n i have try to solve it, but i still cant find the clue how to start it, in this home work, the lec give me this question, n i have fill up at "//"(comment), n this question about linked list in data structure, n i dont know from where i need to start it. cos i also dont have the print screen of the output, the way i stuck in this quest n dont know how to start it.
对于此代码,我已经提出了问题想要它,此问题的说明,向后打印单个链表.包括功能reversePrint和recursiveReverseprint,还编写程序功能以向后打印(单个)链表.
这个输出
输入以-999结尾的数字
2 23 4 5 6 7 45 33 2 11 -999
清单:2 23 4 5 6 7 45 33 2 11
以相反的顺序列出:
清单:11 2 33 45 7 6 5 4 23
或任何人都可以根据输出结果帮助我找到c ++中链表的简单代码.
for this code i have get the out put waht the question want it, this the instruction of that question, Printing a single linked list backward. Include the functions reversePrint and recursiveReverseprint,Also write a program function to print a (single) linked list backward.
This the out put
Enter numbers ending with -999
2 23 4 5 6 7 45 33 2 11 -999
List: 2 23 4 5 6 7 45 33 2 11
list in reverse order:
List: 11 2 33 45 7 6 5 4 23
or any people can help me to find the simple code for linked list in c++ based on that output.
这篇关于需要链表考试题的答案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文