lecture_26_revised

lecture_26_revised - 1 sentinel.cc 2010-11-18 07:06 David...

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

View Full Document Right Arrow Icon
Background image of page 1

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

View Full DocumentRight Arrow Icon
Background image of page 2
1 · sentinel.cc · 2010-11-18 07:06 · David Nicol /* sentinel.cc shows linked lists insert, delete, list using "sentinel" nodes */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <limits.h> typedef struct listNode node; struct listNode { int val; node *next; }; node headNode; node tailNode; /* insert in increasing order */ int Insert ( int val) { /* create a node for the new value, ensure malloc has given us space */ node *n = (node *)malloc( sizeof (node)); if ( n == NULL ) return -1; n->val = val; /* "here" always points to a node that will appear before n. scan until here->next->val >= n->val. We know tailNode.val is larger than any insertion */ node *here = &headNode; while ( here->next->val < n->val ) { here = here->next; } /* put in the new item */ n->next = here->next; here->next = n; return 1; } /* remove least item */ void DeleteFront() { node *ptr = headNode.next; /* make sure we aren't trying to delete from an empty list */ if headNode.next = headNode.next->next; free(ptr); } } /* list elements in list (excluding sentinels) */ void List() { node *ptr = headNode.next; printf( "-----------------------------------\n" ); while ( ptr->next ) { printf( "%p : (%d, %p)\n" NULL :ptr->next); ptr = ptr->next; }
Background image of page 3

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

View Full DocumentRight Arrow Icon
2 · sentinel.cc · 2010-11-18 07:06 · David Nicol printf( "-----------------------------------\n" ); } int main() { /* initialize head and tail sentinel nodes */ headNode.next = &tailNode; headNode.val = -INT_MAX; tailNode.val = INT_MAX; tailNode.next = NULL ; char cmd[20]; while (1) { printf( "(insert N, del, list) > " ); scanf( "%s" , cmd); if ( strcmp(cmd, "insert" )==0 || strcmp(cmd, "Insert" )==0) { int val; /* get the next (integer) argument. Remember that scanf returns the number of matches */ if ( scanf( "%d" /* return code indicates whether operation was successful */ if ( Insert(val) < 0 ) printf( "Unable to insert value %d\n" , val); } continue ; } if ( strcmp(cmd, "del" )==0 || strcmp(cmd, "Del" )==0) { DeleteFront(); continue ; } if ( strcmp(cmd, "list" )==0 || strcmp(cmd, "List" )==0) { List(); continue ; } } }
Background image of page 4
1 · stack.cc · 2010-11-18 07:07 · David Nicol #include <stdio.h> #include <stdlib.h> #include <string.h> enum OpCodes {ERR = -1, PLUS=0, MINUS=1, TIMES=2, DIV=3}; typedef struct nodeStruct node; struct nodeStruct { float number; node *next; }; /* transform non-numeric input into an opcode */ OpCodes InterpretType( char * op) { if ( strcmp(op, "+" ) == 0 ) return PLUS; if ( strcmp(op, "-" ) == 0 ) return MINUS; if ( strcmp(op, "*" ) == 0 ) return TIMES; if ( strcmp(op, "/" ) == 0 ) return DIV; return ERR; } node *Stack; int Push( char
Background image of page 5

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

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

This note was uploaded on 08/29/2011 for the course ECE 265 taught by Professor Kosbar during the Fall '09 term at Missouri S&T.

Page1 / 13

lecture_26_revised - 1 sentinel.cc 2010-11-18 07:06 David...

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

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