Click here to Skip to main content
16,004,887 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: #include problems Pin
Sreekanth Muralidharan7-Nov-04 23:24
Sreekanth Muralidharan7-Nov-04 23:24 
GeneralRe: #include problems Pin
vilmer8-Nov-04 3:09
vilmer8-Nov-04 3:09 
GeneralLinked list headache in VC++. Please help Pin
Sreekanth Muralidharan7-Nov-04 22:03
Sreekanth Muralidharan7-Nov-04 22:03 
GeneralRe: Linked list headache in VC++. Please help Pin
Uwe Keim7-Nov-04 22:35
sitebuilderUwe Keim7-Nov-04 22:35 
GeneralRe: Linked list headache in VC++. Please help Pin
Sreekanth Muralidharan7-Nov-04 23:17
Sreekanth Muralidharan7-Nov-04 23:17 
GeneralRe: Linked list headache in VC++. Please help Pin
FearlessBurner7-Nov-04 23:37
FearlessBurner7-Nov-04 23:37 
GeneralThanks to all... Pin
Sreekanth Muralidharan8-Nov-04 18:04
Sreekanth Muralidharan8-Nov-04 18:04 
GeneralRe: Linked list headache in VC++. Please help Pin
John R. Shaw8-Nov-04 12:40
John R. Shaw8-Nov-04 12:40 
1) This code is totaly written in C.
2) You are using uninitialised variables (ONE CAUSE OF EXCEPTION).
3) You are passing uneeded queue pointer arguments. Every function can see the global head variable and since you are only using one list you do not need to pass pointer arround.
4) You should be using a loop in the menu() function, instead of recursion, to get usere input.
5) You have variable that are not needed or actualy used.
6) You can use a temporary queue pointer in the function that need access to the queue.

I tried to point out as many problems as I could in your current code (without rewring it).

#include <process.h>
#include <stdio.h>
#include <stdlib.h>

struct queuelist /* THIS IS 1 NODE IN A LIST - NOT A LIST */
{
	int intData;
	int valid;   /* WHY IS THIS EVEN NEEDED? THIS JUST WASTE MEMORY! */
	struct queuelist* next;
	
};
struct queuelist *head, *tail; /* UNITIALISED GLOBAL VARIABLES */
/* IF YOU WANT THEM TO SELF INTIALISE TO NULL THEN USE STATIC KEYWORD
static struct queuelist *head, *tail;
*/

int AddToQueue(struct queuelist*,int);	/* Prototype does not match function */
/* ...... */
void menu()  /* In C a void argument should be used */
{
	
	int option=0; /* very good */
	struct queuelist* queue; /* ONE CAUSE OF EXCEPTION IN AddToQueue() */
	/* CORRECTION: struct queuelist* queue = NULL; */
	int data;

/* YOU SHOULD BE USING A LOOP STARTING HERE */

	printf("Queue Manipulation Menu\n");
	/* ...... */
	switch(option)
	{
	case 1:
		/* ...... */
		if(AddToQueue(queue,data)==-1) /* Invalid argument: SECONDARY CAUSE OF EXCEPTION */
		/* correction: if(AddToQueue(&queue,data)==-1) */
		/* ...... */
		menu(); /* Recursive call. This is not needed if using a LOOP! */
		/*
		Note: The recursive call would cause a new list to be created
		      basicaly messing up every thing!
		*/
		/* break; SHOULD BE HERE! */		
	/* ...... */
	case 6:
		exit(0);
		/*
		Note: It would be better just to use a break here (to prevent error message below)
		and test if opition == 6 in a loop ( Exp: while( option != 6 ) { do somthing; } ).
		*/
		/* break; SHOULD BE HERE! Not need but good habit. */
	default:
		printf("Invalid Option..\n");
		
	}

/* THIS IS WHERE THE LOOP SHOUD END */
}

void main() /* SHOULD BE int main() IN C - WITH A RETURN STATEMENT AT END OF FUNCTION */
{ /* ...... */ }

int AddToQueue(struct queuelist** q, int data)
{
	struct queuelist** temp; /* YOU DO NOT NEED A POINTER TO A POINTER HERE */
	/* YOU NEED: struct queuelist* temp; */
	/* IF **q THEN THIS SHOULD BE *q = head */
	q = head; /* ASK YOUR SELF: IS head EVER NULL */
	if(q == NULL) /* IF **q THEN THIS SHOULD BE *q = NULL */
	{ /* ...... */ }
	else
	{ /* ...... */ }
/* THERE IS NO RETURN STATEMENT HERE!
THIS FUNCTION JUST RETURNS WHAT EVER HAPPENS TO BE IN THE REGISTER!
YOU DO NOT KNOW WHAT IS RETURNED!
*/
}


void DisplayQueue(struct queuelist* queue)
{
	system("cls");
	queue = head; /* WHY PASS A QUEUE ARGUMENT IF YOU DO NOT USE IT */
	/* ...... */
}

int RemoveFromQueue(struct queuelist* queue)
{
	struct queuelist* prev;
	struct queuelist* second; /* NOT NEEDED */
	queue = head; /* WHY PASS A QUEUE ARGUMENT IF YOU DO NOT USE IT */
	if(queue->valid !=1) /* FORGOT TO CHECK IF queue == NULL */
	{ /* ...... */ }
	else
	{
		queue = head;
		prev = queue;
		queue = queue->next;
		second = queue; /* second NOT USED FOR ANYTHING */
		head = queue;
		printf("Popped off : %d at %x\n",prev->intData, prev->next);
		prev->next = NULL; /* not needed but does not hurt */
		free(prev);
		/* THE ABOVE REDUCED TO 4 LINES
		prev = head;
		head = head->next;
		printf(...);
		free(prev);
		*/
	}
/* THERE IS NO RETURN STATEMENT HERE!
THIS FUNCTION JUST RETURNS WHAT EVER HAPPENS TO BE IN THE REGISTER!
YOU DO NOT KNOW WHAT IS RETURNED!
*/
}

int DeleteQueue(struct queuelist* queue)
{
	struct queuelist* prev;
	queue = head; /* WHY PASS A QUEUE ARGUMENT IF YOU DO NOT USE IT */
	while(queue!=NULL) /* OK */
	{ /* ...... */ }
/* THERE IS NO RETURN STATEMENT HERE!
THIS FUNCTION JUST RETURNS WHAT EVER HAPPENS TO BE IN THE REGISTER!
YOU DO NOT KNOW WHAT IS RETURNED!
*/
}


Good luck and keep trying!

INTP
GeneralMy Own Linked List Pin
Sreekanth Muralidharan8-Nov-04 19:35
Sreekanth Muralidharan8-Nov-04 19:35 
GeneralRe: My Own Linked List Pin
Sreekanth Muralidharan8-Nov-04 22:33
Sreekanth Muralidharan8-Nov-04 22:33 
GeneralRe: My Own Linked List Pin
John R. Shaw9-Nov-04 5:57
John R. Shaw9-Nov-04 5:57 
GeneralDisabling Toolbar Buttons Pin
Bernhard7-Nov-04 22:00
Bernhard7-Nov-04 22:00 
GeneralRe: Disabling Toolbar Buttons Pin
ThatsAlok7-Nov-04 22:06
ThatsAlok7-Nov-04 22:06 
GeneralRe: Disabling Toolbar Buttons Pin
Uwe Keim7-Nov-04 22:23
sitebuilderUwe Keim7-Nov-04 22:23 
GeneralQuestion about programatically created CListCtrl with WS_BORDER Pin
Uwe Keim7-Nov-04 21:54
sitebuilderUwe Keim7-Nov-04 21:54 
GeneralRe: Question about programatically created CListCtrl with WS_BORDER Pin
Uwe Keim7-Nov-04 22:14
sitebuilderUwe Keim7-Nov-04 22:14 
GeneralXP versions ... Pin
smack_2k27-Nov-04 21:53
smack_2k27-Nov-04 21:53 
GeneralRe: XP versions ... Pin
ThatsAlok7-Nov-04 22:13
ThatsAlok7-Nov-04 22:13 
QuestionHow to program same MSN popup ? Pin
guy20047-Nov-04 21:41
guy20047-Nov-04 21:41 
GeneralCombo Box Error Pin
vc-programmer-7-Nov-04 21:39
vc-programmer-7-Nov-04 21:39 
GeneralRe: Combo Box Error Pin
vc-programmer-7-Nov-04 23:16
vc-programmer-7-Nov-04 23:16 
GeneralRe: Combo Box Error Pin
BlackDice8-Nov-04 3:54
BlackDice8-Nov-04 3:54 
Generalconst_cast Pin
Jerome Conus7-Nov-04 21:29
Jerome Conus7-Nov-04 21:29 
GeneralRe: const_cast Pin
ThatsAlok7-Nov-04 22:17
ThatsAlok7-Nov-04 22:17 
QuestionAccess Active Directory to Control PC User Logon? Pin
DengJW7-Nov-04 19:17
DengJW7-Nov-04 19:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.