-
November 19th, 2013, 05:46 PM
#1
Linked List - Random Nodes
I am currently working on a code using Linked List. I would like my code to do the following:
1. User Enter integers between 1-60 (first List)
2. The application to check for all the repeated entries, remove them from the initial list and store them on a second list
3. Now that the first list has only one copy of each entry, the application should randomly select a node (using for loop multiple times). After the selection is made, the stored information on the node should be shown and that node to be deleted (but temporarily)
4. After the required number of (5) node data has been printed, all the temporarily deleted nodes to be reinstored.
5. Once thats done, the details of the second list to printed in a summary form i.e frequency of each integer entered.
below is my code.
Code:
#include <iostream>
#include <stdlib.h>
#include <string>
#include <fstream>
using namespace std;
struct NODE {
NODE *pNext;
NODE *pPrev;
int nData;
};
//declare head and tail of list
//originally the list is empty
NODE *pHead=NULL, *pTail=NULL;
void AppendNode(NODE *pNode);
void InsertNode(NODE *pNode, NODE *pAfter);
void RemoveNode(NODE *pNode);
void DeleteAllNodes( );
void discard_line(ifstream &in);
int main()
{
int i = 0;
NODE * pNode;
NODE * pNode1;
//Add items to linked list
do
{
pNode = new NODE; //allocate
// memory for each node and make
// pointer point to the
cin >> i; //dynamically allocated memory
pNode->nData = i; //put some data in the node
AppendNode(pNode); //add node to list
}while((i >= 1 )&&(i <= 60));
/*
for(int i = 0; i < = 3; i++){
for(int j = 0; j < 5; j++)
{
pNode1 = pNode1 % (pNode->nData);
}
} */
// Now display each item in list
for(pNode = pHead; pNode != NULL; pNode = pNode->pNext)
cout<<pNode->nData<<endl;
// DeleteAllNodes();
for(pNode = pHead; pNode != NULL; pNode = pNode->pNext)
{
cout<<pNode->nData<< "\t";
}
system ("pause");
return 0;
}
//function Implementations
void AppendNode(NODE *pNode)
{
if (pHead == NULL) { //if list is empty
pHead = pNode; //make head point to pNode
pNode->pPrev = NULL;
}
else {
pTail->pNext = pNode; //make tail point to pNode
pNode->pPrev = pTail;
}
pTail = pNode; //tail is now pNode
pNode->pNext = NULL; //pNode next now points to NULL
}
/* Inserts a node into the list after pAfter
void InsertNode(NODE *pNode, NODE *pAfter)
{
pNode->pNext = pAfter->pNext; //make next of new node point to "next" node
pNode->pPrev = pAfter; //make prev of new node point the "after" node
if (pAfter->pNext != NULL) // if we are NOT inserting at the end
pAfter->pNext->pPrev = pNode; //make prev of "next" node point to the new node
else
pTail = pNode; //if we are inserting at the end, make new node the tail
pAfter->pNext = pNode; //make next of "after" node point to new node
}
*/
// Removes the specified node from the list
void RemoveNode(NODE *pNode)
{
if (pNode->pPrev == NULL) //if removing the head
pHead = pNode->pNext;
else
pNode->pPrev->pNext = pNode->pNext; //if removing a middle node
if (pNode->pNext == NULL) //if removing the tail
pTail = pNode->pPrev;
else
pNode->pNext->pPrev = pNode->pPrev;
delete pNode; //*free the memory
}
//****** Deletes the entire list******
void DeleteAllNodes( )
{
while (pHead != NULL) //keep on removing until the
//head points to NULL
RemoveNode(pHead);
}
void discard_line(ifstream &in)
{
char c;
do
in.get(c);
while (c!='\n');
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|