Thread: Menu problem involving structs

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    4

    Menu problem involving structs

    I am currently doing a Queue program a using structs and linked lists. Most of it works fine however the menu used to drive it keeps having runtime errors when i try to print the queue.
    Am I doing something wrong?

    functions

    Code:
    CTaxi_addTaxi(int TaxiReg)
    {
    	struct node *temp,*r;
    	temp= (struct node *)malloc(sizeof(struct node));
    	temp->data=TaxiReg;
    	r=(struct node *)p;
    
    	if (p == NULL)
    	{
    		p=temp;
    		p->next =NULL;
    	}
    	else
    	{
    		while( r->next != NULL)
    		r=r->next;
    		r->next =temp;
    		r=temp;
    		r->next=NULL;
    	}
    }
    
    CTaxiDelete(char taxi)
    {
    	struct node *temp, *m;
    	temp=p;
    	while(temp!=NULL)
    	{
    		if(temp->data==taxi)
    		{
    			if(temp==p)
    			{
    				p=temp->next;
    				free(temp);
    				return;
    			}
    			else
    			{
    				m->next=temp->next;
    				free(temp);
    				return;
    			}
    		}
    		else
    		{
    			m=temp;
    			temp= temp->next;
    		}
    	}
    }
    
    CTaxi_printQueue(struct node *r)
    {
    	r=p;
    	if(r==NULL)
    	{
    		printf("Taxi Rank is currently empty!");
    		return;
    	}
    
    	while(r!=NULL)
    	{
    		printf(" -> %d ",r->data);
    		r=r->next;
    	}
    	printf("/n");
    }
    menu

    Code:
    main()
    {
    	int menuChoice = 0;
    	p=NULL;
    	while(menuChoice != 4)
    	{
    		printf("1.Add Taxi\n");
    		printf("2.Delete Taxi\n");
    		printf("3.Print current taxi queue\n");
    		printf("4.Exit\n");
    		printf("Enter a choice from the above menu:");
    		scanf("%d",&menuChoice);
    		
    		switch(menuChoice)
    		{
    			case 1:
    			{
    				int TaxiReg;
    				printf("Please enter a reg no :\n");
    				scanf("%d",&TaxiReg);
    				CTaxi_addTaxi(TaxiReg);
    				break;
    			}
    			
    			case 2:
    			{
    				int TaxiReg;
    				printf("Please enter a reg no to delete :\n");
    				scanf("%d",&TaxiReg);
    				CTaxiDelete(TaxiReg);
    				break;
    			}
    			
    			case 3:
    			{
    				struct node *n;
    				printf("The Current taxi queue is:\n");
    				CTaxi_printQueue(n);
    				break;
    			}
    			
    			case 4:
    			{
    				menuChoice =4;
    				return;
    			}
    		}
    	}
    }
    Also is there a way to search for a specific node in the list? So if I wanted to find a particular taxi entered by the user it will display it.

    Thanks for any help/suggestions given
    Last edited by NyHoK; 03-31-2009 at 07:47 AM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Perhaps we need to see the "addQueue" function?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    4
    My bad, edited the first post again

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    When dealing with a queue, it is much simpler to have two variables, one representing the "head" of the queue, and one for the "tail" (tail where things are entered, head is where things are taken off).

    I'm sure someone else will spot it, but I can't actually see anything wrong with teh code.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    4
    I have been staring at it for ages and can't figure it out. Any idea on how to implement searches on linked lists?

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by NyHoK View Post
    I have been staring at it for ages and can't figure it out. Any idea on how to implement searches on linked lists?

    Something like this:
    Code:
    node *p;
    for(p = head; p; p = p->next)
    {
       if (p->member == what_you_search_for)
         break;
    }
    if (p) ... we found something.
    There is no other way than to walk along the list and check each item.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Menu Problem
    By vopo in forum Windows Programming
    Replies: 4
    Last Post: 10-06-2007, 07:57 PM
  2. MDI and MENU Problem
    By Marc in forum Windows Programming
    Replies: 3
    Last Post: 02-21-2004, 06:59 PM
  3. menu check problem
    By SuperNewbie in forum Windows Programming
    Replies: 5
    Last Post: 11-22-2003, 06:34 PM
  4. A rather unique problem, involving the good ole gb
    By Scourfish in forum Game Programming
    Replies: 2
    Last Post: 09-09-2001, 09:00 PM
  5. Menu problem
    By morbuz in forum Windows Programming
    Replies: 5
    Last Post: 09-09-2001, 06:10 AM