Sorting Not In Order

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2005
Posts: 7
Reputation: {{unknown}} is an unknown quantity at this point 
Solved Threads: 0
{{unknown}} {{unknown}} is offline Offline
Newbie Poster

Sorting Not In Order

 
0
  #1
Feb 24th, 2005
Hi..
I made two codes in C language. One of them is a Dynamic stack and the other is Dynamic queue. They are working correctly but I had one problem In both of them.
The option 3 in Dynamic Stack code doesn't display the topic in order?
This problem is the same thing applies to the other code (Dynamic Queue) in option 4?
So can anyone please help me how can I make this option work to display the details in order? Thanks in advance.
--------------------------
Dynamic Stack Code

  1. #include<stdio.h>
  2. #include<alloc.h>
  3. struct data_type
  4. {
  5. int rollno;
  6. char name[20];
  7. char topic[30];
  8.  
  9. struct data_type *next;
  10. };
  11. struct data_type *top = NULL;
  12. struct data_type *origin = NULL;
  13.  
  14. void push(struct data_type);
  15. struct data_type pop(void);
  16. void display (void);
  17. void disno (void);
  18.  
  19. main()
  20. {
  21. int opt;
  22. struct data_type tempdata;
  23. clrscr();
  24. do
  25. {
  26. clrscr();
  27. printf("\n\n\t\t\t\tDynamic Stack \n");
  28. printf("\n\n\t\t\t\tT H E M E N U \n");
  29. printf("\t\t-------------------------------------------\n");
  30. printf("\n\t\t\t 1- Add a Topic");
  31. printf("\n\t\t\t 2- Remove a Topic");
  32. printf("\n\t\t\t 3- Display All In Ascending Order");
  33. printf("\n\t\t\t 4- Display Topics Based On Roll Number");
  34. printf("\n\t\t\t 5- Quit");
  35. printf("\n\t\t-------------------------------------------\n");
  36. printf("\n\n\t\t\t Enter your choice please : ");
  37. scanf("%d",&opt);
  38. switch(opt)
  39.  
  40. {
  41. case 1:
  42. {
  43. clrscr();
  44. printf("\n\t\t\t Enter roll number: ");
  45. scanf("%d",&tempdata.rollno);
  46. fflush(stdin);
  47. printf("\n\t\t\t Enter Name: ");
  48. gets(tempdata.name);
  49. fflush(stdin);
  50. printf("\n\t\t\t Enter Topic: ");
  51. gets(tempdata.topic);
  52. tempdata.next = NULL;
  53. push(tempdata);
  54. break;
  55. }
  56.  
  57. case 2:
  58. {
  59. clrscr();
  60. if(top==NULL)
  61. {
  62. printf("\n\n\t\t\t---> Stack Is Empty <---");
  63. getch();
  64. }
  65. else
  66. {
  67. clrscr();
  68. tempdata=pop();
  69. printf("\n\t\t\t The Removed element is: \n");
  70. printf("\n\t\t\t Roll Number is:");
  71. printf("%d",tempdata.rollno);
  72. printf("\n\t\t\t Name is:");
  73. printf("%s",tempdata.name);
  74. printf("\n\t\t\t Topic is:");
  75. printf("%s",tempdata.topic);
  76. getche();
  77.  
  78. }
  79. break;
  80. }
  81.  
  82. case 3:
  83. {
  84. clrscr();
  85. display();
  86. break;
  87. }
  88.  
  89.  
  90. case 4:
  91. { clrscr();
  92. disno();
  93. break;
  94. }
  95.  
  96. case 5:
  97. {
  98. clrscr();
  99. printf("\n\n\n\n\n\n\n\n\t\t\t Program Is Over \n");
  100. getch();
  101. break;
  102. }
  103.  
  104. default:
  105. {
  106. clrscr();
  107. printf("\n\n\t\t\t--> Wrong Option!!! Try again <--\n");
  108. getch();
  109. }
  110. }
  111. }
  112. while(opt!=5);
  113. }
  114.  
  115. void push (struct data_type data)
  116. {
  117. struct data_type *node = NULL;
  118. node = (struct data_type*) malloc(sizeof(struct data_type));
  119. clrscr();
  120. node->rollno = data.rollno;
  121. strcpy(node->name,data.name);
  122. strcpy(node->topic,data.topic);
  123. node->next =NULL;
  124. if(top==NULL)
  125. {
  126. origin=node;
  127. top=node;
  128. }
  129. else
  130. {
  131. top->next =node;
  132. top=node;
  133. }
  134. }
  135.  
  136. struct data_type pop(void)
  137. {
  138. struct data_type temp;
  139. struct data_type *tempptr;
  140. temp= *top;
  141. tempptr= origin;
  142. if(tempptr==top)
  143. {
  144. free((void*)top);
  145. top = origin=NULL;
  146. }
  147. else
  148. {
  149. while (tempptr-> next !=top)
  150. tempptr=tempptr-> next;
  151. tempptr->next = NULL;
  152. free ((void*)top);
  153. top=tempptr;
  154. }
  155. return temp;
  156. }
  157.  
  158. void display(void)
  159. {
  160. struct data_type *ptr;
  161. struct data_type data;
  162. ptr=origin;
  163. while(ptr!=NULL)
  164. {
  165. data=*ptr;
  166. printf("\n\n\n\t\t\t Roll Number: %d", data.rollno);
  167. printf("\n\t\t\t Name : %s",data.name);
  168. printf("\n\t\t\t Topic : %s", data.topic);
  169. ptr=ptr->next;
  170. }
  171. getche();
  172. clrscr();
  173.  
  174. }
  175. void disno(void)
  176. {
  177. int rollno;
  178. int i=0;
  179. struct data_type *ptr;
  180. struct data_type data;
  181. ptr = origin;
  182. printf("\n\n\t\t\t Enter Roll Number: ");
  183. scanf(" %d",&rollno);
  184. while(ptr != NULL)
  185.  
  186. {
  187. data=*ptr;
  188. if(rollno == data.rollno)
  189. {
  190.  
  191. printf("\n\t\t\t Name : %s",data.name);
  192. printf("\n\t\t\t Topic :%s",data.topic);
  193. i = 1;
  194. }
  195. ptr = ptr -> next;
  196. getch();
  197. }
  198.  
  199. if (i==0)
  200. printf("\n\n\n\t\t\t This Number not available\n");
  201. getch();
  202. }

-------------------------

Dynamic Queue Code


  1. /*Dynamic Queue Programe*/
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <alloc.h>
  5.  
  6. struct msg_type
  7. {
  8. int aircraft_no;
  9. char name [30];
  10. char type [30];
  11.  
  12. struct msg_type*next;
  13. };
  14. struct msg_type *front=NULL;
  15. struct msg_type *rear=NULL;
  16.  
  17. void add (struct msg_type);
  18. struct msg_type remove1();
  19. void find (void);
  20. void display (void);
  21. void exit (void);
  22. main()
  23. {
  24. int option=0;
  25. struct msg_type tempmsg;
  26. do
  27. {
  28. clrscr();
  29. printf("\n\n\t\t\t(DYNAMIC QUEUE)\n");
  30. printf("\n\t\t(MENU)\n");
  31. printf("================================\n");
  32. printf("1.Add Aircraft Detail\n");
  33. printf("2.Remove Email\n");
  34. printf("3.Find details by Aircraft number\n");
  35. printf("4.List all Display\n");
  36. printf("5.Exit\n");
  37. printf("================================\n");
  38. printf("\n");
  39. printf("Enter your option\n");
  40. scanf("%d", &option);
  41. switch (option)
  42. {
  43. case 1: /*Add elements in the Dynamic Queue*/
  44. printf("Enter the Aircraft number:\n");
  45. fflush(stdin);
  46. scanf("%d",&tempmsg.aircraft_no);
  47. printf("Enter the name:\n");
  48. fflush(stdin);
  49. gets(tempmsg.name); /*scanf("%s"tempmsg.name);*/
  50. printf("Enter the Type:\n");
  51. gets(tempmsg.type); /*scanf ("%s",tempmsg.message);*/
  52. tempmsg.next=NULL;
  53. add (tempmsg);
  54. getche();
  55. break;
  56. case 2:
  57. if (front==NULL) /*Remove elements from the Dynamic Queue*/
  58. printf ("Queue is empty\n");
  59. else
  60. {
  61. tempmsg=remove1();
  62. printf("The Removed Element is:-\n");
  63. printf("--------------------------\n");
  64. printf("Aircraft Number :%d\n",tempmsg.aircraft_no);
  65. printf("Name :%s\n",tempmsg.name);
  66. printf(" :%s\n",tempmsg.type);
  67. printf("\n");
  68. }
  69. getche();
  70. break;
  71. case 3:
  72. find(); /*Find the arrival number from the Dynamic Queue*/
  73. getche();
  74. break;
  75. case 4:
  76. display(); /*Display all elements in the Dynamic Queue*/
  77. getche();
  78. break;
  79. case 5:
  80. exit();
  81. printf("Thank You, Porgrame is Over\n");
  82. getche();
  83. break;
  84. default:
  85. printf("Wrong option,Please choose from Menu\n"); /*if user chose wrong option*/
  86. getche();
  87. break;
  88. }
  89. }
  90. while (option!=5);
  91. }
  92.  
  93. /*Main Programe Function started here*/
  94.  
  95. void add (struct msg_type msg)
  96. {
  97. struct msg_type*node=NULL;
  98. node=(struct msg_type*)malloc(sizeof(struct msg_type));
  99. node->aircraft_no=msg.aircraft_no;
  100. strcpy(node->name,msg.name);
  101. strcpy(node->type,msg.type);
  102. node->next=NULL;
  103. if (front==NULL)
  104. {
  105. front=node;
  106. rear=node;
  107. }
  108. else
  109. {
  110. rear->next=node;
  111. rear=node;
  112. }
  113. }
  114.  
  115. struct msg_type remove1(void) /*Remove elements from the Dynamic Queue*/
  116. {
  117. struct msg_type temp;
  118. struct msg_type *tempptr;
  119. temp=*front;
  120. tempptr=front;
  121. if(tempptr==front)
  122. front=front->next;
  123. free((void*)front);
  124. return temp;
  125. }
  126.  
  127. void find (void) /*Find arrival number from the Dynamic Queue*/
  128. {
  129. int found=0,aircraft;
  130. struct msg_type *ptr;
  131. struct msg_type msg;
  132. ptr=front;
  133. printf("Find the Aircraft Number:\n");
  134. scanf("%d",&aircraft);
  135. while (ptr!=NULL)
  136. {
  137. msg=*ptr;
  138. if(aircraft==msg.aircraft_no) /*compyring if the arrival is equal to arrival number in the Dynamic queue*/
  139. {
  140. printf("\n");
  141. printf("The Aircraft number is available in the Dynamic Queue\n");
  142. printf("\n\%s\t%s\t\t\t%s\n","Aircraft_No","Name","Type");
  143. printf("\-----------------------------------------------------------");
  144. printf("\n%d\t\t%s\t\t%s\n",msg.aircraft_no,msg.name,msg.type);
  145. printf("\n");
  146. found=1;
  147. }
  148. ptr=ptr->next;
  149. }
  150. if(!found)
  151. printf ("\n Aircraft Number is not avilable\n");
  152. }
  153.  
  154. void display (void) /*Display all elements in the Dynamic Queue*/
  155. {
  156. struct msg_type*ptr;
  157. struct msg_type msg;
  158. ptr=front;
  159. printf("\n%s\t%s\t\t\t%s","aircraft_No","Name","Type");
  160. printf("\n----------------------------------------------------------");
  161. while(ptr!=NULL)
  162. {
  163. msg=*ptr;
  164. printf("\n%d\t\t%s\t\t%s\n",msg.aircraft_no,msg.name,msg.type);
  165. ptr=ptr->next;
  166. }
  167. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,433
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 249
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Sorting Not In Order

 
0
  #2
Feb 25th, 2005
Aaaaaah!
  1. main()
  2.  
  3. clrscr();
  4. printf("\n\t\t\t Enter roll number: ");
  5. scanf("%d",&tempdata.rollno);
  6. fflush(stdin);
  7. printf("\n\t\t\t Enter Name: ");
  8. gets(tempdata.name);
  9. fflush(stdin);
  10. printf("\n\t\t\t Enter Topic: ");
  11. gets(tempdata.topic);
  12.  
  13. getch();
Burn the book (or teacher) you have been learning from.

gets()fflush(stdin)scanf()clrscr()You're not quite there, but let's nip this in the bud:

main()I'd recommend wandering through the rest of these FAQs as well.
  1. #include<alloc.h>
Now that's old.


Sorry, I'd end up rewriting it completely if I tried to help you out.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC