Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Linked list headache in VC++. Please help

Linked list headache in VC++. Please help

Scheduled Pinned Locked Moved C / C++ / MFC
data-structuresc++performancehelpquestion
9 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sreekanth Muralidharan
    wrote on last edited by
    #1

    Hello buddies, I am designing a queue by a linked list for an application. I wrote the following code while it brought up an exception in the line marked here. Could you please help me out ? #include #include struct queuelist { int intData; int valid; struct queuelist* next; }; struct queuelist *head, *tail; int AddToQueue(struct queuelist*,int); int RemoveFromQueue(struct queuelist*); int DeleteQueue(struct queuelist*); void DisplayQueue(struct queuelist*); void menu(); void menu() { int option=0; struct queuelist* queue; int data; printf("Queue Manipulation Menu\n"); printf("........................\n\n"); printf("1. Add member to queue.\n"); printf("2. Extract next member.\n"); printf("3. Delete whole queue.\n"); printf("4. Help\n"); printf("5. Display queue contents.\n"); printf("6. Quit.\n"); scanf("%d",&option); switch(option) { case 1: printf("Adding to queue.\n"); printf("Member: "); scanf("%d",&data); if(AddToQueue(queue,data)==-1) printf("Could not add..\n"); else printf("Added..\n"); menu(); case 2: system("cls"); printf("Extract next member.\n"); RemoveFromQueue(queue); menu(); case 3: DeleteQueue(queue); menu(); case 4: printf("Under Construction.\n"); menu(); case 5: printf("Queue Contents.\n"); DisplayQueue(queue); menu(); case 6: exit(0); default: printf("Invalid Option..\n"); } } void main() { system("cls"); menu(); getch(); } int AddToQueue(struct queuelist** q, int data) { struct queuelist** temp; q = head; if(q == NULL) { printf("First member in queue.\n"); temp = (struct queuelist**)malloc(sizeof(struct queuelist)); if(*temp == NULL) { printf("Not enough memory. Stopping...\n"); return -1; } else { (*temp)->intData = data; (*temp)->next=NULL; (*temp)->valid=1; /* Exception falls in the next line */ (*q)->next = temp; head = *q; tail = *q; } } else { while((*q)->next!=NULL) (*q) = &(*q)->next; temp = (struct queuelist**)malloc(sizeof(struct queuelist)); if((*temp) == NULL) { printf("Not enough memory. Quitting...\n"); return -1; } else { (*temp)->intData = data; (*temp)->next=NULL; (*q)->next = *temp; (*q) = (*q)->next; (*q)->valid = 1; tail = *q; } } } void DisplayQueue(struct queuelist* queue) { syst

    U F J 3 Replies Last reply
    0
    • S Sreekanth Muralidharan

      Hello buddies, I am designing a queue by a linked list for an application. I wrote the following code while it brought up an exception in the line marked here. Could you please help me out ? #include #include struct queuelist { int intData; int valid; struct queuelist* next; }; struct queuelist *head, *tail; int AddToQueue(struct queuelist*,int); int RemoveFromQueue(struct queuelist*); int DeleteQueue(struct queuelist*); void DisplayQueue(struct queuelist*); void menu(); void menu() { int option=0; struct queuelist* queue; int data; printf("Queue Manipulation Menu\n"); printf("........................\n\n"); printf("1. Add member to queue.\n"); printf("2. Extract next member.\n"); printf("3. Delete whole queue.\n"); printf("4. Help\n"); printf("5. Display queue contents.\n"); printf("6. Quit.\n"); scanf("%d",&option); switch(option) { case 1: printf("Adding to queue.\n"); printf("Member: "); scanf("%d",&data); if(AddToQueue(queue,data)==-1) printf("Could not add..\n"); else printf("Added..\n"); menu(); case 2: system("cls"); printf("Extract next member.\n"); RemoveFromQueue(queue); menu(); case 3: DeleteQueue(queue); menu(); case 4: printf("Under Construction.\n"); menu(); case 5: printf("Queue Contents.\n"); DisplayQueue(queue); menu(); case 6: exit(0); default: printf("Invalid Option..\n"); } } void main() { system("cls"); menu(); getch(); } int AddToQueue(struct queuelist** q, int data) { struct queuelist** temp; q = head; if(q == NULL) { printf("First member in queue.\n"); temp = (struct queuelist**)malloc(sizeof(struct queuelist)); if(*temp == NULL) { printf("Not enough memory. Stopping...\n"); return -1; } else { (*temp)->intData = data; (*temp)->next=NULL; (*temp)->valid=1; /* Exception falls in the next line */ (*q)->next = temp; head = *q; tail = *q; } } else { while((*q)->next!=NULL) (*q) = &(*q)->next; temp = (struct queuelist**)malloc(sizeof(struct queuelist)); if((*temp) == NULL) { printf("Not enough memory. Quitting...\n"); return -1; } else { (*temp)->intData = data; (*temp)->next=NULL; (*q)->next = *temp; (*q) = (*q)->next; (*q)->valid = 1; tail = *q; } } } void DisplayQueue(struct queuelist* queue) { syst

      U Offline
      U Offline
      Uwe Keim
      wrote on last edited by
      #2

      Beside writing code for learning new things, I would never ever implement my own linked list or so when there are powerful, well-tested and broadly used collection classes like the STL [^]. -- Affordable Windows-based CMS: www.zeta-producer.de/enu

      S 1 Reply Last reply
      0
      • U Uwe Keim

        Beside writing code for learning new things, I would never ever implement my own linked list or so when there are powerful, well-tested and broadly used collection classes like the STL [^]. -- Affordable Windows-based CMS: www.zeta-producer.de/enu

        S Offline
        S Offline
        Sreekanth Muralidharan
        wrote on last edited by
        #3

        Hello friend, I would not prefer using STL for my small application. If you would refer to the code and find out the bug, it would be of great help to me. Regards, Sreekanth Muralidharan

        1 Reply Last reply
        0
        • S Sreekanth Muralidharan

          Hello buddies, I am designing a queue by a linked list for an application. I wrote the following code while it brought up an exception in the line marked here. Could you please help me out ? #include #include struct queuelist { int intData; int valid; struct queuelist* next; }; struct queuelist *head, *tail; int AddToQueue(struct queuelist*,int); int RemoveFromQueue(struct queuelist*); int DeleteQueue(struct queuelist*); void DisplayQueue(struct queuelist*); void menu(); void menu() { int option=0; struct queuelist* queue; int data; printf("Queue Manipulation Menu\n"); printf("........................\n\n"); printf("1. Add member to queue.\n"); printf("2. Extract next member.\n"); printf("3. Delete whole queue.\n"); printf("4. Help\n"); printf("5. Display queue contents.\n"); printf("6. Quit.\n"); scanf("%d",&option); switch(option) { case 1: printf("Adding to queue.\n"); printf("Member: "); scanf("%d",&data); if(AddToQueue(queue,data)==-1) printf("Could not add..\n"); else printf("Added..\n"); menu(); case 2: system("cls"); printf("Extract next member.\n"); RemoveFromQueue(queue); menu(); case 3: DeleteQueue(queue); menu(); case 4: printf("Under Construction.\n"); menu(); case 5: printf("Queue Contents.\n"); DisplayQueue(queue); menu(); case 6: exit(0); default: printf("Invalid Option..\n"); } } void main() { system("cls"); menu(); getch(); } int AddToQueue(struct queuelist** q, int data) { struct queuelist** temp; q = head; if(q == NULL) { printf("First member in queue.\n"); temp = (struct queuelist**)malloc(sizeof(struct queuelist)); if(*temp == NULL) { printf("Not enough memory. Stopping...\n"); return -1; } else { (*temp)->intData = data; (*temp)->next=NULL; (*temp)->valid=1; /* Exception falls in the next line */ (*q)->next = temp; head = *q; tail = *q; } } else { while((*q)->next!=NULL) (*q) = &(*q)->next; temp = (struct queuelist**)malloc(sizeof(struct queuelist)); if((*temp) == NULL) { printf("Not enough memory. Quitting...\n"); return -1; } else { (*temp)->intData = data; (*temp)->next=NULL; (*q)->next = *temp; (*q) = (*q)->next; (*q)->valid = 1; tail = *q; } } } void DisplayQueue(struct queuelist* queue) { syst

          F Offline
          F Offline
          FearlessBurner
          wrote on last edited by
          #4

          Firstly, I would offer the same advice, don't write your own linked lists or collections etc, use STL or the MFC CList etc. The problem in your code appears to be a confusion in the pointers, and the code is littered with such problems, eg in AddToQueue(), you have set temp to be of type struct questlist **, but your call to malloc (btw why are you not using new???), will create a pointer to a single queuelist (i.e. (struct queuelist *)). So you should change the code to something like struct queuelist *temp = NULL; ............. temp = (struct queuelist *)malloc( sizeof(struct queuelist) ); if ( temp == NULL ) { // Not enough memory etc } else { temp->intData = data; temp->next=NULL; temp->valid=1; *q = temp; } The function call to AddToQueue() in your main part should read AddToQueue( &queue ) else the wrong type is being passed to the function. I must admit that I am surprised that your compiler did not generate warning messages with your code? I think that you would be best of revisiting all of your code, and checking all your pointers and paraeters. Remember that malloc will only return a pointer to a section of memory that you have allocated. Better still, use STL or perhaps the MFC CList template.

          S 1 Reply Last reply
          0
          • S Sreekanth Muralidharan

            Hello buddies, I am designing a queue by a linked list for an application. I wrote the following code while it brought up an exception in the line marked here. Could you please help me out ? #include #include struct queuelist { int intData; int valid; struct queuelist* next; }; struct queuelist *head, *tail; int AddToQueue(struct queuelist*,int); int RemoveFromQueue(struct queuelist*); int DeleteQueue(struct queuelist*); void DisplayQueue(struct queuelist*); void menu(); void menu() { int option=0; struct queuelist* queue; int data; printf("Queue Manipulation Menu\n"); printf("........................\n\n"); printf("1. Add member to queue.\n"); printf("2. Extract next member.\n"); printf("3. Delete whole queue.\n"); printf("4. Help\n"); printf("5. Display queue contents.\n"); printf("6. Quit.\n"); scanf("%d",&option); switch(option) { case 1: printf("Adding to queue.\n"); printf("Member: "); scanf("%d",&data); if(AddToQueue(queue,data)==-1) printf("Could not add..\n"); else printf("Added..\n"); menu(); case 2: system("cls"); printf("Extract next member.\n"); RemoveFromQueue(queue); menu(); case 3: DeleteQueue(queue); menu(); case 4: printf("Under Construction.\n"); menu(); case 5: printf("Queue Contents.\n"); DisplayQueue(queue); menu(); case 6: exit(0); default: printf("Invalid Option..\n"); } } void main() { system("cls"); menu(); getch(); } int AddToQueue(struct queuelist** q, int data) { struct queuelist** temp; q = head; if(q == NULL) { printf("First member in queue.\n"); temp = (struct queuelist**)malloc(sizeof(struct queuelist)); if(*temp == NULL) { printf("Not enough memory. Stopping...\n"); return -1; } else { (*temp)->intData = data; (*temp)->next=NULL; (*temp)->valid=1; /* Exception falls in the next line */ (*q)->next = temp; head = *q; tail = *q; } } else { while((*q)->next!=NULL) (*q) = &(*q)->next; temp = (struct queuelist**)malloc(sizeof(struct queuelist)); if((*temp) == NULL) { printf("Not enough memory. Quitting...\n"); return -1; } else { (*temp)->intData = data; (*temp)->next=NULL; (*q)->next = *temp; (*q) = (*q)->next; (*q)->valid = 1; tail = *q; } } } void DisplayQueue(struct queuelist* queue) { syst

            J Offline
            J Offline
            John R Shaw
            wrote on last edited by
            #5
            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
            #include
            #include

            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 queue

            S 1 Reply Last reply
            0
            • F FearlessBurner

              Firstly, I would offer the same advice, don't write your own linked lists or collections etc, use STL or the MFC CList etc. The problem in your code appears to be a confusion in the pointers, and the code is littered with such problems, eg in AddToQueue(), you have set temp to be of type struct questlist **, but your call to malloc (btw why are you not using new???), will create a pointer to a single queuelist (i.e. (struct queuelist *)). So you should change the code to something like struct queuelist *temp = NULL; ............. temp = (struct queuelist *)malloc( sizeof(struct queuelist) ); if ( temp == NULL ) { // Not enough memory etc } else { temp->intData = data; temp->next=NULL; temp->valid=1; *q = temp; } The function call to AddToQueue() in your main part should read AddToQueue( &queue ) else the wrong type is being passed to the function. I must admit that I am surprised that your compiler did not generate warning messages with your code? I think that you would be best of revisiting all of your code, and checking all your pointers and paraeters. Remember that malloc will only return a pointer to a section of memory that you have allocated. Better still, use STL or perhaps the MFC CList template.

              S Offline
              S Offline
              Sreekanth Muralidharan
              wrote on last edited by
              #6

              Hello Thanks to all who took their time to go through my bugful code. Regards, Sreekanth Muralidharan

              1 Reply Last reply
              0
              • J John R Shaw
                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
                #include
                #include

                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 queue

                S Offline
                S Offline
                Sreekanth Muralidharan
                wrote on last edited by
                #7

                Hello, I cleared the above problems with the following updates to my original code: #include #include #include #include struct queuelist { int intData; int valid; struct queuelist* next; }; struct queuelist *head, *tail; int AddToQueue(struct queuelist*,int); int RemoveFromQueue(struct queuelist*); int DeleteQueue(struct queuelist*); void DisplayQueue(struct queuelist*); void menu(); struct queuelist* queue=NULL; void menu() { int option=0; int data=0; printf("Queue Manipulation Menu\n"); printf("........................\n\n"); printf("1. Add member to queue.\n"); printf("2. Extract next member.\n"); printf("3. Delete whole queue.\n"); printf("4. Help\n"); printf("5. Display queue contents.\n"); printf("6. Quit.\n"); scanf("%d",&option); switch(option) { case 1: queue=head; printf("Adding to queue.\n"); printf("Member: "); scanf("%d",&data); if(AddToQueue(queue,data)==-1) printf("Could not add..\n"); else printf("Added..\n"); break; case 2: system("cls"); printf("Extract next member.\n"); RemoveFromQueue(queue); break; case 3: DeleteQueue(queue); break; case 4: printf("Under Construction.\n"); case 5: printf("Queue Contents.\n"); DisplayQueue(queue); break; case 6: exit(0); default: printf("Invalid Option..\n"); } } void main() { system("cls"); while(1) menu(); getch(); } int AddToQueue(struct queuelist* q, int data) { struct queuelist* temp; q = head; if(q == NULL) { printf("First member in queue.\n"); q = (struct queuelist*)malloc(sizeof(struct queuelist)); if(q == NULL) { printf("Not enough memory. Stopping...\n"); return -1; } else { q->intData = data; q->valid=1; q->next=NULL; head = q; tail = q; } } else { q=head; while(q->next!=NULL) q = q->next; temp = (struct queuelist*)malloc(sizeof(struct queuelist)); if(temp == NULL) { printf("Not enough memory. Quitting...\n"); return -1; } else { temp->intData = data; temp->next=NULL; q->next = temp; q = q->next; q->valid = 1; tail = q; } } return data; } void DisplayQueue(struct queuelist* queue) { system("cls"); queue = head; if(queue == NULL || queue->valid != 1) { printf("Empty Queue"); } else { while(queue != NULL) { printf("||%d|%x||->",q

                S 1 Reply Last reply
                0
                • S Sreekanth Muralidharan

                  Hello, I cleared the above problems with the following updates to my original code: #include #include #include #include struct queuelist { int intData; int valid; struct queuelist* next; }; struct queuelist *head, *tail; int AddToQueue(struct queuelist*,int); int RemoveFromQueue(struct queuelist*); int DeleteQueue(struct queuelist*); void DisplayQueue(struct queuelist*); void menu(); struct queuelist* queue=NULL; void menu() { int option=0; int data=0; printf("Queue Manipulation Menu\n"); printf("........................\n\n"); printf("1. Add member to queue.\n"); printf("2. Extract next member.\n"); printf("3. Delete whole queue.\n"); printf("4. Help\n"); printf("5. Display queue contents.\n"); printf("6. Quit.\n"); scanf("%d",&option); switch(option) { case 1: queue=head; printf("Adding to queue.\n"); printf("Member: "); scanf("%d",&data); if(AddToQueue(queue,data)==-1) printf("Could not add..\n"); else printf("Added..\n"); break; case 2: system("cls"); printf("Extract next member.\n"); RemoveFromQueue(queue); break; case 3: DeleteQueue(queue); break; case 4: printf("Under Construction.\n"); case 5: printf("Queue Contents.\n"); DisplayQueue(queue); break; case 6: exit(0); default: printf("Invalid Option..\n"); } } void main() { system("cls"); while(1) menu(); getch(); } int AddToQueue(struct queuelist* q, int data) { struct queuelist* temp; q = head; if(q == NULL) { printf("First member in queue.\n"); q = (struct queuelist*)malloc(sizeof(struct queuelist)); if(q == NULL) { printf("Not enough memory. Stopping...\n"); return -1; } else { q->intData = data; q->valid=1; q->next=NULL; head = q; tail = q; } } else { q=head; while(q->next!=NULL) q = q->next; temp = (struct queuelist*)malloc(sizeof(struct queuelist)); if(temp == NULL) { printf("Not enough memory. Quitting...\n"); return -1; } else { temp->intData = data; temp->next=NULL; q->next = temp; q = q->next; q->valid = 1; tail = q; } } return data; } void DisplayQueue(struct queuelist* queue) { system("cls"); queue = head; if(queue == NULL || queue->valid != 1) { printf("Empty Queue"); } else { while(queue != NULL) { printf("||%d|%x||->",q

                  S Offline
                  S Offline
                  Sreekanth Muralidharan
                  wrote on last edited by
                  #8

                  Hello See the code in C++ for linked list implementation of a queue. #include #include #include #include #include struct queuelist { int intData; int valid; struct queuelist* next; }; struct queuelist *head, *tail; class CNode { private: struct queuelist* queue; public: int AddToQueue(int); int PopFromQueue(); int DeleteQueue(); void DisplayQueue(); }; void menu(); void menu() { textcolor(WHITE); cout<<" Queue Manipulation Menu"<>option; cout<>data; if(node.AddToQueue(data)==-1) cout<<"Could not add.."<intData = data; temp->next=NULL; temp->valid=1; q->next = temp; head = temp; tail = temp; q = temp; } } else { while(q->next!=NULL) q=q->next; temp = (struct queuelist*)malloc(sizeof(struct queuelist)); if(temp == NULL) { textcolor(RED); cout<<"Not enough memory. Quitting..."<intData = data; temp->next=NULL; q->next = temp; q = q->next; q->valid = 1; tail = q; }

                  J 1 Reply Last reply
                  0
                  • S Sreekanth Muralidharan

                    Hello See the code in C++ for linked list implementation of a queue. #include #include #include #include #include struct queuelist { int intData; int valid; struct queuelist* next; }; struct queuelist *head, *tail; class CNode { private: struct queuelist* queue; public: int AddToQueue(int); int PopFromQueue(); int DeleteQueue(); void DisplayQueue(); }; void menu(); void menu() { textcolor(WHITE); cout<<" Queue Manipulation Menu"<>option; cout<>data; if(node.AddToQueue(data)==-1) cout<<"Could not add.."<intData = data; temp->next=NULL; temp->valid=1; q->next = temp; head = temp; tail = temp; q = temp; } } else { while(q->next!=NULL) q=q->next; temp = (struct queuelist*)malloc(sizeof(struct queuelist)); if(temp == NULL) { textcolor(RED); cout<<"Not enough memory. Quitting..."<intData = data; temp->next=NULL; q->next = temp; q = q->next; q->valid = 1; tail = q; }

                    J Offline
                    J Offline
                    John R Shaw
                    wrote on last edited by
                    #9

                    :)Much better, but still shows confusion. :omg:Now you have C and C++ mixed all up together in a Frakeinstineon (the monster) kind of way. You need to know a little more about C programing and for C++ programing I recommend the Bruce Eckel series of books, since he is good at explaning it. Also, Bruce Eckel has a web-site that has copies of his books in electronic form that you can download (recomend purchasing them though). After reading you bio and seeing how well you did, I have decided to write my version and will send it to you (when finished). Mean while: 1) queuelist is still a node. 2) CNode is not a node it represents the list. 3) The fuctions that require a return value do not return a success value. 4) The getch() function will never be reached, because you are still using exit() to exit from the program.

                    /* A simple global list in C */

                    /* A simple list node */
                    typedef struct tagNode
                    {
                    struct tagNode* pNext;
                    int intData;
                    } NODE; /* Using typedef to avoid need to type 'struct tagNode' */

                    /* Global list head */
                    static NODE* g_pHead; /* 'static' makes it local global and = NULL */

                    /* Function prototypes */
                    int AddNode(int intData); /* Add new data node to end of list */
                    int DisplayList(); /* Display all data in list */
                    int DeleteNode(); /* Remove and delete node from start of list */
                    void DeleteAll(); /* Delete all node in list */

                    // A Simple linked list class in C++
                    class CIntList // class defults to private
                    {
                    // Private data and declarations
                    struct NODE
                    {
                    struct NODE* pNext;
                    int intData;
                    };
                    NODE* m_pHead; // Pointer to list head

                    // Public interface
                    public:
                    int AddNode(int intData); // Add new data node to end of list
                    int DisplayList(); // Display all data in list
                    int DeleteNode(); // Remove and delete node from start of list
                    void DeleteAll(); // Delete all node in list

                    // Constructor and Destructor
                    CIntList() : m_pHead(NULL) {} // Initialize head point to NULL
                    ~CIntList() { DeleteAll(); } // Make sure list if freed when destoyed
                    };

                    :)Good Luck! INTP

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups