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

#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

/*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!

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();

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.

#include<alloc.h>

Now that's old.


Sorry, I'd end up rewriting it completely if I tried to help you out.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.