icp/oer/courses/c-advanced/sections/01-introduction/04-queue/program.c

71 lines
1.4 KiB
C

#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
char Character;
struct node *Next;
}node;
typedef struct
{
node *Head;
node *Tail;
}queue;
node *alloc_node(char c, node *Next)
{
node *NewNode = malloc(sizeof(node));
NewNode->Character = c;
NewNode->Next = Next;
return NewNode;
}
// You could reuse the implementation of the singly linked list,
// but in this case we can optimize our operations,
// because we have pointers to the beginning end of the linked list.
// NOTE: this put elements on the end of the list
// Don't forget to initialize the Head pointer
void enqueue(queue *Queue, char c)
{
// TODO: implement this
}
// NOTE: This returns the char, because we don't want to manage
// the memory in "user code"
// Don't forget to free the node, otherwise this queue has a leak.
char dequeue(queue *Queue)
{
// TODO: implement this
return 0;
}
// NOTE: in this example we only use this to check if the list is empty.
// To do that we could also track the element count, but the peak operation
// has also some other use cases.
node *peak(queue *Queue)
{
// TODO: implement this
return 0;
}
// you can implement the queue-operations without ever searching through the list.
int main(int argc, char **argv)
{
queue Queue = {};
char *String = "HelloWorld!";
// The String should come out as it went in.
while(*String)
{
enqueue(&Queue, *String++);
}
while(peak(&Queue))
{
printf("%c", dequeue(&Queue));
}
}