enqueue and deuque funct.

This is a discussion on enqueue and deuque funct. within the C Programming forums, part of the General Programming Boards category; Hi all I'm having kind of queue problem in my program while handling queues,, basicly in my program I have ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    28

    enqueue and deuque funct.

    Hi all

    I'm having kind of queue problem in my program while handling queues,,

    basicly in my program I have 4 different quees , you can try to think like, there are 2 different cities
    and 2 different airports, and 1 takeoff and 1 landing queues an..Totally I've got 4 different linked list...
    Every take off queues consist of just 2 item, landing has got no limit...

    User fill in the queues in the begining of program by enterin two item into every queue list.
    like

    A city Take Off--> x-y
    A City Landing--> k-f

    B city take off--> p-m
    B city take off--> j-t

    After user filled in by entering char names like above,

    User enters a direction like A to B
    and

    Program Takes first element of A City's takeoff and transfers it into B city's landing,
    like so

    A to B

    A city take off--> Y
    A city Landing--> K F

    B city take off--> P M
    B city landing --> X - J- T

    while doing this I'm using tradational enqueue and dequeue functions but I'm having a problem while
    transfering A link's one element to B link...
    here is the a part of the code
    Code:
     
    
    if(flight_choice=='I');{choice=1;}
    if(flight_choice=='A');{choice=2;}
    if(flight_choice=='E');{choice=3;}
    switch(choice){
                   case 1:
                        flight_choice2=getch();
                        flight_choice2=toupper(flight_choice2);
                        printf("%c\n",flight_choice2);
                        if(flight_choice2 == 'A'){
                                          dequeue(&ist_take_head,&key1);
                                          enqueue(&ank_land_head,key1);
                                          dequeue(&ist_land_head,&key1);
                                          enqueue(&ist_take_head,key1);
                                          }
    
    void enqueue( flight** head, char key )
    {
      flight* newNode = malloc(sizeof(flight));
      newNode->key = key;
      newNode->next = NULL;
      if ( *head == NULL ) 
      {
        *head = newNode; 
      }
      else
      {
        flight* last = *head;
        while ( last->next != NULL )
          last = last->next;
        last->next = newNode;
      }
    }
    
    void dequeue(flight** head, char *key) {
      if ( *head == NULL ) 
      {
        *key = 0;
      }
      else 
      {
        *key = (*head)->key;
        *head = (*head)->next;
      }
    }
    What I dont understand is what I have to pass as a key,while doing dequeue function the key(which is the name of element in the linked list),
    I pass into that function.

    I know it's not a clear explanation but I got totally confused so that I cant excatly explain my question .

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Firstly, revise your if statements
    Code:
    if(flight_choice=='I');{choice=1;}
    if(flight_choice=='A');{choice=2;}
    if(flight_choice=='E');{choice=3;}
    These in-fact do nothing (the trailing ; indicates end-of-statement), I think you mean
    Code:
    if(flight_choice=='I')
        choice=1;
    if(flight_choice=='A')
        choice=2;
    if(flight_choice=='E')
        choice=3;
    And then again that can be clean'd up with the use of a switch

    • Getch is also non-standard.
    • There is no corisponding free() for your malloc() (this is in-fact a memory leak)
    • ...

  3. #3
    Registered User
    Join Date
    Jul 2006
    Posts
    5
    hocam bu data ödevi deil mi yaa ?

    burda buna cevap çıkması cok zor bence

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    28
    Quote Originally Posted by ercumania View Post
    hocam bu data ödevi deil mi yaa ?

    burda buna cevap çıkması cok zor bence
    problem's been solved...thanx alot...

    Cikti

    ugrastirdi ama biraz

  5. #5
    Registered User
    Join Date
    Jul 2006
    Posts
    5
    Quote Originally Posted by m0ntana View Post
    problem's been solved...thanx alot...

    Cikti

    ugrastirdi ama biraz
    çok az tiyo versen ?

  6. #6
    Registered User
    Join Date
    Mar 2007
    Posts
    28
    Quote Originally Posted by ercumania View Post
    çok az tiyo versen ?

    sorry for the answer in another language rather than english..

    Her şehir için 1 take-off 1 landing head pointer tanımlıyorsun, totalde 6 tane oluyor...

    Her birini farklı bir linked list olarak düşün, ve kullanıcıdan her şehir için 2 takeoff 2 landing uçağı alıyorsun, yani her bir queue de 2 şer tane item olmuş oluyor.

    Bunu hazır fonksyonlarla yapıyosun zaten

    Code:
    void enqueue( flight** head, char key )
    {
      flight* newNode = malloc(sizeof(flight));
      newNode->key = key;
      newNode->next = NULL;
      if ( *head == NULL ) 
      {
        *head = newNode; 
      }
      else
      {
        flight* last = *head;
        while ( last->next != NULL )
          last = last->next;
        last->next = newNode;
      }
    }
    buda dequeue part ı

    Code:
    void dequeue(flight** head, char *key) {
      if ( *head == NULL ) 
      {
        *key = 0;
      }
      else 
      {
        *key = (*head)->key;
        *head = (*head)->next;
      }
    }

    aldığın her char ı enquee e yolla aynen o kuyruğa atıyo zaten,
    asıl mesele ondan sonra zaten switch case le kullanıcıdan aldığı direction ı kontrol etmen lazım
    I A- E I gibi onuda yukadaki gibi yaptım ben....

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21