Computing.Net > Forums > Programming > prob with Linked list in C

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

prob with Linked list in C

Reply to Message Icon

Name: Chis W
Date: January 8, 2002 at 04:55:43 Pacific
Comment:

I am having problems with some code containing a linked list which i am writing in C i have defined an order structure to hold data for an order and then defined a Node structure to hold an order and the next node i.e

typedef struct Node
{
Order *order;
struct Node* next;
}Node;

then i have defined a list as
typedef struct List
{
Node* head;
}List;

i populate the order then assign the order to a new Node called node then i have set up a pointer(ptr) to initially point to
list->head (note list->head is initialised so its next value is itself)
then i use a while loop that says while(
ptr->next !=list->head){ptr=ptr->next;}
so that i finds the end of the list to insert the node. then i assign ptr->next = node;
and node->next = list->head;
but when i later go through the list trying to print a value from each order it only prints the last order read in??
as if the progarm is not adding the node to the end of the lsit but instead just overwriting the first node. also the other problem is that all this is in a function add_record() that is runs for each line in the text file that holds the orders. i decided to check if the list->head->next value had changed after the first time and so i got the program to check if list->head->next =list ->head which should be true the first time then false from second time on yet it always returns true?? so i cant see how it can be getting the value for the last order and be saying that the lists head is always pointing to itself?



Sponsored Link
Ads by Google

Response Number 1
Name: Apple
Date: January 8, 2002 at 16:56:34 Pacific
Reply:

What I'm getting out of this is you have a circular linked list that you're trying to find the end of. hmm. By definition, this causes problems.

Unless you NEED to have the end of the linked list be the beginning, make the last node NULL. It's simple and rather obvious when you find it. Also, you won't have problems like while( ptr != head ) because at some point ptr == head and you want it to.

Try something like:
ptr = head;
while( ptr->next != NULL ) ptr = ptr->next;

To tack a node onto the end of a ll,

ptr = head;
while( ptr->next != NULL ) ptr = ptr->next;
temp = new Node;
/* initialize temp here */
ptr->next = temp;
temp->next = NULL;

To tack a node onto the beginning of a ll,

ptr = new Node;
/* initialize ptr here */
ptr->next = head;
head = ptr;

No worry, no bugs.

Strictly speaking, your struct List is unnecessary. A Node * will do the same thing and you don't need the confusing ->s all over the place.


0

Response Number 2
Name: yakup
Date: March 12, 2002 at 11:09:37 Pacific
Reply:

1. INTRODUCTIon
Many applications require operations on floating point numbers that have many digits so that, any of the programming languages doesn’t support. In this experiment, you will study Linked lists in C programming language. With respect to scenario, you will design your own linked list structure and will practise on pointers.
1. DESCRIPTION
You will implement mathematical operations (“+”,”-“,”CMP”) on floating point numbers using linked lists.
Experiment Steps

1. Your program must read an input file that will be specified on the command line (see Appendix a).
1. You must define linked lists to hold the numbers in input file (See Appendix c). You are forbidden to use C type arrays to hold numbers. (“+”, “-“,”CMP”) floating operations will be applied on linked lists.
2. The parenthesis will be used for priorities, you may use stack data type in order to consider priorities.
3. The operations given in the input file must be done.
As you may notice, these operations must only operate on linked lists defined in Appendix c. This means, you must not use any different data types to keep the numbers during operations on digits.
5. You will write the result on the output file that will be specified on the command line as shown in Appendix b.

Notes:
1. Do not use static structures to hold numbers. For example don’t use arrays or any other static structures instead of linked lists (Dont’ transfer the values of the nodes into arrays to make calculations).
1. xCMPx => 1 ; xCMPy=> 0 (If x is not equal to y)
APPENDIX A – Input file example
2365984398.54986459-4354654699.34337942739+9454666469.159684125854-365465499.86465469809-94655659.5658091230+423156483459.978323958739+90344560930939.9931+123.345CMP456.568=
345.456234-234.789+234.7892-345.456+399.939+9239.2398-934.29+3489.238-(456.5678CMP2345CMP34.7)=
949349.349489+348.239230-89.38+2349.89-389.239+39.654+654.64-654.654456+456.429-239.489=
APPENDIX B – Output file example

2365984398.54986459-4354654699.34337942739+9454666469.159684125854-365465499.86465469809-94655659.5658091230+423156483459.978323958739+90344560930939.9931+123.345CMP456.568=90774723289408.907129426113
345.456234-234.789+234.7892-345.456+399.939+9239.2398-934.29+3489.238-(456.5678CMP2345CMP34.7)=12194.127234
949349.349489+348.239230-89.38+2349.89-389.239+39.654+654.64-654.654456+456.429-239.489=951825.439263



APPENDIX C
Number 12.6 must be declared like this..............


! This linked list must hold a floating-point number.
! Every node must contain only one field which is a digit or “.”.


0

Response Number 3
Name: Ahmed Zouari
Date: April 15, 2002 at 14:49:11 Pacific
Reply:

I need some help with this:
LAB #5 ASSIGNMENT

ASSIGNED : week of Monday, March 25, 2002
DUE : week of Monday, April 15, 2002 (3 weeks)
CONCEPTS : structures,
file i/o,
command line parameters,
linked lists
Weight : 8%

Write a C program that will use a linked list of structures to
represent the customers of a bank. Each structure will be designed
to hold information regarding an individual's

(1) Last name
(2) First name
(3) Middle initial
(4) Social Security number
(5) Bank balance

The program must create the linked list by accepting initial input from the
input file "lab3_1.dat". The input file is listed in random order, but the
linked list must be maintained in increasing alphabetical order based on the
the customer's last name. Two customers with the same last name should be
listed in alphabetical order based on first name. Two customers with the
same last and first name should be listed in numerical order based on the
Social Security number. As each node is added to the linked list, print out
the contents of the linked linked list so far, in formatted order, including
the following items:

(1) The node's location in the list (1, 2, 3, etc.), beginning with the first
node. This "location" can not be a field within the node.
(2) Address of the node in the linked list, in UPPER-CASE HEXADECIMAL
(3) Last name
(4) First name
(5) Middle initial
(6) Social Security number
(7) Bank balance
(8) Address of the next node in the linked list, in UPPER-CASE HEXADECIMAL
(note: an address of NULL must be displayed as the string "NULL", not 0)

Typical lines from the input file "lab3_1.dat" might look something like:

Heineman Doug A. 264592791 19473.08
Springston Ray L. 374926490 40392.37
Davis, Don R. 194394500 50394.97
Taffe, Henry C. 028495039 84039.00

and the output file might look something like

NUM ADDRESS LAST FIRST MI SSN BALANCE NEXT
-----------
1 1F3928C0 Heineman Doug A 264592791 19473.00 NULL


NUM ADDRESS LAST FIRST MI SSN BALANCE NEXT
-----------
1 1F3928C0 Heineman Doug A 264592791 19473.00 1F3928D8
2 1F3928D8 Springston Ray L 374926490 40392.37 NULL


NUM ADDRESS LAST FIRST MI SSN BALANCE NEXT
-----------
1 1F403940 Davis Don R 194394500 50394.97 1F3928C0
2 1F3928C0 Heineman Doug A 264592791 19473.00 1F3928D8
3 1F3928D8 Springston Ray L 374926490 40392.37 NULL


NUM ADDRESS LAST FIRST MI SSN BALANCE NEXT
-----------
1 1F403940 Davis Don R 194394500 50394.97 1F3928C0
2 1F3928C0 Heineman Doug A 264592791 19473.00 1F3928D8
3 1F3928D8 Springston Ray L 374926490 40392.37 1F40394C
4 1F40394C Taffe Henry C 028495039 84039.00 NULL

(as customers are being added to the bank and the updated linked lists
are being printed out, be sure to separate each printout of the linked
list by at least two blank lines, as shown above)

NOTE: use an output file to contain all output of this program except for
possibly statements executed as a result of testing the success or failure
of fopen(), etc., which could print their output to the screen (but would
still show up in the photo session).

NOTE: the names of the input and output file must be entered as command line
parameters.

After initialization of the linked list, update the bank balances in the list
from the input file "lab3_2.dat", which is a list of transaction requests.
This file contains a person's last and first name and middle initial, followed
by the Social Security number and an amount which is either a deposit or with-
drawal(signified by a + or - preceding the amount of the transaction requested,
as in

Springston Ray L. 374926490 +2048.93
Heineman Doug A. 264592791 -43.08
Nut Cashew R. 392749309 +189.95
Springston Ray L. 374926490 -300049.08

NOTE: neither input file is formatted--varying spaces exist between each item
in each row. Also, this assignment needs to work for these input files only.


The following possible scenarios ("cases") regarding transactions by this
bank will be simulated in this assignment:

WITHDRAWAL >
SCENARIO person is already a customer? DEPOSIT? WITHDRAWAL? BALANCE?
--------------------
CASE 1: NO YES
CASE 2: NO YES N/A
CASE 3: YES YES
CASE 4: YES YES YES
CASE 5: YES YES NO


/****************************************************************************/

CASE 1:

If the Social Security number does NOT already exist in the linked list, AND
the transaction request is a DEPOSIT, then

(1) print out a statement to the effect that this person is being added to
the linked list, including first and last name and middle initial,
Social Security number, and balance
(2) then create the new node and add it to the linked list, maintaining
the list in the order specified above
(3) then print out the new complete contents of the linked list

NOTE: so it is possible that customers will be added to the linked list from
the 2nd input file.


/***************************************************************************/

CASE 2:

If the Social Security number does NOT already exist in the linked list, AND
the transaction request is a WITHDRAWAL, then

(1) print out a statement to the effect that this person does not exist in
the linked list, including first and last name and middle initial,
Social Security number, and amount of the requested withdrawal, in one
complete sentence
(2) then print out the complete contents of the linked list


/***************************************************************************/

CASE 3:

If the Social Security number DOES already exist in the linked list, AND the
transaction request is a DEPOSIT, then

(1) print out a statement to the effect that this person's (by last and first
name and middle initial and SSN) balance will be increased by DEPOSIT
amount
(2) then increase the balance of that person by the DEPOSIT amount, in that
node in the linked list

(3) then print out the complete contents of the linked list


/***************************************************************************/

CASE 4:

If the Social Security number DOES already exist in the linked list, AND the
transaction request is a WITHDRAWAL, and the amount to be withdrawn does NOT
exceed the balance, then

(1) print out a statement to the effect that this person's (by last and first
name and middle initial and SSN) has requested this withdrawal
(2) reduce the balance by the amount of the WITHDRAWAL
(3) and print out a statement to that effect
(4) and then print out the new complete contents of the linked list


/***************************************************************************/

CASE 5:

BUT if the Social Security number DOES already exist in the linked list, AND the
transaction request is a WITHDRAWAL, AND the amount to be withdrawn exceeds
the balance, then

(1) print out a statement to that effect, including the person's last and
first name and middle initial, Social Security number, present balance,
and the withdrawal amount requested, beginning on a new page
(2) then delete the node from the linked list, still maintaining the list in
the order specified above
(3) then print out the complete contents of the linked list

/////////////////////////////////////////////////////////////////////////////

For the above sample 2nd input file, the output might look something like:


Ray L. Springston, 374926490, is making a deposit of $2048.93.

The customers are:

NUM ADDRESS LAST FIRST MI SSN BALANCE NEXT
---------------
1 1F3928C0 Heineman Doug A 264592791 19473.00 1F3928D8
2 1F3928D8 Springston Ray L 374926490 42441.30 NULL


Doug A. Heineman, 264592791, is requesting a withdrawal of $43.08.
Doug A. Heineman, 264592791, has withdrawn $43.08.

The customers are:

NUM ADDRESS LAST FIRST MI SSN BALANCE NEXT
--------------
1 1F3928C0 Heineman Doug A 264592791 19429.92 1F3928D8
2 1F3928D8 Springston Ray L 374926490 42441.30 NULL

The customers are:

Cashew R. Nut, 392749309, is being added to the list with a balance of $189.95.

NUM ADDRESS LAST FIRST MI SSN BALANCE NEXT
--------------
1 1F3928C0 Heineman Doug A 264592791 19429.92 1F3928D8
2 1F3928E7 Nut Cashew R 392749309 189.95 1F3928D8
3 1F3928D8 Springston Ray L 374926490 42441.30 NULL


Ray L. Springston, 374926490, is requesting a withdrawal of $300049.08 but has
a balance of only $42441.30.

The customers are:

NUM ADDRESS LAST FIRST MI SSN BALANCE NEXT
-------------
1 1F3928C0 Heineman Doug A 264592791 19429.92 1F3928D8
2 1F3928E7 Nut Cashew R 392749309 189.95 NULL

/////////////////////////////////////////////////////////////////////////////

Once all transactions from "Lab3_2.dat" have been processed, close the file
and print out to the output file the final contents of the linked list.

TURNIN a photo copy of your source code, your header file, the compile/link/run
commands, any associated screen output, followed by a 'cat' of the output file.

NOTE: the input files will be emailed separately from this assignment. In the
meantime, set up the above example input files and use them to test
the code
NOTE: use 2 source code files : constants, and functions, and conditionally
compile each
NOTE: hard-coded numbers may be used in this assignment.
NOTE: each file must have its own program header and each function must have
it's own function header

GRADING SCHEME (100 pts):

CODE : 50 pts

+ 5 : complete program and function headers
+ 5 : command line parameters(input and output file names)
+15 : proper use of the linked list concept
+10 : proper use of functions (1 main task per function, no more than 8
decision points in a function, each function no larger than 1 page,
etc.)
+10 : style (line comments, indentation, consistency, meaningful identifiers,
etc.)
+ 5 : conditional compilation of each of the constants file and the
functions file

OUTPUT : 50 pts

+15 : format
+25 : accuracy, including maintaining of the linked list in the order
specified in the assignment
+10 : clarity


This page was last updated on 03.25.2002.


0

Response Number 4
Name: Trilok
Date: May 1, 2002 at 10:11:25 Pacific
Reply:

Hi Sir/Madam ,

I am having problems with Reversing a single Linked List. Please help me out .
Thank U.



0

Response Number 5
Name: bego loo
Date: May 2, 2002 at 23:53:38 Pacific
Reply:

#include
#include
#include
#include
void cetak ();
void main ()
{
struct date d;
int jml,i;
long int ahir,hasil=0;
char karakter;
getdate(&d);
clrscr();
printf("\nMasukkan jumlah string : ");
scanf("%d",&jml);
for (i=1;i>8;

}
printf("\nHasil : %c",ahir);

gotoxy(50,20);printf("\n\n Dibuat oleh : Prasatyo Adji, G64101901");
printf("\n Diperiksa : %d %d %d", d.da_day, d.da_mon, d.da_year);
getch();
}


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon

prolog help ANSI Characters in VB



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: prob with Linked list in C

Linked Lists in C www.computing.net/answers/programming/linked-lists-in-c/11729.html

Linked List in C problem in Vista www.computing.net/answers/programming/linked-list-in-c-problem-in-vista/19940.html

linked list in c www.computing.net/answers/programming/linked-list-in-c/7408.html