stacks14 - Before introducing new material, here are the...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Before introducing new material, here are the recursive versions of the algorithms from the previous days notes. Stacks A stack is a collection of items into which new items are inserted and from which items are deleted from only one end, called the top of the stack. Different implementations are possible; although the concept of a stack is unique. Example: Trays in the cafeteria. Stacks - 1 Data Structures: Stacks Practice: Create a recursive version of this function to create a linked list from an array of values. Answer will appear in the next set of notes. Practice: Create a recursive version of this function to count the nodes in a linked list. Answer will appear in the next set of notes. //Copies the contents of an array into a dynamically //growing list. struct node *array_to_list (int a[ ], int j, int n) { struct node *head; if ( j >= n) //base case return NULL; else { head = malloc(sizeof(struct node)); head -> data = a[j]; head ->next = array_to_list(a, j+1, n); return head; } } //Count the number of nodes in a list int count(struct node *head) { if (head == NULL) //base case return 0; else return (1 + count(head->next)); } A B C D E F G top There are two primary operations defined on a stack: o push : add a new item to the top of the stack....
View Full Document

This document was uploaded on 06/12/2011.

Page1 / 6

stacks14 - Before introducing new material, here are the...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online