473,386 Members | 1,720 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,386 software developers and data experts.

Linked list inserting items from two different funcion

Kay
There are two different function. The first one loads data from a txt
file and adds the name and num to the linked list. It can display the
context of the linked list. I'm designing to use second function to
input the data and add the new data to the same linked list. However, It
doesn't work. Did I miss sth or do sth wrong in the second function ?
//Load data from restaurant text file
void rest_list(istream& is, Position p, List * restL) {

string temp_name;

p = ListHead(restL);
int num = 1;

//get the restaurant name in restaurnat.txt
while ( getline(is, temp_name, '\n')) {
char * name;

//change type of name of restaurant from string to char
name = new char[temp_name.length() + 1 ];
strcpy( name, temp_name.c_str());

if (!insert_name(restL, p, name, num )) {
exit (EXIT_FAILURE);
}

//Move to the next node
p = ListNext(restL, p);
num++;

}


int AddRestaurant( List * l, Position pos, int num){

ListNode *p;

for ( p = l->head; p != NULL; p = p->next) {
if( (int)p->number == num ){
cout << (char*)p->item;
if (!insert_name(l, pos, (char*)p->item, num )) {
exit (EXIT_FAILURE);
}
return 0;
}

}

Jul 22 '05 #1
1 1371

"Kay" <er*********@yahoo.com.hk> wrote in message
news:41**************@yahoo.com.hk...
There are two different function. The first one loads data from a txt
file and adds the name and num to the linked list. It can display the
context of the linked list. I'm designing to use second function to
input the data and add the new data to the same linked list. However, It
doesn't work. Did I miss sth or do sth wrong in the second function ?


Well one thing you don't do in the second function is input any data. You
just seem to be adding the data that is already there, is there any reason
for that?

Secondly in the first function insert_name is only used (apparently) to add
data to the end of a list. In the second function you seem to be adding data
to the middle of a list. Has insert_name been written in such a way that it
can handle both cases? If not then that would explain why the second
function doesn't work.

It's impossible to give specific advice because there is far to much missing
from the code you posted, like the definition of List and Position and
insert_name. It's also a good idea, when posting, to say exactly what 'it
doesn't work' means.

john
Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Jonathan Moss | last post by:
I have (almost) succesfully created a linked list data structure class. It will quite happily add items to to the end of the list (once I found that objects are not passed by reference :). What I...
3
by: Andrew Clark | last post by:
*** post for FREE via your newsreader at post.newsfeed.com *** it's been a while since i have poseted to this newsgroup, but for a long time i was not programming at all. but now that i am out of...
7
by: dam_fool_2003 | last post by:
friends, I wanted to learn the various ways of inserting a single list. so: Method 1: #include<stdlib.h> #include<stdio.h> struct node { unsigned int data; struct node *next;
14
by: Peter Schmitz | last post by:
Hi, for my current application, I need a linked list in which I can access and walk through at highest speed possible - adding/deleting/inserting elements can be as slow as necessary. Now, which...
6
by: deanfamily | last post by:
I am re-posting my second problem. I have a double-linked list. I need to know if it is possible to remove just one of an item, instead of all that match the given criteria with the remove()...
4
by: FBM | last post by:
Hi, I am working on a program that simulates one of the elements of ATM. The simulation stores events which occurs every some milliseconds for a certain amount of time. Every time that an event...
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...
9
by: Sheldon | last post by:
Hi, I am trying to understand linked lists and the different ways to write a linked list and double linked list. I have been trying to get this function called insert_word to work but to no...
8
by: Jeff Bown | last post by:
Consider implementing a (doubly) linked list. The simplest strategy is to provide functions item_t add_item(item_t predecessor); void delete_item(item_t item); where add_item allocates memory...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
0
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...

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.