473,397 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Linked List: Reprinting the List?

Okay I fixed my code and this is what i got but i cant reprint the list and take out the desired number.
its at the bottom

#include<iostream.h>

struct ListNode
{
int number;
ListNode *link;
};

typedef ListNode* ListNodePtr;

void insertNodeAtFront(ListNodePtr&, int);
void printList(ListNodePtr);
void printReversed(ListNodePtr);
ListNodePtr search(ListNodePtr, int);
void insertAfter(ListNodePtr, int);
void insertAtRear(ListNodePtr, int);
void removeNode(ListNodePtr, ListNodePtr);

void main()
{
ListNodePtr head = new ListNode;
head -> number = 5;
head -> link = NULL;

for(int i = 0; i < 20; i++)
insertNodeAtFront(head, 10*i);

//printList(head);
//printReversed(head);

cout << "What number would you like to find?" << endl;
int target;
cin >> target;
ListNodePtr result = search(head, target);
if(result != NULL)
insertAfter(result, 18);

cout << "What item would you like to remove?" << endl;
cin >> target;
result = search(head, target);
if(result != NULL)
removeNode(head, result);

insertAtRear(head, 99);
printList(head);
int a;
cin >> a;
}

void insertNodeAtFront(ListNodePtr& home, int num)
{
ListNodePtr tempPtr = new ListNode;
tempPtr -> number = num;
tempPtr -> link = home;
home = tempPtr;
}

void printList(ListNodePtr home)
{
for(ListNodePtr here = home; here != NULL; here = here ->link)
{
cout << here -> number << endl;
}
cout << "done" << endl;
}

void printReversed(ListNodePtr here)
{
if(here != NULL)
{
printReversed(here -> link);
cout << here ->number << endl;
}
}

ListNodePtr search(ListNodePtr here, int target)
{
ListNodePtr tempPtr = here;
while((tempPtr -> number != target)&&(tempPtr -> link != NULL))
tempPtr = tempPtr -> link;

if(tempPtr -> number == target)
return tempPtr;
else
return NULL;
}

void insertAfter(ListNodePtr here, int num)
{
ListNodePtr tempPtr = new ListNode;
tempPtr -> number = num;
tempPtr -> link = here -> link;
here -> link = tempPtr;
}

void insertAtRear(ListNodePtr head, int num)
{
ListNodePtr tempPtr = new ListNode;
tempPtr -> number = num;
tempPtr -> link = NULL;
ListNodePtr finder = head;
while(finder -> link != NULL)
finder = finder -> link;
finder -> link = tempPtr;
}


//right here
void removeNode(ListNodePtr head, ListNodePtr here)
{
ListNode *cursor;
for(cursor = head; cursor->link != here; cursor = cursor -> link)
{
cursor->link = head -> link;

}
}

cant get the list to reprint out and take out the desired number.
Dec 5 '07 #1
1 1392
looker
18
Well let me break your source codes in pieces and go line by line.

#include<iostream.h>

struct ListNode
{
int number;
ListNode *link;
};

typedef ListNode* ListNodePtr;

void insertNodeAtFront(ListNodePtr&, int);
void printList(ListNodePtr);
void printReversed(ListNodePtr);
ListNodePtr search(ListNodePtr, int);
void insertAfter(ListNodePtr, int);
void insertAtRear(ListNodePtr, int);
void removeNode(ListNodePtr, ListNodePtr);
This block of code should be in a header file.....why ?
I do know that it works just the same way as what you did it now, but you will have some big problems in upgradability and scalability. so why ?
What happen when another file .cpp want to call a routine in this prototype ?
or what if your code becomes a Library for the all the Informatics Engineers. So you need to update it to become a solid library right ?

The best way to go is to
- mkdir structure of your files
- Learn to use header file, resource file ....etc
- put the right files in the right folder.

I would suggest you that you better put all the function prototypes, type, #define...etc in a header file.....because it will be easy for readability and your current code will become a very strong library for other to use.

void main()
{
ListNodePtr head = new ListNode;
head -> number = 5;
head -> link = NULL;
First you allocate memory for head, and then you assign the data ( number = 5 ) and the tail of your list = NULL, right ?
At the first view, I also think that it is the right way to go. but it never 100% correct....why ?
I do know that you use new ListNode mean you instructs the system to do it for you. but are you sure 100% that the system will allocates memory for you (head) on the size of your need ? Memory is for everyone to read or write. This is because you assume yourself that everything is fine. but sometime it never.
So i would suggest you to ALWAYS do sanity check on the pointer you allocated before bring do something with it.
so it should look like this

ListNodePtr head = new ListNode;
if (NULL == head)
{
return (something to tell us that there is something wrong);
}
......do your job here....


One more problem with that piece of code. Why you think main should return void ? Are you sure that your main routine works properly and never get bug ?
So if there is a bug in your code, how can you trace out that bug and know what happen ?. You can use dubugger to step over/in each line of code to look for bug right ?....but imagine you have a million lines of code ( like the one of Microsoft ). then how long you spend to check for bug...? i do believe that you will spend a month at least to look for a little bug. WASTING TIME.
So i would suggest that you should return error code to main. it is very helpful to trace out what wrong with code.



for(int i = 0; i < 20; i++)
insertNodeAtFront(head, 10*i);

//printList(head);
//printReversed(head);

cout << "What number would you like to find?" << endl;
int target;
cin >> target;
ListNodePtr result = search(head, target);
if(result != NULL)
insertAfter(result, 18);

cout << "What item would you like to remove?" << endl;
cin >> target;
result = search(head, target);
if(result != NULL)
removeNode(head, result);

insertAtRear(head, 99);
printList(head);
int a;
cin >> a;
}
1.The problem with constant value and string
I think this link below will help you much
http://www.thescripts.com/forum/thread745005.html

2. The problem with calling function
You call insertNodeAtFront(....) and do something after. Why do you believe that insertNodeAtFront work properly and never get bug ? it is because you make sure yourself ( cos you are the author of this code ). But what wrong if there is a bug in your insertNodeAtFront and your code is a library ( the other programmer include your library and use because they are lazy to re-code again ), So do you know what wrong with that bug.....? impossible right ?
So i would love to see you ALWAYS return value for all your functions, and ALWAYS check for return value when calling them in your own function.

3. The problem with your target variable.
Why you are sure that variable target must be integer ?
This is because you assume that the user will enter integer as an input. but do you know the best application is design not for a clever user, but if you can design a very reliable system for DUMMY user, that is the excellent, i think.
So what happen when user ( intentionally or unintentionally ) enter alphabetic data ( a, b ,c ....) So what happen when it reach a line : cin >> target. CRASH
So you better declare target as char or array of char. and then convert it to integer by using atoi(.......), What you need to do next is to check the return value of atoi to make sure that input data is integer or not ? go to http://www.cplusplus.com/reference/c...dlib/atoi.html for reference.

[/quote]

Well let me finish at this point, i will be back tomorrow to go on with other bugs
Dec 6 '07 #2

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

Similar topics

5
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...
10
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...
6
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...
12
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...
51
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...
8
by: Jack | last post by:
Hi I have a somewhat easy question. Lets say I have a link list (struct) with three items in it and I want to delete the middle item. Once I point the pointer from the first item to the third...
4
by: Miesha.James | last post by:
Hello. Is there a way you can insert a new row between current rows in a .NET listview? I tried and it was no success. The reason I want to do this is because when the application starts up it...
0
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...
7
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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...
0
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.