Recursive Algorithms and Stack Data Structure, Study notes of Computer Science

An in-depth explanation of stacks as a data structure, including its definition, operations, implementations, and applications. Recursive algorithms for creating a linked list from an array and counting the nodes in a linked list. The document also covers the concept of postfix expressions and their evaluation using stacks.

Typology: Study notes

Pre 2010

Uploaded on 11/08/2009

koofers-user-i3t
koofers-user-i3t 🇺🇸

10 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Before introducing new material, here are the recursive versions of the
algorithms from the previous day’s 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
pf3
pf4
pf5

Partial preview of the text

Download Recursive Algorithms and Stack Data Structure and more Study notes Computer Science in PDF only on Docsity!

Before introducing new material, here are the recursive versions of the algorithms from the previous day’s 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. o pop : removes the item from the top of the stack.  A stack is also known as a push-down list.  The “access policy” of a stack is LIFO ( L ast I n F irst O ut).  A stack is a dynamic structure. It changes as elements are added to and removed from it. pop push  A stack can be implemented as a constrained version of a linked list. A stack is referenced via a pointer to the top element of the stack. The link member in the last node of the stack is set to NULL to indicate the bottom of the stack.  Example: stackptr where stackptr points to the top of the stack. A B C D E F G top A B C D E F H H (^8 2 5 )

Applications of Stacks Reading a line of text and writing it out backwards (palindrome checking) Evaluation of arithmetic expressions  Notation can be infix, postfix, or prefix.  Infix: operators appear between operands. A + B  Postfix: operators appear after operands. AB+  Prefix: operators appear before operands. +AB  Operators in a postfix expression are in correct evaluation order and operands appear in the same order as in an infix expression.  Compilers convert infix expressions into postfix expression since they are easy to evaluate with a stack. Postfix Expressions  Precedence of * is higher than + Infix: a + b * c Postfix: abc+  Precedence of * and / are the same and they are left associative. Infix: a + b * c / d Postfix: abcd/+ //reverses a line of text. int main() { structu stackNode *top = NULL; int c; while ((c = getchar() )!= ‘\n’); push(&top, c); while(!isEmpty(top)) printf(“%c”, pop(&top)); printf(“\n”;

 Parentheses override the precedence rules. Infix: (a + b) * c Postfix: ab+c*  More examples Infix: (a + b) * (c – d) Postfix: ab+cd-* Infix: a – b/ (c + d * e) Postfix: abcde+/- Infix: ((a + b) * c – (d – e))/(f + g) Postfix: ab+cde- -fg+/  Association is assume to be left to right except for exponentiation where the association is right to left. Infix: a + b + c = (a + b) + c Postfix: ab+c+ Infix: a ^ b ^ c = a ^ (b ^ c) Postfix: abc^^ Algorithm for Converting an Infix Expression to Postfix Stacks - (^5) while there are more characters in the input string { read the next symbol ch in the infix expression if ch is an operand, put it into the output string. If ch is an operator { check the item op on the top of the stack while (more items in the stack && precedence (ch) <= precedence(op) { pop op and append it to the output string. op becomes the next top element. } push ch onto the stack } }