review of class on nov 23 n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Review of Class on Nov 23 : PowerPoint Presentation
Download Presentation
Review of Class on Nov 23 :

Loading in 2 Seconds...

play fullscreen
1 / 27

Review of Class on Nov 23 : - PowerPoint PPT Presentation


  • 98 Views
  • Uploaded on

Review of Class on Nov 23 :. Chapter 12: Structures and ADTs. Outline Declaring Structures Accessing a Member in a structure variable Initialization of Structures The use of typedef Self-Referential Structures Linear Linked Lists. members of the structure. Declaring Structures.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Review of Class on Nov 23 :' - sibley


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
chapter 12 structures and adts
Chapter 12: Structures and ADTs
  • Outline
    • Declaring Structures
    • Accessing a Member in a structure variable
    • Initialization of Structures
    • The use of typedef
    • Self-Referential Structures
    • Linear Linked Lists
declaring structures

members of the structure

Declaring Structures
  • How to declare a structure data type?
    • Example: a structure type to represent a date:

Components: day, month, year

struct date_str{

int day;

int month;

int year;

};

    • This declaration creates the derived date type struct date_str.

structure tag name

declaring structures1
Declaring Structures
  • How to declare variables of a structure type?
    • Declare variables in declaration of a structure type
      • struct date_str{

int day;

int month;

int year;

} date1, date2;

    • Declare variables “struct str_name variable_list;”
      • struct date_str{

int day;

int month;

int year;

};

struct date_str date3, date4;

chapter 12 structures and adts1
Chapter 12: Structures and ADTs
  • Outline
    • Declaring Structures
    • Accessing a Member in a structure variable
    • Initialization of Structures
    • The use of typedef
    • Self-Referential Structures
    • Linear Linked Lists
access a member
Access a member
  • How to access a member?
    • member operator “.”
      • structure_variable.member_name
    • Example:

struct date_str{

int day;

int month;

int year;

} date1, date2;

date1.year = 2000;

data2.year= 2005;

date1.day = date2.day = 10;

date1.month = date2.month = 11;

accessing a member
Accessing a Member
  • How to access a member?
    • structure pointer operator ->

access the members of a structure via a pointer.

pointer_to_structure -> member_name

 (*pointer_to_structure).member_name

    • Example:

struct date_str *pDate = &date1;

(*pDate).day



pDate->day

chapter 12 structures and adts2
Chapter 12: Structures and ADTs
  • Outline
    • Declaring Structures
    • Accessing a Member in a structure variable
    • Initialization of Structures
    • The use of typedef
    • Self-Referential Structures
    • Linear Linked Lists
initialization of structures
Initialization of Structures
  • Initialization
    • A structure variable can be followed by
      • an equal sign = and
      • a list of constants contained within braces
    • Example:

struct date_str{

int day;

int month;

int year;

};

struct date_str date={12, 12, 2000};

initialization of structures1
Initialization of Structures
  • Initialization
    • If there are not enough values, the remaining members are assigned the value zero.
    • Example:

struct student_str{

char last_name[15];

char first_name[15];

int UIN;

int assign[6];

int midterm[3];

int final;

}

strcut student_str s1={“Bush”, “Jenny”, 80002211};

chapter 12 structures and adts3
Chapter 12: Structures and ADTs
  • Summary
    • Declaring Structures
    • Accessing a Member in a structure variable
      • member operator “.”:
        • structure_variable.member_name
      • structure pointer operator “ -> ” :
        • pointer_to_structure -> member_name
    • Initialization of Structures
      • A structure variable can be followed by
        • an equal sign = and
        • a list of constants contained within braces
        • If there are not enough values, the remaining members are assigned the value zero.

Read Chapter 12.1 – 12. 6

chapter 12 structures and adts4
Chapter 12: Structures and ADTs
  • Outline
    • Declaring Structures
    • Accessing a Member in a structure variable
    • Initialization of Structures
    • Self-Referential Structures
    • Linear Linked Lists
    • The use of typedef
slide14

include <stdio.h>

int main(void){

struct list{

int data;

struct list *pNext;

} a, b, c;

struct list* p=&a;

a.data=1;

b.data=2;

c.data=3;

a.pNext = &b;

b.pNext = &c;

c.pNext = NULL;

while (p!=NULL){

printf("%2d ", p->data);

p = p->pNext;

}

}

self referential structures
Self-Referential Structures
  • self-referential structures
    • structures with pointer members that pointto the structure type containing them.
    • Example:

struct list{

int data;

struct list *pNext;

} a, b, c;

member pNext points to the structure type struct list, which contains pNext as a member 

struct list is a self-referential structure.

self referential structures1
Self-Referential Structures
  • Using self-referential structures to implement linear linked lists
        • struct list{
        • int data;
        • struct list *pNext;
        • } a, b, c;
  • a.data=1;
  • b.data=2;
  • c.data=3;
  • a.pNext = &b;
  • b.pNext = &c;
  • c.pNext = NULL;

3

2

1

data

pNext

&c

&b

NULL

c

a

b

chapter 12 structures and adts5
Chapter 12: Structures and ADTs
  • Outline
    • Declaring Structures
    • Accessing a Member in a structure variable
    • Initialization of Structures
    • Self-Referential Structures
    • Linear Linked Lists
    • The use of typedef
linear linked lists
Linear Linked Lists
  • What is linear Linked List?
  • How to implement linear linked lists
    • create a list
    • counting and lookup
    • insertion
    • deletion
linear linked lists1
Linear Linked Lists
  • What is Linear Linked List?
    • data structure hang sequentially.
      • a head pointer that points to the first element of the list,
      • each element points at a successor element,
      • the last element having a link value NULL.
  • struct list{
  • int data;
  • struct list *pNext;
  • } a, b, c;

2

1

3

data

pNext

&c

&b

NULL

pHead

linear linked lists2
Linear Linked Lists
  • Linear Linked Lists
    • A linked list is a very common data structure.
    • It can be used to implement efficient algorithms, such as sorting, searching.
linear linked lists3
Linear Linked Lists
  • What is linear Linked List?
  • How to implement linear linked lists
    • create a list
    • counting and lookup
    • insertion
    • deletion
linear linked lists4

data

data

data

data

data

data

Linear Linked Lists
  • How to implement linear linked lists
    • Consider the following list:

…………

NULL

pHead

struct linked_list{

char data;

struct linked_list *pNext;

};

linear linked lists5
Linear Linked Lists
  • Operations on a linked list

Define functions such that

    • create a linked list
      • from a value of type char
      • from an array of type char
    • counting: the number of elements
    • looking up an element
    • inserting an element
    • deleting an element
linear linked lists6

‘A’

NULL

pHead

struct linked_list{

char data;

struct linked_list *pNext;

};

Linear Linked Lists
  • Operations on a linked list
    • create a linked list from a value:

struct linked_list *create_value(char data);

      • return the head pointer of a link which contains a single item; the data field of this item is data.

struct linked_list *pHead;

pHead = create_value(‘A’);

slide25

‘A’

NULL

pHead

list.h

#include <stdio.h>

struct linked_list{

char data;

struct linked_list *pNext;

};

struct linked_list *create_value(char data);

main.c

#include "list.h"

int main(){

struct linked_list *pHead;

pHead = (struct linked_list *) create_value('A');

…….

}

#include "list.h"

struct linked_list *create_value(char data){

struct linked_list *pHead = NULL;

pHead = (struct linked_list *) malloc(sizeof(struct linked_list));

pHead->data = data; pHead->pNext = NULL;

return pHead;

}

list.c

linear linked lists7

‘a’

‘b’

‘c’

‘d’

‘e’

NULL

pHead

struct linked_list{

char data;

struct linked_list *pNext;

};

Linear Linked Lists
  • Operations on a linked list
    • create a linked list from an array:

struct linked_list *create_array(char data_array[], int n);

      • return the head pointer of a link which contains n items; the data fields of the items are decided by data_array.

char data_array[]={'a', 'b', 'c', 'd', 'e'};

struct linked_list * pHead;

pHead = create_array(data_array, 5);

slide27

‘a’

‘b’

‘c’

‘d’

‘e’

NULL

pHead

#include <stdio.h>

struct linked_list{

char data;

struct linked_list *pNext;

};

struct linked_list *create_array(char data_array[],

int n);

#include "list.h"

int main(){

struct linked_list *pHead;

char data_array[]={'a', 'b', 'c', 'd', 'e'};

pHead = create_array(data_array, 5);

……

}

main.c

list.h

struct linked_list *create_array(char data_array[], int n){

struct linked_list *p=NULL, *pHead = NULL;

int i;

if(n==0)

return NULL;

else{

pHead = (struct linked_list *) malloc(sizeof(struct linked_list));

pHead->data = data_array[0];

pHead->pNext = NULL;

p = pHead;

for (i=1; i<n;i++){

p->pNext = (struct linked_list *) malloc(sizeof(struct linked_list));

p->pNext->data = data_array[i];

p->pNext->pNext = NULL;

p = p->pNext;

}

}

return pHead;

}

list.c