链表 [英] linked list

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

问题描述

我正在做其中一个问题,但我的程序运行不正常。任何人都可以帮助我PLIZ ........


这是该计划的问题


问题。

第1部分。


添加函数splitAt以在给定数据的节点处拆分链接列表。


假设你有一个包含元素的列表

10 18 34 6 28 92 56 48


并且列表将在数据为6的节点处拆分然后你将有两个子列表。

原始元素10 18 34和另一个元素6 28 92 56 48


a。将以下函数添加到双向链表的结构实现中

在课堂上给出。


节点* splitAt(int item);

//将包含Data项的节点上的列表拆分为两个子列表。

//前提条件:列表必须存在

//后置条件:pHead仍然指向原始列表的第一个节点

//该函数返回第二个列表的头指针


从您的主页打印原始列表的内容和结果的子列表。

向用户询问一个值,并在第二个节点上的第二个节点后插入一个带有该值的新节点。


显示结果列表。


第2部分。


a。将以下函数添加到类中给出的模板类List实现中。


void splitAt(List< Type>& secondList,const Type& item);

//将带有Data项的节点上的列表拆分为两个子列表。


注意:请注意,splitAt函数通过引用接收两个参数。第一个

参数是List类型的对象。你不需要对

类进行任何其他修改。只需添加这个新功能。


关键字const只表示无法在

函数中修改item的值。


考虑以下陈述:


List< int> myList;

List< int> otherList;


他们创建两个List类型的对象。

假设myList是一个包含元素的列表34 65 18 39 27 89 12(按此顺序) 。

声明


myList.splitAt(otherList,18);


将myList拆分为两个子列表:myList有元素34 65和otherList有

元素18 39 27 89 12.


写一个驱动程序来打印原始列表的内容和结果子列表。

向用户询问一个值,并在第二个节点上的第二个节点后插入一个带有该值的新节点。


显示结果列表。


除非另有说明,否则不要确认任何键盘输入


我写的程序


#include< iostream.h>

#include< stdlib.h>

#include< string>


struct NODE {

NODE * pNext;

NODE * pPrev;

int nData;

};


NODE * pHead = NULL,* pTail = NULL;


void AppendNode( NODE * pNode);

void InsertNode (NODE * pNode,NODE * pAfter);

void RemoveNode(NODE * pNode);

void DeleteAllNodes();

NODE * splitAt (int item);


int main()

{

NODE * pNode;


for(int i = 0; I< 100; i ++){

pNode =新NODE;

pNode-> nData = i;

AppendNode(pNode);

}


cout<<" \ n原始列表的内容为:\ n" ;;


for (pNode = pHead; pNode!= NULL; pNode = pNode-> pNext)

cout<< " \t" << pNode-> nData<< endl;


system(" pause");

返回0;

}


void AppendNode(NODE * pNode)

{

if(pHead == NULL){

pHead = pNode;

pNode-> pPrev = NULL;

}

else {

pTail - > pNext = pNode;

pNode-> pPrev = pTail;

}

pTail = pNode;

pNode-> pNext = NULL;

}


void InsertNode(NODE * pNode,NODE * pAfter)

{

pNode-> pNext = pAfter-> pNext;

pNode-> pPrev = pAfter;


if(pAfter-> pNext!= NULL)

pAfter-> pNext-> pPrev = pNode;

else

pTail = pNode;


pAfter-> pNext = pNode;

}



void RemoveNode(NODE * pNode)

{

if(pNode-> pPrev == NULL)

pHead = pNode-> pNext;


else

pNode-> pPrev-> pNext = pNode-> pNext;


if(pNode-> pNext == NULL)

pTail = pNode-> pPrev;


else

pNode-> pNext-> pPrev = pNode-> pPrev;


删除pNode;

}


void DeleteAllNodes()

{

while(pHead!= NULL)

RemoveNode(pHead);

}


NODE * splitAt(int item)

{

}

I was doing one of the question but my program was not working properly. Can anyone help me with it pliz........

Here is the question for the program

Question.
Part 1.

Add the function splitAt to split a linked list at a node whose Data is given.

Suppose you have a list with the elements
10 18 34 6 28 92 56 48

and the list is to be split at the node whose Data is 6. You will then have two sublists.
The original one with the elements 10 18 34 and another one with 6 28 92 56 48

a. Add the following function to the structure implementation for doubly linked lists
given in class.

Node* splitAt ( int item);
//splits the list at the node with the Data item into two sublists.
//precondition: The list must exist
//postcondition: pHead still points to the first node of the original list
//the function returns the head pointer for the second list

From your main, print the contents of the original list and the resulting sublists.
Ask the user for a value and insert a new node with that value after the second node on
the second list.

Display the resulting list.

Part 2.

a. Add the following function to the template class List implementation given in class.

void splitAt ( List <Type> &secondList, const Type &item);
//splits the list at the node with the Data item into two sublists.

Note: Notice that the splitAt function receives two parameters by reference. The first
argument is and object of type List. You DON?T need any other modifications to the
class. Just add this new function.

The keyword const just means that the value of item cannot be modified within the
function.

Consider the following statements:

List <int> myList;
List <int> otherList;

they create two objects of type List.
Suppose myList is a list with the elements 34 65 18 39 27 89 12 (in this order). The
Statement

myList.splitAt (otherList, 18);

splits myList into two sublist: myList has the elements 34 65 and otherList has the
elements 18 39 27 89 12.

Write a driver program to print the contents of the original list and the resulting sublists.
Ask the user for a value and insert a new node with that value after the second node on
the second list.

Display the resulting list.

DO NOT VALIDATE ANY KEYBOARD ENTRY UNLESS OTHERWISE SPECIFIED

My program that i have written

#include <iostream.h>
#include <stdlib.h>
#include <string>

struct NODE {
NODE *pNext;
NODE *pPrev;
int nData;
};

NODE *pHead=NULL, *pTail=NULL;


void AppendNode(NODE *pNode);
void InsertNode(NODE *pNode, NODE *pAfter);
void RemoveNode(NODE *pNode);
void DeleteAllNodes( );
NODE *splitAt( int item );

int main( )
{
NODE *pNode;

for (int i=0; i<100; i++){
pNode = new NODE;
pNode->nData = i;
AppendNode ( pNode );
}

cout<<"\nThe contents of the original list are:\n";

for (pNode = pHead; pNode != NULL; pNode = pNode->pNext)
cout << "\t" << pNode->nData<<endl;

system ("pause");
return 0;
}

void AppendNode(NODE *pNode)
{
if (pHead == NULL) {
pHead = pNode;
pNode->pPrev = NULL;
}
else {
pTail->pNext = pNode;
pNode->pPrev = pTail;
}
pTail = pNode;
pNode->pNext = NULL;
}


void InsertNode(NODE *pNode, NODE *pAfter)
{
pNode->pNext = pAfter->pNext;
pNode->pPrev = pAfter;

if (pAfter->pNext != NULL)
pAfter->pNext->pPrev = pNode;
else
pTail = pNode;

pAfter->pNext = pNode;
}


void RemoveNode(NODE *pNode)
{
if (pNode->pPrev == NULL)
pHead = pNode->pNext;

else
pNode->pPrev->pNext = pNode->pNext;

if (pNode->pNext == NULL)
pTail = pNode->pPrev;

else
pNode->pNext->pPrev = pNode->pPrev;

delete pNode;
}


void DeleteAllNodes( )
{
while (pHead != NULL)
RemoveNode(pHead);
}

NODE* splitAt(int item )
{
}

推荐答案

我假设您的列表存在唯一性约束(即你不能在列表中有2个或3个副本6,否则你会分成哪个?)。


在列表中找到split元素。前一个元素的下一个指针需要无效,并且拆分元素的前一个指针需要无效。然后返回& split_element。你有一个特殊的情况,你分割头部元素,但不是尾部元素(除非头部是尾部)。
I assume there is a uniqueness constraint for your lists (i.e. you can''t have 2 or 3 copies of 6 in the list, otherwise which one would you split at?).

Find the split element in the list. The previous element''s next pointer needs to be nullified, and the split element''s previous pointer needs to be nullified. Then return &split_element. You have a special case when you split the head element, but not the tail element (unless the head is the tail).


所以你的意思是我写的代码不正确....我已经修改了我的程序。
so u mean that the code that i have written is not correct....and i have modify my program more.


编写一个驱动程序来打印原始列表的内容和生成的子列表。

问用户获取值并在第二个节点上插入具有该值的新节点

第二个列表。


任何人都可以为avove实现部分提供解决方案。

i表示如何插入:

void InsertNode(NODE * pNode,NODE * pAfter)


怎么称呼这个main()的功能。我有解决这个问题的问题。我已经解决了其余问题。

问候

fanda
Write a driver program to print the contents of the original list and the resulting sublists.
Ask the user for a value and insert a new node with that value after the second node on
the second list.

can anyone provide solution to the avove implementation part.
i mean how to insert the :
void InsertNode(NODE *pNode, NODE *pAfter)

how to call this funtion from main(). i have problem solving that part. i have solved the rest.
regards
fanda


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

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