协助链接列表 [英] Assistance with linked lists

查看:47
本文介绍了协助链接列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了一些代码来接受输入并将此输入放在

列表的开头或结尾,但我需要帮助包括一个类,以便

it将允许输入格式正确的电话号码。我会在下面发布

我的代码。在此先感谢。


这是链表的代码。它使用int'和string'进行测试。


#ifndef LIST_H

#define LIST_H


#include< iostream>

使用std :: cout;


#include" Listnode.h"


模板< typename NODETYPE>

class List

{

public:

List();

~List();

void insertAtFront(const NODETYPE&);

void insertAtBack(const NODETYPE&);

bool removeFromFront(NODETYPE&);

bool removeFromBack(NODETYPE&);

bool isEmpty()const;

void print() const;

private:

ListNode< NODETYPE * firstPtr;

ListNode< NODETYPE * lastPtr;


//分配新节点的效用函数

ListNode< NODETYPE * getNewNode(const NODETYPE&);

}; //结束类列表


//默认构造函数

模板< typename NODETYPE>

列表< NODETYPE> :: List()

:firstPtr(0),lastPtr(0)

{

}


//析构函数

模板< typename NODETYPE>

列表< NODETYPE> ::〜List()

{

if(!isEmpty())//列表不为空

{

cout<< 销毁节点...... \ n;


ListNode< NODETYPE * currentPtr = firstPtr;

ListNode< NODETYPE * tempPtr;


而(currentPtr!= 0)//删除剩余的节点

{

tempPtr = currentPtr;

cout<< tempPtr-> data<< ''\ n'';

currentPtr = currentPtr-> nextPtr;

删除tempPtr;

} //结束时间

} //结束如果


cout<< 所有节点被破坏了\ n \ nn;

} //结束列表析构函数


//在列表前插入节点

模板< typename NODETYPE>

void List< NODETYPE> :: insertAtFront(const NODETYPE& value)

{

ListNode< NODETYPE * newPtr = getNewNode(value); //新节点


if(isEmpty())//列表为空

firstPtr = lastPtr = newPtr; //新列表只有一个节点

其他//列表不为空

{

newPtr-> nextPtr = firstPtr; //将新节点指向前一个节点

firstPtr = newPtr; //将firstPtr指向新节点

} //结束其他

} //结束函数insertAtFront


//插入节点在列表的后面

模板< typename NODETYPE>

void List< NODETYPE> :: insertAtBack(const NODETYPE& value)

{

ListNode< NODETYPE * newPtr = getNewNode(value); //新节点


if(isEmpty())//列表为空

firstPtr = lastPtr = newPtr; //新列表只有一个节点

其他//列表不为空

{

lastPtr-> nextPtr = newPtr; //更新上一个节点

lastPtr = newPtr; //新的最后一个节点

} //结束其他

} //结束函数insertAtBack


//从前面删除节点列表

模板< typename NODETYPE>

bool List< NODETYPE> :: removeFromFront(NODETYPE& value)

{

if(isEmpty())//列表为空

返回false ; //删除不成功

else

{

ListNode< NODETYPE * tempPtr = firstPtr; //保持tempPtr删除


if(firstPtr == lastPtr)

firstPtr = lastPtr = 0; //删除后没有剩余节点

else

firstPtr = firstPtr-> nextPtr; //指向前一个第二个节点


value = tempPtr-> data; //返回要删除的数据

delete tempPtr; //回收前一个节点

返回true; //删除成功

} //结束其他

} //结束函数removeFromFront


//从后面删除节点list

template< typename NODETYPE>

bool List< NODETYPE> :: removeFromBack(NODETYPE& value)

{

if(isEmpty())//列表为空

返回false ; //删除不成功

else

{

ListNode< NODETYPE * tempPtr = lastPtr; //保持tempPtr删除


if(firstPtr == lastPtr)//列表有一个元素

firstPtr = lastPtr = 0; //删除后没有剩余节点

else

{

ListNode< NODETYPE * currentPtr = firstPtr;


//找到倒数第二个元素

while(currentPtr-> nextPtr!= lastPtr)

currentPtr = currentPtr-> nextPtr; //移动到下一个节点

lastPtr = currentPtr; //删除最后一个节点

currentPtr-> nextPtr = 0; //这是现在的最后一个节点

} //结束其他


value = tempPtr-> data; //从旧的最后一个节点返回值

delete tempPtr; //回收前一个节点

返回true; //删除成功

} //结束其他

} //结束函数removeFromBack


//列表为空吗? br />
模板< typename NODETYPE>

bool List< NODETYPE> :: isEmpty()const

{

返回firstPtr == 0;

} //结束函数isEmpty


//返回指向新分配节点的指针

template< typename NODETYPE>

ListNode< NODETYPE *列表< NODETYPE> :: getNewNode(

const NODETYPE& value)

{

返回新的ListNode< NODETYPE>(值);

} //结束函数getNewNode


//显示List的内容

template< typename NODETYPE>

void List< NODETYPE> :: print()const

{

if(isEmpty())//列表为空

{

cout<< 列表是空的\ n \ nn;

返回;

} //结束如果


ListNode< ; NODETYPE * currentPtr = firstPtr;


cout<< 列表是:" ;;


而(currentPtr!= 0)//获取元素数据

{

cout<< currentPtr-> data<< '''';

currentPtr = currentPtr-> nextPtr;

} //结束时


cout<< ; \ n \ nn;

} //结束功能打印


#endif


#ifndef LISTNODE_H

#define LISTNODE_H

template< typename NODETYPE类列表;


模板< typename NODETYPE>

class ListNode

{

朋友类列表< NODETYPE> ;;


public:

ListNode(const NODETYPE&);

NODETYPE getData()const;

private:

NODETYPE数据;

ListNode< NODETYPE * nextPtr; //列表中的下一个节点

}; //结束类ListNode


//构造函数

模板< typename NODETYPE>

ListNode< NODETYPE> :: ListNode(const NODETYPE& info)

:data(info),nextPtr(0)

{

} / / end ListNode构造函数


//返回节点中的数据副本

template< typename NODETYPE>

NODETYPE ListNode< NODETYPE> :: getData()const

{

返回数据;

} //结束函数getData

#endif


#include< iostream>

使用std :: cin;

使用std: :cout;

使用std :: endl;


#include< string>

使用std :: string;


#include" List.h"


//测试列表的功能

模板< typename T>

void testList(List< T& listObject,const string& typeName)

{

cout<< 测试列表 << typeName<< " values \ n" ;;

instructions(); //显示说明


int choice; //商店用户选择

T值; //存储输入值


执行//执行用户选择的操作

{

cout<< " ;? " ;;

cin>选择;


开关(选择)

{

case 1://开头插入

cout<< 输入 << typeName<< ":";

cin> value;

listObject.insertAtFront(value);

listObject.print();

休息;

案例2://在结尾插入

cout<< 输入 << typeName<< " ;:" ;;

cin> value;

listObject.insertAtBack(value);

listObject.print();

休息;

案例3://从开头删除

if(listObject.removeFromFront(value))

cout <<值<< "从列表中删除\ n" ;;


listObject.print();

break;

case 4://删除从结束

if(listObject.removeFromBack(value))

cout<<值<< "从列表中删除\ n" ;;


listObject.print();

break;

} //结束开关

} while(选择!= 5); //结束做...而


cout<< 结束列表test\\\
\ n;;

} //结束函数testList

无效指令()

{

cout<< 输入以下内容之一:\ n"

<< " 1在列表的开头插入\ n"

<< " 2在列表末尾插入\ n"

<< " 3从列表开头删除\ n"

<< " 4从列表末尾删除\ n"

<< " 5到结束列表处理\ n" ;;

} //结束函数指令


int main()

{

//测试int值列表

列表< int intList;

testList(intList," integer");


// test字符串值列表

List< string stringList;

testList(stringList," string");


返回0;

} //结束主


我希望能够包含我的电话号码类,这样就可以接受

电话号码作为输入。这是电话课


#ifndef PHONENUMBER_H

#define PHONENUMBER_H


#include< iostream>

使用std :: ostream;

使用std :: istream;


#include< string>

使用std :: string;


#include< iomanip>

使用std :: setw;


class PhoneNumber

{

朋友ostream& operator<<(ostream&,const PhoneNumber&);

朋友istream& operator>>(istream&,PhoneNumber&);

private:

string areaCode; // 3位数区号

字符串交换; // 3位数交换

string line; // 4位数线

}; //结束类PhoneNumber


ostream& operator<<(ostream& output,const PhoneNumber& number)

{

输出<< "("<<< number.areaCode<<")"

<< number.exchange<< " - " << number.line;

返回输出; //启用cout<< a<< b<< c;

} //结束函数运算符<<


istream& operator>>(istream& input,PhoneNumber& number)

{

input.ignore(); //跳过(

输入> setw(3)> number.areaCode; //输入区域代码

input.ignore(2); // skip)和空间

输入> setw(3)> number.exchange; //输入交换

input.ignore(); // skip dash( - )

input> setw(4)> number.line; //输入行

返回输入; //启用cin> a> b> c;

} //结束函数运算符>>


#endif

解决方案

* B. Williams:


我需要帮助,包括课程以便

它将允许输入格式正确的电话号码。



尝试更具体一点。你想让别人为你写代码

吗?


-

答:因为它弄乱了订单人们通常会阅读文字。

问:为什么这么糟糕?

A:热门帖子。

问:什么是usenet和电子邮件中最烦人的事情是?




" Alf P. Steinbach" < al *** @ start.nowrote in message

news:4t ************* @ mid.individual.net ...
< blockquote class =post_quotes>
> * B. Williams:


>我需要帮助,包括一个类,以便它允许输入一个
格式正确的电话号码。



尝试更具体一点。你想让别人为你写代码

吗?


-

答:因为它弄乱了订单人们通常会阅读文字。

问:为什么这么糟糕?

A:热门帖子。

问:什么是usenet和电子邮件中最烦人的事情是什么?



我为不具体而道歉。我知道这两项工作都是独立的,但我需要帮助将电话号码等级加入到程序中。我无法找到更好的方法来获得该计划

接受电话号码。我尝试输入电话号码作为字符串,但是空白空格结束字符串

。我想我在写代码时要求帮助
,但我只是需要一个启动。这是测试

电话号码类的代码。


#include< iostream>

使用std :: cout ;

使用std :: cin;

使用std :: endl;


#include" PhoneNumber.h"


int main()

{

PhoneNumber手机;


cout< < 在表格(123)456-7890中输入电话号码: <<结束;


cin>电话;


cout<< 输入的电话号码是:" ;;


cout<<电话<< endl;

返回0;

} //结束主。


B. Williams< wi ******* @ hotmail.comwrote:


我尝试输入电话号码作为字符串,但是

空格结束字符串。



尝试使用std :: getline()代替。


-

Marcus Kwok

将''invalid''替换为''net''以回复


I have written some code to accept input and place this input at the
beginning or end of a list, but I need assistance including a class so that
it will allow input of a phone number that is properly formatted. I''ll post
my code below. Thanks in advance.

This is the code for the linked list. It tests using int''s and string''s.

#ifndef LIST_H
#define LIST_H

#include <iostream>
using std::cout;

#include "Listnode.h"

template< typename NODETYPE >
class List
{
public:
List();
~List();
void insertAtFront( const NODETYPE & );
void insertAtBack( const NODETYPE & );
bool removeFromFront( NODETYPE & );
bool removeFromBack( NODETYPE & );
bool isEmpty() const;
void print() const;
private:
ListNode< NODETYPE *firstPtr;
ListNode< NODETYPE *lastPtr;

// utility function to allocate new node
ListNode< NODETYPE *getNewNode( const NODETYPE & );
}; // end class List

// default constructor
template< typename NODETYPE >
List< NODETYPE >::List()
: firstPtr( 0 ), lastPtr( 0 )
{
}

// destructor
template< typename NODETYPE >
List< NODETYPE >::~List()
{
if ( !isEmpty() ) // List is not empty
{
cout << "Destroying nodes ...\n";

ListNode< NODETYPE *currentPtr = firstPtr;
ListNode< NODETYPE *tempPtr;

while ( currentPtr != 0 ) // delete remaining nodes
{
tempPtr = currentPtr;
cout << tempPtr->data << ''\n'';
currentPtr = currentPtr->nextPtr;
delete tempPtr;
} // end while
} // end if

cout << "All nodes destroyed\n\n";
} // end List destructor

// insert node at front of list
template< typename NODETYPE >
void List< NODETYPE >::insertAtFront( const NODETYPE &value )
{
ListNode< NODETYPE *newPtr = getNewNode( value ); // new node

if ( isEmpty() ) // List is empty
firstPtr = lastPtr = newPtr; // new list has only one node
else // List is not empty
{
newPtr->nextPtr = firstPtr; // point new node to previous 1st node
firstPtr = newPtr; // aim firstPtr at new node
} // end else
} // end function insertAtFront

// insert node at back of list
template< typename NODETYPE >
void List< NODETYPE >::insertAtBack( const NODETYPE &value )
{
ListNode< NODETYPE *newPtr = getNewNode( value ); // new node

if ( isEmpty() ) // List is empty
firstPtr = lastPtr = newPtr; // new list has only one node
else // List is not empty
{
lastPtr->nextPtr = newPtr; // update previous last node
lastPtr = newPtr; // new last node
} // end else
} // end function insertAtBack

// delete node from front of list
template< typename NODETYPE >
bool List< NODETYPE >::removeFromFront( NODETYPE &value )
{
if ( isEmpty() ) // List is empty
return false; // delete unsuccessful
else
{
ListNode< NODETYPE *tempPtr = firstPtr; // hold tempPtr to delete

if ( firstPtr == lastPtr )
firstPtr = lastPtr = 0; // no nodes remain after removal
else
firstPtr = firstPtr->nextPtr; // point to previous 2nd node

value = tempPtr->data; // return data being removed
delete tempPtr; // reclaim previous front node
return true; // delete successful
} // end else
} // end function removeFromFront

// delete node from back of list
template< typename NODETYPE >
bool List< NODETYPE >::removeFromBack( NODETYPE &value )
{
if ( isEmpty() ) // List is empty
return false; // delete unsuccessful
else
{
ListNode< NODETYPE *tempPtr = lastPtr; // hold tempPtr to delete

if ( firstPtr == lastPtr ) // List has one element
firstPtr = lastPtr = 0; // no nodes remain after removal
else
{
ListNode< NODETYPE *currentPtr = firstPtr;

// locate second-to-last element
while ( currentPtr->nextPtr != lastPtr )
currentPtr = currentPtr->nextPtr; // move to next node

lastPtr = currentPtr; // remove last node
currentPtr->nextPtr = 0; // this is now the last node
} // end else

value = tempPtr->data; // return value from old last node
delete tempPtr; // reclaim former last node
return true; // delete successful
} // end else
} // end function removeFromBack

// is List empty?
template< typename NODETYPE >
bool List< NODETYPE >::isEmpty() const
{
return firstPtr == 0;
} // end function isEmpty

// return pointer to newly allocated node
template< typename NODETYPE >
ListNode< NODETYPE *List< NODETYPE >::getNewNode(
const NODETYPE &value )
{
return new ListNode< NODETYPE >( value );
} // end function getNewNode

// display contents of List
template< typename NODETYPE >
void List< NODETYPE >::print() const
{
if ( isEmpty() ) // List is empty
{
cout << "The list is empty\n\n";
return;
} // end if

ListNode< NODETYPE *currentPtr = firstPtr;

cout << "The list is: ";

while ( currentPtr != 0 ) // get element data
{
cout << currentPtr->data << '' '';
currentPtr = currentPtr->nextPtr;
} // end while

cout << "\n\n";
} // end function print

#endif

#ifndef LISTNODE_H
#define LISTNODE_H
template< typename NODETYPE class List;

template< typename NODETYPE >
class ListNode
{
friend class List< NODETYPE >;

public:
ListNode( const NODETYPE & );
NODETYPE getData() const;
private:
NODETYPE data;
ListNode< NODETYPE *nextPtr; // next node in list
}; // end class ListNode

// constructor
template< typename NODETYPE >
ListNode< NODETYPE >::ListNode( const NODETYPE &info )
: data( info ), nextPtr( 0 )
{
} // end ListNode constructor

// return copy of data in node
template< typename NODETYPE >
NODETYPE ListNode< NODETYPE >::getData() const
{
return data;
} // end function getData

#endif

#include <iostream>
using std::cin;
using std::cout;
using std::endl;

#include <string>
using std::string;

#include "List.h"

// function to test a List
template< typename T >
void testList( List< T &listObject, const string &typeName )
{
cout << "Testing a List of " << typeName << " values\n";
instructions(); // display instructions

int choice; // store user choice
T value; // store input value

do // perform user-selected actions
{
cout << "? ";
cin >choice;

switch ( choice )
{
case 1: // insert at beginning
cout << "Enter " << typeName << ": ";
cin >value;
listObject.insertAtFront( value );
listObject.print();
break;
case 2: // insert at end
cout << "Enter " << typeName << ": ";
cin >value;
listObject.insertAtBack( value );
listObject.print();
break;
case 3: // remove from beginning
if ( listObject.removeFromFront( value ) )
cout << value << " removed from list\n";

listObject.print();
break;
case 4: // remove from end
if ( listObject.removeFromBack( value ) )
cout << value << " removed from list\n";

listObject.print();
break;
} // end switch
} while ( choice != 5 ); // end do...while

cout << "End list test\n\n";
} // end function testList
void instructions()
{
cout << "Enter one of the following:\n"
<< " 1 to insert at beginning of list\n"
<< " 2 to insert at end of list\n"
<< " 3 to delete from beginning of list\n"
<< " 4 to delete from end of list\n"
<< " 5 to end list processing\n";
} // end function instructions

int main()
{
// test List of int values
List< int intList;
testList( intList, "integer" );

// test List of string values
List< string stringList;
testList( stringList, "string" );

return 0;
} // end main

I want to be able to include my phone number class so that this will accept
phone numbers as input. Here is the phone class

#ifndef PHONENUMBER_H
#define PHONENUMBER_H

#include <iostream>
using std::ostream;
using std::istream;

#include <string>
using std::string;

#include <iomanip>
using std::setw;

class PhoneNumber
{
friend ostream &operator<<( ostream &, const PhoneNumber & );
friend istream &operator>>( istream &, PhoneNumber & );
private:
string areaCode; // 3-digit area code
string exchange; // 3-digit exchange
string line; // 4-digit line
}; // end class PhoneNumber

ostream &operator<<( ostream &output, const PhoneNumber &number )
{
output << "(" << number.areaCode << ") "
<< number.exchange << "-" << number.line;
return output; // enables cout << a << b << c;
} // end function operator<<

istream &operator>>( istream &input, PhoneNumber &number )
{
input.ignore(); // skip (
input >setw( 3 ) >number.areaCode; // input area code
input.ignore( 2 ); // skip ) and space
input >setw( 3 ) >number.exchange; // input exchange
input.ignore(); // skip dash (-)
input >setw( 4 ) >number.line; // input line
return input; // enables cin >a >b >c;
} // end function operator>>

#endif

解决方案

* B. Williams:

I need assistance including a class so that
it will allow input of a phone number that is properly formatted.

Try to be a little more specific. Do you want someone to write the code
for you?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?



"Alf P. Steinbach" <al***@start.nowrote in message
news:4t*************@mid.individual.net...

>* B. Williams:

>I need assistance including a class so that it will allow input of a
phone number that is properly formatted.


Try to be a little more specific. Do you want someone to write the code
for you?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

I apologize for not being more specific. I know both of these work
independently, but I need help with incoporating the phone number class into
the program. I couldn''t figure out any better way to get the program to
accept phone numbers. I tried inputting the phone number as a string, but
the blank space ends the string. I guess I am asking for some assistance on
writing the code, but I just need a kick start. This is the code to test the
phone number class.

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include "PhoneNumber.h"

int main()
{
PhoneNumber phone;

cout << "Enter phone number in the form (123) 456-7890:" << endl;

cin >phone;

cout << "The phone number entered was: ";

cout << phone << endl;
return 0;
} // end main.


B. Williams <wi*******@hotmail.comwrote:

I tried inputting the phone number as a string, but
the blank space ends the string.

Try using std::getline() instead.

--
Marcus Kwok
Replace ''invalid'' with ''net'' to reply


这篇关于协助链接列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆