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(pHea d);
}
NODE* splitAt(int item )
{
}
6 5336 D_C 293
Contributor
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_eleme nt. 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.
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
void List::SplitList (const int iSplitValue)
{
Node *pCurr = NULL;
for(pCurr = pHead; pCurr; pCurr = pCurr->pNext)
if(pCurr->pNext->iValue == iSplitValue)
{
pNewHead = pCurr->pNext;
pCurr->pNext = NULL;
}
}
//hope this helps you in splitting the linked list
D_C 293
Contributor
so u mean that the code that i have written is not correct....and i have modify my program more.
You didn't write any code for the splitAt method, so it correctly does nothing. Yes, you need more code. After looking at fanda's code above, I think it's missing something.
For example, create a list with one element, say 5. Split it at that element. That should break fanda's code. Look at the head pointer, before and after. Let <- be pPrev, -> be pNext, and <-> be two nodes pointing to each other with pPrev, and pNext respectively. Before - NULL <- head <-> 5 -> NULL
After - NULL <- head -> NULL
-
NULL <- secondHead <-> 5 -> NULL
- Node* splitAt(const int nodeValue)
-
{
-
Node* pCursor = head;
-
while(pCursor != NULL)
-
{
-
if(pCursor->nData == nodeValue)
-
{ // if we found the split element
-
if(pCursor == pHead) // if we split at the head
-
pHead = NULL; // the head becomes null
-
pTail = pCursor->pPrev; // tail is the last node before split
-
return pCursor; // return pointer to new head
-
}
-
pCursor = pCursor->pNext; // else go to next node
-
}
-
return NULL; // did not find element
-
}
I have written the code again. Is this a correct program for the question?
Program modified Sign in to post your reply or Sign up for a free account.
Similar topics |
by: C++fan |
last post by:
Suppose that I define the following class:
class example_class{
public:
example_class();
void funtion_1();
void function_2();
protected:
|
by: Dream Catcher |
last post by:
1. I don't know once the node is located, how to return that node.
Should I return pointer to that node or should I return the struct of that
node.
2. Also how to do the fn call in main for that LOCATE subroutine that
returns a node????
Any help would be appreciated.
Thanks
|
by: Kent |
last post by:
Hi!
I want to store data (of enemys in a game) as a linked list, each node will
look something like the following:
struct node
{
double x,y; // x and y position coordinates
struct enemy *enemydata; // Holds information about an enemy (in a game)
// Its a double linked list node
|
by: Steve Lambert |
last post by:
Hi,
I've knocked up a number of small routines to create and manipulate a linked
list of any structure. If anyone could take a look at this code and give me
their opinion and details of any potential pitfalls I'd be extremely
grateful.
Cheers
Steve
|
by: Eugen J. Sobchenko |
last post by:
Hi!
I'm writing function which swaps two arbitrary elements
of double-linked list. References to the next element of list
must be unique or NULL (even during swap procedure), the same condition
should be kept for references to previous element of list.
Here is my solution below:
struct node {
| |
by: joshd |
last post by:
Hello,
Im sorry if this question has been asked before, but I did search
before posting and couldnt find an answer to my problem. I have two
classes each with corresponding linked lists, list1 and list2, each
node within list1 has various data and needs to have a pointer to the
corresponding node in list2, but I cant figure out how to do this.
Could someone explain what I might be missing, or maybe point me in the
direction of a good...
|
by: Joerg Schoen |
last post by:
Hi folks!
Everyone knows how to sort arrays (e. g. quicksort, heapsort etc.)
For linked lists, mergesort is the typical choice.
While I was looking for a optimized implementation of mergesort for
linked lists, I couldn't find one. I read something about Mcilroy's
"Optimistic Merge Sort" and studied some implementation, but they
were for arrays. Does anybody know if Mcilroys optimization is applicable to
truly linked lists at all?
|
by: theeverdead |
last post by:
Ok I have a file in it is a record of a persons first and last name. Format is like:
Trevor Johnson
Kevin Smith
Allan Harris
I need to read that file into program and then turn it into a linked list. So on the list I can go Trevor, Kevin, Allan in a straight row but I can also call out there last name when I am on their first name in the list. Sorry if it doesn't make sense trying to explain best I can.
So far I have
// list.cpp
|
by: Atos |
last post by:
SINGLE-LINKED LIST
Let's start with the simplest kind of linked list : the single-linked list which only has one link per node. That node except from the data it contains, which might be anything from a short integer value to a complex struct type, also has a pointer to
the next node in the single-linked list. That pointer will be NULL if the end of the single-linked list is encountered.
The single-linked list travels only one...
|
by: QiongZ |
last post by:
Hi, I just recently started studying C++ and basically copied an example in the textbook into VS2008, but it doesn't compile. I tried to modify the code by eliminating all the templates then it compiled no problem. But I can't find the what the problem is with templates? Please help. The main is in test-linked-list.cpp. There are two template classes. One is List1, the other one is ListNode. The codes are below:
// test-linked-list.cpp :...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |