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

error in the sorting code it removes some output from the linked list

error in the sorting code it removes inputs in the linked list when the p->data > q->data

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2.  
  3. struct node{
  4. int data;
  5. node* next;
  6. };
  7.  
  8. node* insert (node*list, int x)
  9. {node *m=new(node);
  10. node*q=list;
  11. node*p=new(node);
  12. p->data=x;
  13.  
  14. if(q==NULL)
  15. {p->next=list;
  16. list=p;
  17. return list;}
  18.  
  19. else if(p->data > q->data)
  20. { while(q!=NULL){
  21. m=q->next;
  22. q->next=p;
  23. p->next=m;
  24. q=q->next;
  25. }
  26. }
  27.  
  28. else
  29. {
  30.  
  31. p->next=q;
  32. list=p;
  33. q=q->next;
  34.  
  35. return list;
  36. }
  37. return list;}
  38.  
  39.  
  40. void display (node*list)
  41. {node*p=list;
  42. while (p!=NULL)
  43. {printf("%d ",p->data);
  44. p=p->next;
  45. }}
  46.  
  47. int main()
  48. {node*list=NULL;
  49.  
  50. list=insert(list,5);
  51. list=insert(list,4);
  52. list=insert(list,3);
  53. list=insert(list,9);
  54.  
  55.  
  56.  
  57. printf("The sorted linked list=\n");
  58. display(list);
  59.  
  60. return 0;
  61.  
  62. }
Apr 9 '11 #1

✓ answered by donbock

Suppose a list were a node (with data field ignored) rather than a node pointer. Doing so would make the insert function a little bit simpler and the display function a little bit more complicated.

Suppose you had a function that creates a new node and connects it into the list immediately after the node specified as an argument.
Expand|Select|Wrap|Line Numbers
  1. node *insertAfter(node *priorNode, int x);
Then all your insert function has to do is traverse the list looking for where the new node should go and then call insertAfter.

By the way, your program should fail gracefully if it can't allocate the new mode because no memory is available. That's the reason I suggested insertAfter return a value (the address of the new node) so that the caller can recognize an out-of-memory failure.

6 1768
donbock
2,426 Expert 2GB
Please describe in more detail what goes wrong.

By the way, the definition of m has an initializer that creates a new node. You unconditionally discard this new node. I don't know if this is a memory leak, but it is certainly unnecessary.
Apr 10 '11 #2
when i run the program the output is 3 9
it removes the first two numbers 4 & 5
Apr 10 '11 #3
there is some thing goes wrong in this condition
Expand|Select|Wrap|Line Numbers
  1. else if(p->data > q->data)
  2. { while(q!=NULL){
  3. m=q->next;
  4. q->next=p;
  5. p->next=m;
  6. q=q->next;
  7. }
  8. }
Apr 10 '11 #4
donbock
2,426 Expert 2GB
The elsif block inserts node p in the list over and over again.
Apr 10 '11 #5
what can i do ?? to solve it.......
can u explain ??
Apr 10 '11 #6
donbock
2,426 Expert 2GB
Suppose a list were a node (with data field ignored) rather than a node pointer. Doing so would make the insert function a little bit simpler and the display function a little bit more complicated.

Suppose you had a function that creates a new node and connects it into the list immediately after the node specified as an argument.
Expand|Select|Wrap|Line Numbers
  1. node *insertAfter(node *priorNode, int x);
Then all your insert function has to do is traverse the list looking for where the new node should go and then call insertAfter.

By the way, your program should fail gracefully if it can't allocate the new mode because no memory is available. That's the reason I suggested insertAfter return a value (the address of the new node) so that the caller can recognize an out-of-memory failure.
Apr 11 '11 #7

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

Similar topics

3
by: Jeff Cheng | last post by:
Can anybody help to trace the runtime error for the following code about sub-Linked list. Thank you, # include <stdio.h> # include <stdlib.h> typedef char *CategoryType;
3
by: darth | last post by:
Does anyone have heap sorting with queue(doublely linked list) in C? I have heap sort with array but with queue, it's somewhat hard.
32
by: Clunixchit | last post by:
How can i read lines of a file and place each line read in an array? for exemple; array=line1 array=line2 ...
3
by: chellappa | last post by:
hi this simple sorting , but it not running...please correect error for sorting using pointer or linked list sorting , i did value sorting in linkedlist please correct error #include<stdio.h>...
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...
6
by: oskar | last post by:
Hello everyone. I have a problem with my program... and i kinda dunno what to do.. everything seems to work ok, but i'm getting corrupted double-linked list error =\. *** glibc detected ***...
2
by: waceys | last post by:
hello everybody i need some body help me to make code for a linked list and arrays build stack and queue such that when i push an element in the stack i can dequeue it first from the queue and when...
1
by: theeverdead | last post by:
Ok I have a file in it is a record of a persons first and last name. Format is like: Trevor Johnson Kevin Smith Allan Harris I need to read that file into program and then turn it into a linked...
1
by: crimson08 | last post by:
does anyone know how to use radix sorting in a linked list???
4
by: phe2003 | last post by:
Hi All, I've been testing some extremely simple code about a linked list. What I keep doing is actually writing some parts of a linked list and testing them. Below are my codes:...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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
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...

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.