944,044 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 6597
  • C RSS
Oct 11th, 2004
1

can ANYONE help? need to make a queue

Expand Post »
i havent been able to find anyone to help me ....

i am looking to create a queue ....

i cant use a stack, as that is LIFO (last in, first out) ...

but i need a FIFO (first in, first out) ....

i have some code for a stack class using linked lists .... and i tried modifying the code to do FIFO, but i keep getting an error in this one spot (instead of popping the head of the stack, i would have it traverse the stack to the back and return the end ... but getting the element before the end to point to NULL, well, keeps crashing the program ....

can anyone help?
Similar Threads
Reputation Points: 12
Solved Threads: 0
Light Poster
jaeSun is offline Offline
31 posts
since Oct 2004
Oct 11th, 2004
1

Re: can ANYONE help? need to make a queue

Here is some C code that might help you get started.
  1. // A linear list of data accessed first in, first out (FIFO) is called a QUEUE.
  2. // Pelles C (vegaseat)
  3.  
  4. #include <stdio.h> // in/out function header
  5. #include <string.h> // string function header
  6. #include <stdlib.h> // malloc()
  7.  
  8. void enter(void);
  9. void list(void);
  10. void q_store(char *ptr);
  11. void q_delete(void);
  12. int store = 0; // next storage position in queue[]
  13. int retrieve = 0; // retrieve position in queue[]
  14. char *queue[100]; // this array forms the queue
  15.  
  16. void main(void)
  17. {
  18. enter(); // enter some data in the queue and list the data
  19. puts("\n\nThis is all the data in the fifo queue:");
  20. list();
  21. q_delete(); // delete first entry in queue and list again
  22. puts("\n\nThis the data after one deletion (first in, first out):");
  23. list();
  24. getchar();
  25. }
  26.  
  27. //
  28. // prompt for data entry and store in queue via q_store()
  29. //
  30. void enter(void)
  31. {
  32. static char str[100], *ptr;
  33.  
  34. do {
  35. printf("Enter name (ENTER only will exit) : ");
  36. gets(str);
  37. ptr = (char *) malloc(strlen(str)); // starting address of memory
  38. strcpy(ptr,str);
  39. if (*str)
  40. q_store(ptr); // store in queue if string has info
  41. } while (*str); // until no entry
  42. }
  43.  
  44. void list(void) // list the contents of the queue
  45. {
  46. int k;
  47.  
  48. for (k = retrieve; k < store; k++)
  49. printf("\n%d) %s",k+1,queue[k]);
  50. }
  51.  
  52. //
  53. // store data items in the queue
  54. //
  55. void q_store(char *ptr)
  56. {
  57. if (store == 100) {
  58. puts("\nList is full!");
  59. return;
  60. }
  61. queue[store] = ptr;
  62. store++; // point to next available storage position in queue[]
  63. }
  64.  
  65. //
  66. // delete data at retrieve position
  67. //
  68. void q_delete(void)
  69. {
  70. if (store == retrieve) {
  71. puts("\nQueue is empty!");
  72. return;
  73. }
  74. retrieve++; // move retrieve position to next data item
  75. }
There could be more modern code for the puts() and gets() functions.
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Oct 11th, 2004
0

Re: can ANYONE help? need to make a queue

hmm ... i was doing it in C++ ...i kinda understand C

this is the code i was trying to do:

here is the main file:
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <stdio.h>
  5. #include <string>
  6. #include "stackli.h"
  7. using namespace std;
  8.  
  9. struct flight {
  10. int num;
  11. };
  12.  
  13. int main()
  14. {
  15.  
  16. flight ted;
  17. Stack<flight> readyq2;
  18. int print;
  19.  
  20. ted.num = 1;
  21. readyq2.push(ted);
  22. ted.num = 2;
  23. readyq2.push(ted);
  24. ted.num = 3;
  25. readyq2.push(ted);
  26.  
  27. ted = readyq2.topAndPop();
  28. cout<<ted.num<<endl;
  29. ted = readyq2.topAndPop();
  30. cout<<ted.num<<endl;
  31. ted = readyq2.topAndPop();
  32. cout<<ted.num<<endl;
  33.  
  34. cout <<"hello"<<endl;
  35.  
  36. return 0;
  37. }

here is the header declaration for the stack:

  1. #ifndef STACKLI_H
  2. #define STACKLI_H
  3. //#include "dsexceptions.h"
  4. #include <iostream> // For NULL
  5. // Stack class -- linked list implementation
  6. //
  7. // CONSTRUCTION: with no parameters
  8. //
  9. // ******************PUBLIC OPERATIONS*********************
  10. // void push( x ) --> Insert x
  11. // void pop( ) --> Remove most recently inserted item
  12. // Object top( ) --> Return most recently inserted item
  13. // Object topAndPop( ) --> Return and remove most recently inserted item
  14. // bool isEmpty( ) --> Return true if empty; else false
  15. // bool isFull( ) --> Return true if full; else false
  16. // void makeEmpty( ) --> Remove all items
  17. // ******************ERRORS********************************
  18. // Overflow and Underflow thrown as needed
  19. template <class Object>
  20. class Stack
  21. {
  22. public:
  23. Stack( );
  24. Stack( const Stack & rhs );
  25. ~Stack( );
  26. bool isEmpty( ) const;
  27. bool isFull( ) const;
  28. const Object & top( ) const;
  29. void makeEmpty( );
  30. void pop( );
  31. void push( const Object & x );
  32. Object topAndPop( );
  33. private:
  34. struct ListNode
  35. {
  36. Object element;
  37. ListNode *next;
  38. ListNode( const Object & theElement, ListNode * n = NULL )
  39. : element( theElement ), next( n ) { }
  40. };
  41. ListNode *topOfStack;
  42. };
  43. #include "stackli.cpp"
  44. #endif


and here is the implementation of the stack:

i commented where the problem lies ....

  1. #include "stackli.h"
  2. #include <iostream>
  3.  
  4. /**
  5.  * Construct the stack.
  6.  */
  7. template <class Object>
  8. Stack<Object>::Stack( )
  9. {
  10. topOfStack = NULL;
  11. }
  12.  
  13. /**
  14.  * Copy constructor.
  15.  */
  16. template <class Object>
  17. Stack<Object>::Stack( const Stack<Object> & rhs )
  18. {
  19. topOfStack = NULL;
  20. *this = rhs;
  21. }
  22.  
  23. /**
  24.  * Destructor.
  25.  */
  26. template <class Object>
  27. Stack<Object>::~Stack( )
  28. {
  29. makeEmpty( );
  30. }
  31.  
  32. /**
  33.  * Test if the stack is logically full.
  34.  * Return false always, in this implementation.
  35.  */
  36. template <class Object>
  37. bool Stack<Object>::isFull( ) const
  38. {
  39. return false;
  40. }
  41.  
  42. /**
  43.  * Test if the stack is logically empty.
  44.  * Return true if empty, false otherwise.
  45.  */
  46. template <class Object>
  47. bool Stack<Object>::isEmpty( ) const
  48. {
  49. return topOfStack == NULL;
  50. }
  51.  
  52. /**
  53.  * Make the stack logically empty.
  54.  */
  55. template <class Object>
  56. void Stack<Object>::makeEmpty( )
  57. {
  58. while( !isEmpty( ) )
  59. pop( );
  60. }
  61.  
  62. /**
  63.  * Get the most recently inserted item in the stack.
  64.  * Return the most recently inserted item in the stack
  65.  * or throw an exception if empty.
  66.  */
  67. template <class Object>
  68. const Object & Stack<Object>::top( ) const
  69. {
  70. if( isEmpty( ) )
  71. {}
  72. ListNode *current = topOfStack;
  73.  
  74. while(current->next != NULL)
  75. {
  76. current = current->next;
  77. }
  78.  
  79. return current->element;
  80. }
  81.  
  82. /**
  83.  * Remove the most recently inserted item from the stack.
  84.  * Exception Underflow if the stack is empty.
  85.  */
  86. template <class Object>
  87. void Stack<Object>::pop( )
  88. {
  89. if( isEmpty( ) )
  90. {}
  91.  
  92. ListNode *previous = topOfStack;
  93. ListNode *toTheEnd = topOfStack;
  94.  
  95. while(toTheEnd->next != NULL)
  96. {
  97. previous = toTheEnd;
  98. toTheEnd = toTheEnd->next;
  99. }
  100.  
  101. /*************************
  102.   this next line is where the problem lies
  103.   *************************/
  104. previous->next = NULL;
  105. delete toTheEnd;
  106. }
  107.  
  108. /**
  109.  * Return and remove the most recently inserted item
  110.  * from the stack.
  111.  */
  112. template <class Object>
  113. Object Stack<Object>::topAndPop( )
  114. {
  115. Object topItem = top( );
  116. pop( );
  117. return topItem;
  118. }
  119.  
  120. /**
  121.  * Insert x into the stack.
  122.  */
  123. template <class Object>
  124. void Stack<Object>::push( const Object & x )
  125. {
  126. topOfStack = new ListNode( x, topOfStack );
  127.  
  128. }

i think the problem was that i cannot legally access the pointer within the struct ......

i thought of another solution to my problem ... i was going to use a binary heap .... and when it enters the queue, it gets a ticket number, and it will just sort based on that (ticket number just increases everytime) .....

not exactly the most effecient, but it works ....
Reputation Points: 12
Solved Threads: 0
Light Poster
jaeSun is offline Offline
31 posts
since Oct 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Math problem in C
Next Thread in C Forum Timeline: pls.help me im begging you!!!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC