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

Big problem doing sorted insertion in a list

I've compiled this code and no problems, but when I run the program, it
prints only the last entry i've inserted. Looks like a problem in the
sorted insertion algorithm. Can u help me plz?

#include <stdio.h>
#include <stdlib.h>

/*---data type---*/
typedef int type;
/*---------------*/

/*---the node---*/
struct node
{
type data;
struct node *next;
};
typedef struct node Node; /* <-defines the node object */
typedef Node *NodePtr; /* <-defines the pointer to a node */
/*-------------*/

/*sorted insertion*/
void addsorted(NodePtr *root, type value)
{
NodePtr newptr;
NodePtr back;
NodePtr curr;
if(newptr = malloc(sizeof(Node)))
{
newptr->data = value;
newptr->next = NULL;
back = NULL;
curr = *root;
while(curr != NULL && value curr->data)
{
back = curr;
curr = curr->next;

}
if(back == NULL) /*value is the smallest number in
the list*/
{
newptr->next = *root;
*root = newptr;
}
else /* back<value<curr */
{
back->next = newptr;
newptr->next = curr;
}
}
}
/*---------------*/

/*-list printer--*/
void printlist(NodePtr root)
{
while(root)
{
printf("\n%d\n", root->data);
root = root->next;
}
}
/*---------------*/

/*---the main----*/
int main()
{
NodePtr root = NULL;
char ins;
printf("Inserisci i numeri della lista, inserisci un carattere
per interrompere\n");
while(1 == scanf("%d", &ins))
{
addsorted(&root, ins);
}
if(root)
printlist(root);
system("Pause");
return 0;
}

Jul 14 '06 #1
3 1478

Franco Perilli ha scritto:
I've compiled this code and no problems, but when I run the program, it
prints only the last entry i've inserted. Looks like a problem in the
sorted insertion algorithm. Can u help me plz?

#include <stdio.h>
#include <stdlib.h>

/*---data type---*/
typedef int type;
/*---------------*/

/*---the node---*/
struct node
{
type data;
struct node *next;
};
typedef struct node Node; /* <-defines the node object */
typedef Node *NodePtr; /* <-defines the pointer to a node */
/*-------------*/

/*sorted insertion*/
void addsorted(NodePtr *root, type value)
{
NodePtr newptr;
NodePtr back;
NodePtr curr;
if(newptr = malloc(sizeof(Node)))
{
newptr->data = value;
newptr->next = NULL;
back = NULL;
curr = *root;
while(curr != NULL && value curr->data)
{
back = curr;
curr = curr->next;

}
if(back == NULL) /*value is the smallest number in
the list*/
{
newptr->next = *root;
*root = newptr;
}
else /* back<value<curr */
{
back->next = newptr;
newptr->next = curr;
}
}
}
/*---------------*/

/*-list printer--*/
void printlist(NodePtr root)
{
while(root)
{
printf("\n%d\n", root->data);
root = root->next;
}
}
/*---------------*/

/*---the main----*/
int main()
{
NodePtr root = NULL;
char ins;
printf("Inserisci i numeri della lista, inserisci un carattere
per interrompere\n");
while(1 == scanf("%d", &ins))
{
addsorted(&root, ins);
}
if(root)
printlist(root);
system("Pause");
return 0;
}
Solved. It is:

int ins;

Instead of:

char ins;

Jul 14 '06 #2
On 2006-07-14, Franco Perilli <li****@tin.itwrote:
I've compiled this code and no problems, but when I run the program, it
prints only the last entry i've inserted. Looks like a problem in the
sorted insertion algorithm. Can u help me plz?

#include <stdio.h>
#include <stdlib.h>

/*---data type---*/
typedef int type;
You might want to call this node_type or something more relevant to
linked lists. If you ever use this code for anything, you'll want to
differentiate between different structures.
/*---------------*/

/*---the node---*/
struct node
{
type data;
struct node *next;
};
typedef struct node Node; /* <-defines the node object */
typedef Node *NodePtr; /* <-defines the pointer to a node */
Do not hide pointers behind typedefs. That's just begging for trouble.
Reading through your code, it's confusing the crap out of me because
you have a bunch of pointers without *'s to indicate their pointerhood.
At a glance, the symbol `*' is much easier to see than `Ptr'.
/*-------------*/

/*sorted insertion*/
void addsorted(NodePtr *root, type value)
{
NodePtr newptr;
NodePtr back;
NodePtr curr;
Just do
Node *new, *prev, *cur;
if(newptr = malloc(sizeof(Node)))
newptr = malloc (sizeof *newptr) is better style and much easier to maintain.
{
newptr->data = value;
newptr->next = NULL;
back = NULL;
curr = *root;
while(curr != NULL && value curr->data)
{
back = curr;
curr = curr->next;
}
if(back == NULL) /*value is the smallest number in
the list*/
Good comment. May I recommend using 2 or 4-space tabs on Usenet, though? 8
spaces makes lines wrap and it's difficult to read.
{
newptr->next = *root;
This doesn't compile cleanly, does it? You've assigned a Node to a Node *.
Oh, wait. You've assigned to a NodePtr *, which is a Node **. See how
confusing that is?
*root = newptr;
So, (*root)->next == *root? This is a circular list, I see. Perhaps you
should have indicated that somewhere, because the code isn't exactly
self-documenting.

I'll stop here and let some compilers and pedants give you some more
detailed help; fix the typedef'd pointer and wide indentation, and I'll
happily read through the rest.

--
Andrew Poelstra <http://www.wpsoftware.net/projects/>
To email me, use "apoelstra" at the above domain.
"You people hate mathematics." -- James Harris
Jul 14 '06 #3
You might want to call this node_type or something more relevant to
linked lists. If you ever use this code for anything, you'll want to
Yes, maybe, but it's not that important
Do not hide pointers behind typedefs. That's just begging for trouble.
Reading through your code, it's confusing the crap out of me because
you have a bunch of pointers without *'s to indicate their pointerhood.
At a glance, the symbol `*' is much easier to see than `Ptr'.
I did it just to bring some higher level abstraction to the code. I
come from Java and sometimes pointers are a mistery to me, when
variables pointing to objects in memory are not :). Though i could
simply remove both the typedef, but what changes?
Good comment. May I recommend using 2 or 4-space tabs on Usenet, though? 8
spaces makes lines wrap and it's difficult to read.
Ok sorry, i'll adapt my codes before posting
newptr->next = *root;
*root = newptr;

So, (*root)->next == *root? This is a circular list, I see. Perhaps you
should have indicated that somewhere, because the code isn't exactly
self-documenting.
That is not :) Remember i'm not switching objects, i'm switching
pointers :)
Doing like this:

*------------* *-----------*
| newptr |----->| *root |----->(the rest of the list)
*------------* *-----------*

| |
\ /

*---------* *-------------*
| *root |----->| newptr |----->(the rest of the list)
*---------* *-------------*

This is very basic i think.

I'll stop here and let some compilers and pedants give you some more
detailed help; fix the typedef'd pointer and wide indentation, and I'll
happily read through the rest.
Ok ty :)

Jul 15 '06 #4

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

Similar topics

2
by: Kushal | last post by:
Hello, I am trying to build a dynamic set/list of data which should be sorted as each element is inserted. The main criteria in this list is the speed, the time it takes to insert a element,...
10
by: Der Andere | last post by:
I need to implement a sorted (ordered) list. STL has no sorted list type as far as I know. Is there a (straight) way to implement a sorted list using STL? BTW: The type of items in the list will...
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...
3
by: chai | last post by:
I am trying out a program to insert an element to a sorted list(singly linked list)without using the temporary variable.Is there a solution for this problem?
6
by: Zoro | last post by:
I come from the Delphi world and there we had a very useful component called TStringList which was a dynamic string array which had a 'sorted' property and 'duplicate' property and it was great for...
8
by: Guy | last post by:
Is there a better way to search identical elements in a sorted array list than the following: iIndex = Array.BinarySearch( m_Array, 0, m_Array.Count, aSearchedObject ); aFoundObject= m_Array;...
7
by: desktop | last post by:
In the C++ standard page 472 it says that you can construct a std::set in linear time if the constructor gets a sorted sequence of elements. But how is this possible when insert takes logarithmic...
1
by: hackerbob | last post by:
I'm trying to create a constant time event timer. Basically, a routine can set a callback to be called n ms from the current time, and the main event loop will wait until the delta between the...
1
by: Harold Howe | last post by:
Howdy all, The msdn help says this about SorteList<k,v>: "If the list is populated all at once from sorted data, SortedList is faster than SortedDictionary." My question is this: how do I...
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: 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...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
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
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,...

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.