插入功能我一直在指針與坦然C現在++掙扎了幾個星期。我有一個放棄的學校項目(截止日期已過)。即使我無法開啓,我仍然希望完成它練習,我堅持我清晰的方法,但可能有其他方法我應該做一些這樣的。鏈表清晰,C++
.h文件中
using namespace std;
template < typename T > // Forward declaration of the SimpList class
class SimpList;
//--------------------------------------------------------------------
//
// Definition of class Node<T>
//
//--------------------------------------------------------------------
template < typename T >
class Node // Node class for the SimpList class
{
private:
// Constructors
Node() { next = 0; } // default constructor
// Complete the definition inline
Node (const T &initItem, Node<T> *ptr) {
value=initItem;
next=ptr;
}
// Data members
T value; // Node data item
Node *next; // Pointer to the next node
friend class SimpList<T>;
};
//--------------------------------------------------------------------
//
// Definition of class SimpList<T>
//
//--------------------------------------------------------------------
template < typename T >
class SimpList
{
public:
// Constructor (add your code inline)
SimpList()
{
head = &PHONY;
length=0;
// complete the data member intialization
}
// Destructor (add your code inline)
~SimpList() { clear();}
// List manipulation operations
void insert (const T &newitem); // insert a data item
bool remove (T &item); // remove data item
bool find (T &item) const; // find data item
void clear(); // empty the list
// (add your code inline)
bool isEmpty() const {
if (length == 0)
{
return true;
}
else
{
return false;
}
}
// length accessor method (add your code inline)
int size() const { return length; }
// print the list items
void print() const;
private: // data members
Node<T> PHONY ; // empty node that anchors the list
Node<T> *head; // pointer to the beginning of the list
int length; // length of list
};
//--------------------------------------------------------------------
//
// Implementation section
//
//--------------------------------------------------------------------
template < typename T >
void SimpList<T>::print() const
{
if (length == 0)
{
cout << "List is empty." << endl;
return;
}
Node<T> *ptr=head;
while (ptr != NULL)
{
cout << ptr->value << " ";
ptr = ptr->next;
}
cout << endl;
}
template <typename T>
void SimpList<T>::insert (const T &newitem)
{
Node<T> *p = new Node<T>;
p->value = newitem;
p->next = head;
head = p;
length++;
}
template <typename T>
void SimpList<T>::clear()
{
Node<T> *p = head->next;
while(p != NULL)
{
head=head->next;
delete p;
}
length = 0;
}
template <typename T>
bool SimpList<T>::find (T &item) const
{
Node<T> *p;
for(p=head; p!=NULL; p=p->next)
if(p->value==item) break;
if(p!=NULL)
{
item=p->value;
return true;
}
else
{
return false;
}
}
template <typename T>
bool SimpList<T>::remove (T &item)
{
Node<T> *p,*c;
for(c=head; c!=NULL;p=c,c=c->next)
if(c->value==item) break;
if(c!=NULL)
{
item=c->value;
p->next=c->next;
delete c;
length --;
for(c=head;c!=NULL;c=c->next)
cout<<c->value<<" ";
return true;
}
else
return false;
}
.cpp文件是在這裏。
#include <iostream>
#include <string>
#include "simpList.h"
int main()
{
SimpList<int> intList; // (empty) list of integers
cout << "Let's build a sorted list of integers." << endl;
cout << endl << "Uninitialized List: ";
intList.print();
cout << endl << "Length: " << intList.size() << endl;
int intData[] = { 5, 3, -2, 7, 9, -8, 1, -4 };
for (int i=0; i<8; i++){
intList.insert(intData[i]);
}
cout << endl << "After inserting 8 integers: ";
intList.print();
cout << endl << "Length: " << intList.size() << endl;
cout << endl << "--- Testing the \"find\" method:" << endl;
int t = 5;
bool ret = intList.find(t);
cout << endl << t << " is in the list: "
<< (ret ? "true" : "false") << endl;
t = 6;
ret = intList.find(t);
cout << endl << t << " is in the list: "
<< (ret ? "true" : "false") << endl;
cout << endl << "--- Testing the \"remove\" method:" << endl;
t = 5;
ret = intList.remove(t);
cout << endl << t << " has been removed: "
<< (ret ? "true" : "false") << endl;
cout << endl << "Remaining list: ";
intList.print();
cout << endl << "Length: " << intList.size() << endl;
cout << endl << "--- Testing the \"clear\" method:" << endl << endl;
intList.clear();
intList.print();
cout << endl << "Length: " << intList.size() << endl;
cout << endl << "--- Testing the \"isEmpty\" predicate:" << endl;
cout << endl << "The integer list is now empty: "
<< (intList.isEmpty()? "true" : "false") << endl << endl;
cout << "Now, let's build a sorted list of strings." << endl;
string strData[] = { "Maria", "Ann", "Emily", "Vivian",
"Beth", "Carla", "Susan" };
SimpList<string> strList;
for (int i=0; i<7; i++){
strList.insert(strData[i]);
}
cout << endl << "After inserting 7 names:" << endl;
strList.print();
cout << endl << "Length: " << strList.size() << endl;
cout << endl << "--- Testing the \"remove\" method:" << endl;
cout << endl << "Bye Carla!" << endl;
string str = "Carla";
ret = strList.remove(str);
cout << endl << str << " has been removed: "
<< (ret ? "true" : "false") << endl;
cout << endl << "Remaining list:" << endl;
strList.print();
cout << endl << "Length: " << strList.size() << endl;
cout << endl << "--- Testing the \"insert\" method:" << endl;
cout << endl << "Bienvenue Pauline!" << endl;
string nom = "Pauline";
strList.insert(nom);
cout << endl << "Extended list:" << endl;
strList.print();
cout << endl << "Length: " << strList.size() << endl;
cout << endl << "--- Auf Wiedersehen!" << endl;`enter code here`
cout << endl << "End of \"main()\"." << endl
<< "At this point, the destructor is called." << endl;
system("PAUSE");}
固定
當打印出在列表中的項目,它打印的所有項目,以及-858993460,我已經用盡了我的如何解決它的知識。
我將非常感謝您的傢伙/ gals幫助或指向正確的方向。
你從來沒有真正改變'p'(在'清除()'),它的初始設置後,但你多次可能將其刪除... – John3136
這個問題似乎是題外話,因爲它是關於這樣濫用一個在線人羣調試服務。 **提到OP:**你有任何懸掛指針嗎?你有沒有調試過,並檢查你的指針值是否正確設置每個操作。先檢查一下,然後回來問一個你不明白的問題! –
打印中的隨機值是列表中的'PHONY'節點。 –