CS3343/3341
 Analysis of Algorithms 
Spring 2012
  Linked List   
Used as
  Stack and Queue  
(C Version)


Linked List, with Stack and Queue (C Version)

[Please see the Java Version of this part of the recitation for Java code, directions, and a picture.]

The C program below was translated from the corresponding Java program, but the translation was only partly successful. In the C version, we can use one single stack or queue but not both. If you want more than this or you want a generic structure, you should code in C++, rather than C.

Linked List, with Stack and Queue (C Version)
Linked List ItselfStack and Queue along with Test Code
// linkedlist.h: header file,  ref in
//   linkedlist.c, stack.c, queue.c
void addlast(int );
void addfirst(int );
int removefirst();
void printlist();
void printreverse();
int empty();

// linkedlist.c: #include <stdio.h> #include <stdlib.h> #include "linkedlist.h" struct listnode { int data; struct listnode* next; }; static void printreverse0(struct listnode* ); struct listnode* first = NULL; // front, top struct listnode* last = NULL; // rear void addlast(int dataitem) { // or add to rear, i.e. enqueue // (a). Supply code for this function. // Add a new link at "last" end // holding "dataItem". // May need to treat empty list // as a separate case. } void addfirst(int dataitem) { // or add to front, i.e. dequeue, or push // (b). Supply code for this function. // Add a new link at "first" end // holding "dataItem". } int removefirst() { // or pop // (c). Supply code for this function. // Must remove link at "first" end, // and return data value stored there. } int empty() { return first == NULL; } void printlist() { // first to last, or front to rear // (d). Supply code to print the list // from first to last. } void printreverse() { printreverse0(first); printf("\n"); } static void printreverse0(struct listnode* temp){ // last to first, or rear to front // (e). Supply code for a RECURSIVE function // that prints from last to first. } } int delete(int dataItem) { // delete from anywhere in list // (f). Supply code for a function that // will delete "dataItem" if it is present. // return 0 if item was deleted. // return -1 if item was not present. }
// stack.h: header file,
void push(int );
int pop();

// stack.c: based on linkedlist.c #include "linkedlist.h" #include "stack.h" void push(int dataItem) { addfirst(dataItem); } int pop() { return removefirst(); }
// queue.h: header file void enqueue(int ); int dequeue();
// queue.c: based on linkedlist.c #include "linkedlist.h" #include "queue.h" void enqueue(int dataitem) { addlast(dataitem); } int dequeue() { return removefirst(); }
// linkedlist.h: header file void printlist(); void printreverse(); int empty();
// linktest.c: test program. Note: // can't use both stack and queue #include <stdio.h> #include "queue.h" #include "stack.h" #include "linkedlist0.h" void main() { int a[] = {2, 3, 5, 7, 11, 13}; int i, t; for (i = 0; i < 6; i++) enqueue(a[i]); printf("Queue:"); printlist(); printreverse(); t = dequeue(); printf("%i ", t); t = dequeue(); printf("%i\n", t); // must empty queue to use stack while (!empty()) t = dequeue(); for (i = 0; i < 6; i++) push(a[i]); printf("Stack:"); printlist(); printreverse(); t = pop(); printf("%i ", t); t = pop(); printf("%i\n", t); push(11); push(13); printf("Delete test:\n"); printlist(); for (i = 5; i >= 0; i--) { delete(a[i]); printlist(); } printf("2nd Delete test:\n"); for (i = 0; i < 6; i++) push(a[i]); printlist(); printf("%i ", delete(1)); printf("%i ", delete(8)); printf("%i\n", delete(15)); printf("3rd Delete test:\n"); for (i = 0; i < 6; i++) { delete(a[i]); printlist(); } }
Output
Queue:
2 3 5 7 11 13 
13 11 7 5 3 2 
2 3
Stack:
13 11 7 5 3 2 
2 3 5 7 11 13
13 11
Delete test:
13 11 7 5 3 2 
11 7 5 3 2 
7 5 3 2 
5 3 2 
3 2 
2 
Empty
2nd Delete test:
13 11 7 5 3 2 
-1 -1 -1
3rd Delete test:
13 11 7 5 3 
13 11 7 5 
13 11 7 
13 11 
13 
Empty


Revision date: 2012-01-02. (Please use ISO 8601, the International Standard Date and Time Notation.)