473,545 Members | 2,451 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

linked list

8 New Member
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 )
{
}
Oct 3 '06 #1
6 5304
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).
Oct 4 '06 #2
Fazana
8 New Member
so u mean that the code that i have written is not correct....and i have modify my program more.
Oct 4 '06 #3
fanda
2 New Member
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
Oct 4 '06 #4
fanda
2 New Member
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
Oct 5 '06 #5
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
Expand|Select|Wrap|Line Numbers
  1. NULL <- head <-> 5 -> NULL
After
Expand|Select|Wrap|Line Numbers
  1. NULL <- head -> NULL
  2. NULL <- secondHead <-> 5 -> NULL
Expand|Select|Wrap|Line Numbers
  1. Node* splitAt(const int nodeValue)
  2. {
  3.   Node* pCursor = head;
  4.   while(pCursor != NULL)
  5.   {
  6.     if(pCursor->nData == nodeValue)
  7.     {                         // if we found the split element
  8.       if(pCursor == pHead)    // if we split at the head
  9.         pHead = NULL;         // the head becomes null
  10.       pTail = pCursor->pPrev; // tail is the last node before split
  11.       return pCursor;         // return pointer to new head
  12.     }
  13.     pCursor = pCursor->pNext; // else go to next node
  14.   }
  15.   return NULL;                // did not find element
  16. }
Oct 5 '06 #6
Fazana
8 New Member
I have written the code again. Is this a correct program for the question?

Program modified

Expand|Select|Wrap|Line Numbers
  1. #include <iostream.h>
  2. #include <stdlib.h>
  3. #include <string>
  4.  
  5. struct NODE
  6. {
  7.    NODE *pNext;
  8.    NODE *pPrev;
  9.    int nData;
  10. };
  11.  
  12. NODE *pHead=NULL, *pTail=NULL;
  13. NODE *pNewHead;
  14.  
  15.  
  16. void AppendNode(NODE *pNode);
  17. void InsertNode(NODE *pNode, NODE *pAfter);
  18. void RemoveNode(NODE *pNode);
  19. void DeleteAllNodes( );
  20. NODE *splitAt( int item );
  21.  
  22. int main( )
  23. {
  24.    NODE *pNode;
  25.    int item;
  26.    int num;
  27.   char res;
  28.    cout <<"\n\n\t do u want to insert a value [Y/N]???" ;
  29.    cin >>res;
  30.    while(res =='y'|| res =='Y')
  31.    {
  32.     cout <<"enter the number that u want to append:";
  33.     cin >> num;
  34.       pNode = new NODE;
  35.       pNode->nData = num;
  36.       AppendNode ( pNode );
  37.       cout <<"\n\n\t do u want to insert another value [Y/N]???" ;
  38.       cin >>res;
  39.    }
  40.  
  41.    cout<<"\nThe contents of the original list are:\n";
  42.  
  43.    for (pNode = pHead; pNode != NULL; pNode = pNode->pNext)
  44.        cout << "\t" << pNode->nData<<endl;
  45.  
  46.        cout <<"from which item u wish to split the linked list :";
  47.        cin  >>item;
  48.  
  49.  
  50.        splitAt(item);
  51.           cout<<"\nThe contents of the first splited list are:\n";
  52.        for (pNode = pHead; pNode != NULL; pNode = pNode->pNext)
  53.        cout << "\t" << pNode->nData<<endl;
  54.        cout<<"\nThe contents of the second splited list are:\n";
  55.          for (pNode = pNewHead; pNode != NULL; pNode = pNode->pNext)
  56.           cout <<"\t" << pNode->nData<<endl;
  57.  
  58.  
  59.    system ("pause");
  60.    return 0;
  61. }
  62.  
  63. void AppendNode(NODE *pNode)
  64. {
  65.    if (pHead == NULL)
  66.    {
  67.       pHead = pNode;
  68.       pNode->pPrev = NULL;
  69.    }
  70.    else {
  71.       pTail->pNext = pNode;
  72.       pNode->pPrev = pTail;
  73.    }
  74.    pTail = pNode;
  75.    pNode->pNext = NULL;
  76. }
  77.  
  78. void InsertNode(NODE *pNode, NODE *pAfter)
  79. {
  80.    pNode->pNext = pAfter->pNext;
  81.    pNode->pPrev = pAfter;
  82.  
  83.    if (pAfter->pNext != NULL)
  84.       pAfter->pNext->pPrev = pNode;
  85.    else
  86.       pTail = pNode;
  87.  
  88.    pAfter->pNext = pNode;
  89. }
  90.  
  91. void RemoveNode(NODE *pNode)
  92. {
  93.    if (pNode->pPrev == NULL)
  94.        pHead = pNode->pNext;
  95.  
  96.    else
  97.       pNode->pPrev->pNext = pNode->pNext;
  98.  
  99.    if (pNode->pNext == NULL)
  100.       pTail = pNode->pPrev;
  101.  
  102.    else
  103.       pNode->pNext->pPrev = pNode->pPrev;
  104.  
  105.    delete pNode;
  106. }
  107.  
  108. void DeleteAllNodes( )
  109. {
  110.    while (pHead != NULL)
  111.  
  112.       RemoveNode(pHead);
  113. }
  114.  
  115.  
  116. NODE* splitAt(int item )
  117. {
  118.  NODE *pCurr = NULL;
  119.  
  120. for(pCurr = pHead; pCurr!=NULL; pCurr = pCurr->pNext)
  121. {
  122.   if(pCurr->pNext->nData == item)
  123.   {
  124.    pNewHead = pCurr->pNext;
  125.    pCurr->pNext = NULL;
  126.   }
  127. }
  128. }
  129.  
  130.  
Oct 6 '06 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

11
3080
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:
5
859
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
10
15107
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
6
4587
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
12
15076
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 {
12
3933
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...
51
8592
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...
1
15511
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...
0
8613
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...
7
5757
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...
0
7487
marktang
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...
0
7420
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...
0
7680
Oralloy
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. ...
0
7934
jinu1996
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...
0
7778
tracyyun
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...
0
6003
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4966
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3459
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1033
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.