| | |
Sorting Not In Order
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jan 2005
Posts: 7
Reputation:
Solved Threads: 0
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
-------------------------
Dynamic Queue Code
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
C Syntax (Toggle Plain Text)
#include<stdio.h> #include<alloc.h> struct data_type { int rollno; char name[20]; char topic[30]; struct data_type *next; }; struct data_type *top = NULL; struct data_type *origin = NULL; void push(struct data_type); struct data_type pop(void); void display (void); void disno (void); main() { int opt; struct data_type tempdata; clrscr(); do { clrscr(); printf("\n\n\t\t\t\tDynamic Stack \n"); printf("\n\n\t\t\t\tT H E M E N U \n"); printf("\t\t-------------------------------------------\n"); printf("\n\t\t\t 1- Add a Topic"); printf("\n\t\t\t 2- Remove a Topic"); printf("\n\t\t\t 3- Display All In Ascending Order"); printf("\n\t\t\t 4- Display Topics Based On Roll Number"); printf("\n\t\t\t 5- Quit"); printf("\n\t\t-------------------------------------------\n"); printf("\n\n\t\t\t Enter your choice please : "); scanf("%d",&opt); switch(opt) { case 1: { clrscr(); printf("\n\t\t\t Enter roll number: "); scanf("%d",&tempdata.rollno); fflush(stdin); printf("\n\t\t\t Enter Name: "); gets(tempdata.name); fflush(stdin); printf("\n\t\t\t Enter Topic: "); gets(tempdata.topic); tempdata.next = NULL; push(tempdata); break; } case 2: { clrscr(); if(top==NULL) { printf("\n\n\t\t\t---> Stack Is Empty <---"); getch(); } else { clrscr(); tempdata=pop(); printf("\n\t\t\t The Removed element is: \n"); printf("\n\t\t\t Roll Number is:"); printf("%d",tempdata.rollno); printf("\n\t\t\t Name is:"); printf("%s",tempdata.name); printf("\n\t\t\t Topic is:"); printf("%s",tempdata.topic); getche(); } break; } case 3: { clrscr(); display(); break; } case 4: { clrscr(); disno(); break; } case 5: { clrscr(); printf("\n\n\n\n\n\n\n\n\t\t\t Program Is Over \n"); getch(); break; } default: { clrscr(); printf("\n\n\t\t\t--> Wrong Option!!! Try again <--\n"); getch(); } } } while(opt!=5); } void push (struct data_type data) { struct data_type *node = NULL; node = (struct data_type*) malloc(sizeof(struct data_type)); clrscr(); node->rollno = data.rollno; strcpy(node->name,data.name); strcpy(node->topic,data.topic); node->next =NULL; if(top==NULL) { origin=node; top=node; } else { top->next =node; top=node; } } struct data_type pop(void) { struct data_type temp; struct data_type *tempptr; temp= *top; tempptr= origin; if(tempptr==top) { free((void*)top); top = origin=NULL; } else { while (tempptr-> next !=top) tempptr=tempptr-> next; tempptr->next = NULL; free ((void*)top); top=tempptr; } return temp; } void display(void) { struct data_type *ptr; struct data_type data; ptr=origin; while(ptr!=NULL) { data=*ptr; printf("\n\n\n\t\t\t Roll Number: %d", data.rollno); printf("\n\t\t\t Name : %s",data.name); printf("\n\t\t\t Topic : %s", data.topic); ptr=ptr->next; } getche(); clrscr(); } void disno(void) { int rollno; int i=0; struct data_type *ptr; struct data_type data; ptr = origin; printf("\n\n\t\t\t Enter Roll Number: "); scanf(" %d",&rollno); while(ptr != NULL) { data=*ptr; if(rollno == data.rollno) { printf("\n\t\t\t Name : %s",data.name); printf("\n\t\t\t Topic :%s",data.topic); i = 1; } ptr = ptr -> next; getch(); } if (i==0) printf("\n\n\n\t\t\t This Number not available\n"); getch(); }
-------------------------
Dynamic Queue Code
C Syntax (Toggle Plain Text)
/*Dynamic Queue Programe*/ #include <stdio.h> #include <string.h> #include <alloc.h> struct msg_type { int aircraft_no; char name [30]; char type [30]; struct msg_type*next; }; struct msg_type *front=NULL; struct msg_type *rear=NULL; void add (struct msg_type); struct msg_type remove1(); void find (void); void display (void); void exit (void); main() { int option=0; struct msg_type tempmsg; do { clrscr(); printf("\n\n\t\t\t(DYNAMIC QUEUE)\n"); printf("\n\t\t(MENU)\n"); printf("================================\n"); printf("1.Add Aircraft Detail\n"); printf("2.Remove Email\n"); printf("3.Find details by Aircraft number\n"); printf("4.List all Display\n"); printf("5.Exit\n"); printf("================================\n"); printf("\n"); printf("Enter your option\n"); scanf("%d", &option); switch (option) { case 1: /*Add elements in the Dynamic Queue*/ printf("Enter the Aircraft number:\n"); fflush(stdin); scanf("%d",&tempmsg.aircraft_no); printf("Enter the name:\n"); fflush(stdin); gets(tempmsg.name); /*scanf("%s"tempmsg.name);*/ printf("Enter the Type:\n"); gets(tempmsg.type); /*scanf ("%s",tempmsg.message);*/ tempmsg.next=NULL; add (tempmsg); getche(); break; case 2: if (front==NULL) /*Remove elements from the Dynamic Queue*/ printf ("Queue is empty\n"); else { tempmsg=remove1(); printf("The Removed Element is:-\n"); printf("--------------------------\n"); printf("Aircraft Number :%d\n",tempmsg.aircraft_no); printf("Name :%s\n",tempmsg.name); printf(" :%s\n",tempmsg.type); printf("\n"); } getche(); break; case 3: find(); /*Find the arrival number from the Dynamic Queue*/ getche(); break; case 4: display(); /*Display all elements in the Dynamic Queue*/ getche(); break; case 5: exit(); printf("Thank You, Porgrame is Over\n"); getche(); break; default: printf("Wrong option,Please choose from Menu\n"); /*if user chose wrong option*/ getche(); break; } } while (option!=5); } /*Main Programe Function started here*/ void add (struct msg_type msg) { struct msg_type*node=NULL; node=(struct msg_type*)malloc(sizeof(struct msg_type)); node->aircraft_no=msg.aircraft_no; strcpy(node->name,msg.name); strcpy(node->type,msg.type); node->next=NULL; if (front==NULL) { front=node; rear=node; } else { rear->next=node; rear=node; } } struct msg_type remove1(void) /*Remove elements from the Dynamic Queue*/ { struct msg_type temp; struct msg_type *tempptr; temp=*front; tempptr=front; if(tempptr==front) front=front->next; free((void*)front); return temp; } void find (void) /*Find arrival number from the Dynamic Queue*/ { int found=0,aircraft; struct msg_type *ptr; struct msg_type msg; ptr=front; printf("Find the Aircraft Number:\n"); scanf("%d",&aircraft); while (ptr!=NULL) { msg=*ptr; if(aircraft==msg.aircraft_no) /*compyring if the arrival is equal to arrival number in the Dynamic queue*/ { printf("\n"); printf("The Aircraft number is available in the Dynamic Queue\n"); printf("\n\%s\t%s\t\t\t%s\n","Aircraft_No","Name","Type"); printf("\-----------------------------------------------------------"); printf("\n%d\t\t%s\t\t%s\n",msg.aircraft_no,msg.name,msg.type); printf("\n"); found=1; } ptr=ptr->next; } if(!found) printf ("\n Aircraft Number is not avilable\n"); } void display (void) /*Display all elements in the Dynamic Queue*/ { struct msg_type*ptr; struct msg_type msg; ptr=front; printf("\n%s\t%s\t\t\t%s","aircraft_No","Name","Type"); printf("\n----------------------------------------------------------"); while(ptr!=NULL) { msg=*ptr; printf("\n%d\t\t%s\t\t%s\n",msg.aircraft_no,msg.name,msg.type); ptr=ptr->next; } }
Aaaaaah! Burn the book (or teacher) you have been learning from.
gets()
main() Now that's old.
Sorry, I'd end up rewriting it completely if I tried to help you out.
C Syntax (Toggle Plain Text)
main() clrscr(); printf("\n\t\t\t Enter roll number: "); scanf("%d",&tempdata.rollno); fflush(stdin); printf("\n\t\t\t Enter Name: "); gets(tempdata.name); fflush(stdin); printf("\n\t\t\t Enter Topic: "); gets(tempdata.topic); getch();
gets()
- http://www.eskimo.com/~scs/C-faq/q12.23.html
- http://faq.cprogramming.com/cgi-bin/...&id=1043284385
- buffer overflow
- http://www.eskimo.com/~scs/C-faq/q12.26.html
- http://faq.cprogramming.com/cgi-bin/...wer=1044873249
- http://faq.cprogramming.com/cgi-bin/...&id=1043284351
main()
- http://www.eskimo.com/~scs/C-faq/q11.12.html
- http://faq.cprogramming.com/cgi-bin/...&id=1043284376
- http://www.research.att.com/~bs/bs_faq2.html#void-main
- http://www.cpp-home.com/forum/viewtopic.php?t=266
C Syntax (Toggle Plain Text)
#include<alloc.h>
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
![]() |
Similar Threads
- To arrange Array Members in ascending order ! (Java)
- 2 Programs - Using structs-I/O-Incorrect Data (C++)
- okay, why is this not working?????? (C++)
- Output Error (C++)
- C Program where gets(), getchar(), are not working. (C)
- Crystal report (VB.NET)
- password problems (C#)
- Vectors, Functions, and Sorting... oh my! (C++)
Other Threads in the C Forum
- Previous Thread: static problem
- Next Thread: C language problem
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays bash binarysearch centimeter char convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic fflush file fork frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hardware highest homework i/o ide inches infiniteloop initialization interest kilometer km lazy linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix meter microsoft motherboard multi mysql open opendocumentformat opensource openwebfoundation owf pattern pdf performance pointer pointers posix power problem probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling segmentationfault send shape single socketprograming socketprogramming spoonfeeding stack standard strchr string strings structures suggestions system systemcall test testautomation unix user voidmain() wab win32api windows.h






