Presentation is loading. Please wait.

Presentation is loading. Please wait.

Structures and Lists (and maybe stacks) Chapters 9-10.

Similar presentations


Presentation on theme: "Structures and Lists (and maybe stacks) Chapters 9-10."— Presentation transcript:

1 Structures and Lists (and maybe stacks) Chapters 9-10

2 These are equivalent Struct definition Structs struct card { int pips; int suit; }; struct card c1, c2; struct card { int pips; int suit; } c1, c2; Varaible declaration

3 Structs c1.pips = 3; c1.suit = 's'; c2 = c1; typedef struct card card; card c3, c4, c5;

4 Structs struct fruit { char *name; int calories; }; struct vegtable { char *name; int calories; }; struct fruit a; struct vegtable b;

5 Structs struct card { int pips; int suit; } desk[52]; --------------------------------- struct { int day, month, year; char day_name[4]; char month_name[4]; } yesterday, today, tomorrow;

6 Structs typedef struct { float re; float im; } complex; complex a, b, c[100];

7 in the header file: “class_info.h” #define CLASS_SIZE 100 struct student { char *last_name; int student_id; char grade; };

8 in a source file #include "class_info.h" int main( void ) { struct student tmp, class[CLASS_SIZE];..... tmp.grade = 'A'; tmp.last_name = "Casanova"; tmp.student_id = 9100017; Using the structs and definitions from the h file

9 and in another source file #include "class_info.h" int fail( struct student class[] ) { int i = 0, cnt = 0; for ( i = 0; i < CLASS_SIZE; ++i ) cnt += class[ i ].grade == 'F'; return cnt; } Count the number of failing grades

10 in the header file: “complex.h” struct complex { double re; double im; }; typedef struct complex complex; real part imaginary part

11 #include "complex.h" void add( complex *a, complex *b, complex *c ) { a → re = b → re + c → re; a → im = b → im + c → im; } Accessing a struct data member from a pointer: -> (the → is just for the presentation!) Accessing a struct data member from a pointer: -> (the → is just for the presentation!) a = b + c

12 Declarations and Assignments struct student tmp, *p = &tmp; tmp.grade = 'A'; tmp.last_name = “Casanova”; tmp.student_id = 9100017; ExpressionEquivalent ExpressionValue tmp_gradep → gradeA tmp_last_namep → last_nameCasanova ( *p ).student_idp → student_id9100017 *p → last_name + 1( *( p → last_name ) ) + 1D *( p → last_name + 2 )( p → last_name )[ 2 ]s

13 Passing Structs empolyee_data update ( empolyee_data e ) {..... prinf( “Input the department number: ” ); scanf( “%d”, &n ); e.department.dept_no = n;..... return e; } e is a copy of the original struct the local struct is modified the copy is returned

14 void update ( empolyee_data *p ) {..... prinf( “Input the department number: ” ); scanf( “%d”, &n ); p → department.dept_no = n;..... } empolyee_data e; update(&e); Passing Structs a pointer to the original struct is passed the original struct is modified

15 Initialization card c = { 13, ‘h’ }; complex a[3][3] = { { { 1.0, -0.1 }, { 2.0, 0.2 }, { 3.0, 0.3 } }, { { 4.0, -0.4 }, { 5.0, 0.2 }, { 6.0, 0.6 } }, }; struct fruit frt = { “plum”, 150 }; the king of hearts a[ 2 ][ ] is assigned zeros

16 Initialization struct home_address { char *street; char *city_and_state; long zip_code; } address = { “87 wost street”, “aspen, colorado”, 80526 }; struct home_address previous_adress = { 0 };

17 struct pcard { unsigned pips: 4; unsigned suit: 2; }; struct abc { int a : 1, b : 16, c : 16; } x; struct small_integer { unsigned i1: 7, i2: 7, i3: 7, : 11, i4: 7, i5 : 7, i6 : 7; } a packed representation align to next word

18 Linked Lists

19 Self-referential Structures struct list { int data; struct list * next; }; datanext struct lista,b,c;

20 a.data = 1; b.data = 2; c.data = 3; a.next = b.next = c.next = NULL; 1NULL2 3

21 a.next = &b; b.next = &c; 123NULL a.next -> next ->data has the value of 3

22 in the file: “list.h” #include typedef char DATA; struct linked_list { DATA d; struct linked_list *next; }; typedef struct linked_listELEMENT; typedef ELEMENT*LINK; We will use chars in the example

23 Iterative List Creation LINK string_to_list( char s[] ) { int i = 0; LINK head = NULL, tail = NULL; if ( s[0] != ‘\0’ ) { head = ( ELEMENT* )malloc( sizeof( ELEMENT ) ); head→d = s[0]; tail = head; String is not empty

24 for ( i=1; s[i] != ‘\0’; ++i ) { tail→next = ( ELEMENT* )malloc( sizeof( ELEMENT ) ); tail = tail→next; tail→d = s[i]; } tail→next = NULL; } return head; } insert elements at the end of the list mark the end of the list

25 Recursive List Creation LINK string_to_list( char s[] ) { LINK head = NULL; if ( s[0] == ‘\0’ ) return NULL; head = ( ELEMENT* )malloc( sizeof( ELEMENT ) ); head→d = s[0]; head→next = string_to_list( s+1 ); return head; } stopping criteria

26 Printing void print_list( LINK head ) { if ( head == NULL ) printf( “NULL\n” ); else { printf( “%c → ”, head→d ); print_list( head→next ); } print a list recursively

27 Counting int count( LINK head ) { if ( head == NULL ) return 0; else return( 1 + count( head→next ) ); } int count_it( LINK head ) { int cnt = 0; for ( ; head !=NULL; head = head→next ) ++cnt; return cnt; } count a list recursively count a list iteratively

28 Concatenate Lists void concatenate( LINK a, LINK b ) { assert(a != NULL); if ( a→next == NULL ) a→next = b; else concatenate( a→next, b ); } concatenate lists a & b, with list a as the head

29 insert void insert( LINK p1, LINK p2, LINK q ) { assert( p1→next == p2 ); p1→next = q; q→next = p2; }

30 Stack

31 An implementation of type Stack #define MAX_LEN 1000 #define EMPTY -1 #define FULL ( MAX_LEN – 1 ) typedef enum boolean { false = 0, true } boolean; typedef struct stack { char s[ MAX_LEN ]; int top; } stack;

32 An implementation of type Stack void reset( stack *stk ) { stk → top = EMPTY; } void push( char c, stack *stk ) { stk → top++; stk → s[ stk → top ] = c; } char pop(stack *stk) { return ( stk → s[ stk → top-- ] ); } Make the stack empty Push a char onto the stack Pop a char off the top of the stack

33 char top( stack **stk ) { return ( stk → s[ stk → top ] ); } boolean empty( const stack *stk ) { return ( ( boolean ) ( stk → top == EMPTY ) ); } boolean full( const stack *stk ) { return ( ( boolean ) ( stk → top == FULL ) ); } An implementation of type Stack The char at the top of the stack Return true if empty Return true if full

34 Test the Stack implementation by Reversing a String #include #include “stack.h” int main(void) { char str[] = “My name is Laura Poh!”; int i = 0; stack s; reset( &s ); printf( “In the string %s\n”, str ); Make the stack empty

35 Test the Stack implementation by Reversing a String for ( i - 0; str[i] != '\0'; ++i ) { if ( !full( &s ) ) push(str[i], &s); } printf( “From the stack: ” ); while ( !empty( &s ) ) { putchar( pop( &s ) ); } putchar ('\n'); return 0;In the tring : My name is Laura Poh! }From the stack: !hoP aruaL is eman yM push a char onto the stack check that the stack is not full check that the stack is not empty pop a char off the stack


Download ppt "Structures and Lists (and maybe stacks) Chapters 9-10."

Similar presentations


Ads by Google