globals.h

#ifndef _globals
#define _globals

#define NEXT(L) ( (L) -> next )

#define DATA(T) ( (T) -> datapointer )
#define LEFT(T) ( (T) -> left )
#define RIGHT(T) ( (T) -> right )

typedef enum { OK, ERROR } status ;
typedef enum { FALSE=0 , TRUE=1 } bool ;
typedef void *generic_ptr ;

#endif

list.c

/*****************************************************************************/
/* list.c                                                                    */
/*                                                                           */
/*****************************************************************************/

#include <stdlib.h>
#include "globals.h"
#include "list.h"

status allocate_node( list *p_L , generic_ptr data)  {


  list L = (list) malloc(sizeof(node));

  if (L == NULL) return ERROR;

  *p_L = L;
  
  DATA(L) = data;
  NEXT(L) = NULL;
  return OK;

}


void free_node(list *p_L) {

  free(*p_L);
  *p_L = NULL;

}

status init_list( list *p_L) {

  *p_L = NULL;
  return OK;

}

bool empty_list(list L) {

  return (L == NULL ) ? TRUE : FALSE;

}

status insert( list *p_L,generic_ptr data ) {

  list L;
  
  if(allocate_node(&L, data) == ERROR) return ERROR;

  NEXT(L) = *p_L;
  *p_L = L; 
  return OK;

}

status append(list *p_L, generic_ptr data) {

  list L, tmplist;
  if(allocate_node(&L, data) == ERROR) return ERROR;

  if(empty_list(*p_L) == TRUE) *p_L = L;

  else {
    for(tmplist = *p_L; NEXT(tmplist) != NULL; tmplist=NEXT(tmplist) );
    NEXT(tmplist) = L;

  }

  return OK;

}

status delete(list *p_L, generic_ptr *p_data) {

  if(empty_list(*p_L)) return ERROR;

  *p_data = DATA(*p_L);

return delete_node(p_L, * p_L);

}

status delete_node( list *p_L, list node) {

  if(empty_list(*p_L) == TRUE) return ERROR;

  if(*p_L == node) *p_L = NEXT(*p_L);

  else {
    list L;
    for(L = *p_L; L != NULL && NEXT(L) != node; L = NEXT(L));

    if (L == NULL) return ERROR;
    else NEXT(L) = NEXT(node);

  }

  free_node(&node);
  return OK;

}




status traverse( list L, status(*p_func_f) () ) {

  if(empty_list(L)) return OK;

  if( (*p_func_f)(DATA(L)) == ERROR ) return ERROR;

  else
    return traverse(NEXT(L), p_func_f);

}


status find_key( list L, generic_ptr key, int(*p_cmp_f)(), list *p_keynode ) {

  list curr = NULL;
  
  while( ( curr = list_iterator(L, curr)) != NULL) {

    if ( (*p_cmp_f) (key, DATA(curr) ) == 0 ) {

      *p_keynode = curr;
      return OK;
    }
  }
  return ERROR;
}


list list_iterator(list L, list lastreturn ) {

  return (lastreturn == NULL) ? L : NEXT(lastreturn);

}


void destroy( list *p_L, void (*p_func_f)() ) {

  if (empty_list(*p_L) == FALSE) {

    destroy( &NEXT(*p_L), p_func_f );
    
    if (p_func_f != NULL) 
	
   (*p_func_f)(DATA(*p_L));
    
    free_node(p_L);

  }
} 


bool equal(list L1 , list L2 , int (*p_cmp_f)() ){

while(L1 != NULL && L2 != NULL ){
if( (*p_cmp_f)(L1->datapointer,L2->datapointer ) == -1)
return FALSE;

else
equal(L1->next,L2->next,p_cmp_f) ; 
}


return TRUE;
}

list.h

#ifndef _list
#define _list

#include "globals.h"

typedef struct node node, *list;

struct node { generic_ptr datapointer; list next; };

extern status allocate_node( list *p_L, generic_ptr data ) ;

extern void   free_node( list *p_L ) ;

extern status init_list( list *p_L ) ;

extern bool   empty_list( list L ) ;

extern status insert( list *p_L, generic_ptr data ) ;

extern status append( list *p_L, generic_ptr data ) ;

extern status delete( list *p_L, generic_ptr *p_data ) ;

extern status delete_node( list *p_L, list node ) ;

extern status traverse( list L, status (*p_func_f) () );

extern status find_key( list L, generic_ptr key, int(*p_cmp_f) (), list *p_keynode );

extern list list_iterator( list L, list lastreturn );

extern void destroy(list *p_L, void (*p_func_f)() );

#endif

queue.c

#include <stdio.h>
#include <stdlib.h>
#include "globals.h"
#include "list.h"
#include "queue.h"

status init_queue(queue *p_Q) {

  return init_list(p_Q);
}

bool empty_queue(queue *p_Q) {
  
  return empty_list(*p_Q);
}

status qadd(queue *p_Q, generic_ptr data) {
  
  return append(p_Q, data);
}

status qremove(queue *p_Q, generic_ptr *p_data) {
  
  return delete(p_Q, p_data);
}




status qprint( queue q, status(*p_func_f) () ) {

  if(empty_list(q)) return OK;

  if( (*p_func_f)(DATA(q)) == ERROR ) return ERROR;

  else
    return traverse(NEXT(q), p_func_f);

}

queue.h

/***********************************************************************/
/* queue.h                                                             */
/***********************************************************************/

#ifndef _queue
#define _queue
 
#include "globals.h"
#include "list.h"

typedef list queue;

extern status init_queue ( queue *p_Q );

extern bool empty_queue ( queue *p_Q );

extern status qadd ( queue *p_Q, generic_ptr data );

extern status qremove( queue *p_Q, generic_ptr *p_data );

#endif

main.c

#include<stdio.h>
#include<stdlib.h>
#include "queue.h"
#include "globals.h" 






status printnum(generic_ptr n ){ 

	printf(" %d " , *(int *)n ) ; 

return OK ; 

}

status addnumber(queue *q , int n ){

	int *p =(int *) malloc(sizeof(int)) ; 
       
	if( p == NULL) return ERROR ; 

	*p = n ; 

	if( qadd( q ,(generic_ptr)p ) == ERROR ) { free(p) ; return ERROR ; } 

return OK ; 

}

status removenumber(queue *q , int *n ){

	int *temp ; 
 	 
	if( qremove( q ,(generic_ptr *)&temp ) == ERROR ) return ERROR ; 

	
	*n = *temp ; 
	free(temp) ; 


return OK ; 

}



int main(int argc , char *argv[]){


	int n , option ; 
	queue q ; 

init_queue( &q ) ; 

while(1){

	printf("Add to queue (1) : \n") ; 
	printf("Remove from queue (2) : \n") ; 
	printf("Done: (3) \n") ; 
 

	scanf("%d" , &option ) ; 

	if( option == 1 ){
	
	printf("\nWhat number do you want to add to the queue? ") ; 
	scanf("%d" ,&n ) ; 
	addnumber(&q , n) ; 

       } 


	if(option == 2){

	removenumber( &q , &n) ; 
	printf("\nThe number %d was removed from the queue.\n" , n) ; 

	}

	if( option == 3 ) break ; 

}


	printf("\n\n The queue has the following values: " ) ; 

	qprint(q,printnum) ; /*********** you need to write this function ******/ 

	printf("\n") ; 

	return 0 ; 

}

I can compile it but when i run the program and it asks me for an option and i input 1 to add a number and when i enter the number it does nothing

for example i input 89 and it does nothing until i enter another number like 6 then i asks again and shows the 3 options and when i choose 3 it shows that the 89 was in the queue but why do i have to input two numbers just to get the 89 into the queue?

Edited 5 Years Ago by rockerjhr: n/a

Maybe you can point out about where you input is so we can find it? Asking us to look through 5 files and 300 lines of code is a little much, don't you think?

printf("Add to queue (1) : \n") ; 
	printf("Remove from queue (2) : \n") ; 
	printf("Done: (3) \n") ; 
 
 
	scanf("%d" , &option ) ; 
 
	if( option == 1 ){
 
	printf("\nWhat number do you want to add to the queue? ") ; 
	scanf("%d" ,&n ) ; 
	addnumber(&q , n) ; 
 
       } 
 
 
	if(option == 2){
 
	removenumber( &q , &n) ; 
	printf("\nThe number %d was removed from the queue.\n" , n) ; 
 
	}
 
	if( option == 3 ) break ; 
 
}
 
 
	printf("\n\n The queue has the following values: " ) ; 
 
	qprint(q,printnum) ; /*********** you need to write this function ******/ 
 
	printf("\n") ; 
 
	return 0 ;

there

when i run the program and enter 1 as an option its supposed to do this

if( option == 1 ){
 
	printf("\nWhat number do you want to add to the queue? ") ; 
	scanf("%d" ,&n ) ; 
	addnumber(&q , n) ; 
 
       }

so i enter 1 and it asks me what number i want to input

i input 89 and it does nothing so i input another number say 4 and it shows me the 3 options again

when i input 3 to end it it shows that 89 was added to the queue but why do i have to enter another number after i put the number i want in the queue?

There's nothing specifically wrong whit this code that I can see. Are you attemptig to read a character somewhere else in the code?

This might give you a clue to the problem if so.

no im not attempting to read any characters just ints i know it should work but it doesnt :(


I run the program and first thing that comes up is this


Add to queue (1) :
Remove from queue (2) :
Done: (3)

my input: 1

now this comes up: What number do you want to add to the queue?

my input: 45

now nothing happens

I'd add some output statements in key places after your 2nd input to see if it's continuing properly. Print out where in the code you are and some key values.

This article has been dead for over six months. Start a new discussion instead.