الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

Dynamic Stack و Dynamic Queue

مغلق
بدأه {{unknown}} في 25 فبراير 2005 · 0 رد · 343 مشاهدة · في ارشيف قسم C/C++
مشاركة: واتساب X فيسبوك تيليجرام
#1

السلام عليكم.....

قمت هذة المرة بعمل كود

Dynamic Stack وأيضا كود آخر يعمل ك

Dynamic Queue..............................

سؤالي هو

بالنسبة للكود

Dynamic Stack

في الإختيار رقم 3

يقوم بعرض البيانات ولكن ليس بشكل متسلسل.

نفس المشكلة في الكود الأخر

Dynamic Queue

ولكن في الخيار رقم 4

سؤالي هو أرجو منكم ايفادي بطريقة جعل هذة الكودات تعمل على عرض البيانات بالتسلسل!!

إنني أطمع بإيفادي بالطريقة المناسبة....حيث انني دائما اتعثرعند هذة النقطة.وأرجو التأكد من البرنامج وتجربته قبل الإجابه. إذا لم يكن سؤالي واضحا فأرجو إيفادي بذلك.

وشكرا لكم

.................................................

كود Dynamic Stack

#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

/*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;
 }
 }

هذا الموضوع مغلق.

مواضيع مشابهة