1 Review of Class on Nov 23:. 2 Chapter 12: Structures and ADTs  Outline  Declaring Structures  Accessing a Member in a structure variable  Initialization.

Slides:



Advertisements
Similar presentations
C and Data Structures Baojian Hua
Advertisements

Chapter 6 Structures By C. Shing ITEC Dept Radford University.
Structures Spring 2013Programming and Data Structure1.
Chapter Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
Data Structures: A Pseudocode Approach with C
Ceng-112 Data Structures I 1 Chapter 3 Linear Lists.
Data Structures: A Pseudocode Approach with C 1 Chapter 5 Contd... Objectives Explain the design, use, and operation of a linear list Implement a linear.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 7 : September 8.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are crated using the class definition. Programming techniques.
1 Review of Chapter 10: String and Pointers. 2 Outline  String:  Representation of a string: \0  Using scanf to read in string  Initilization of strings.
Pointers Chapters 6+9 in ABC. abp 12 int a = 1, b = 2, *p; & - reference operator (address) * - dereference operator (value) p = &a; // *p is now 1 abp.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 17: Linked Lists.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
COMP103 - Linked Lists (Part A)1 Chapter 17 Truly dynamic memory management.
Structures and Lists (and maybe stacks) Chapters 9-10.
1 Review of Class on Nov 30:. 2 Chapter 12: Structures and ADTs  Outline  Declaring Structures  Accessing a Member in a structure variable  Initialization.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 17 Linked.
Linked List C and Data Structures Baojian Hua
1 Structures. 2 C gives you several ways to create a custom data type. –The structure, which is a grouping of variables under one name and is called an.
Self Referential Structure. A structure may not contain a member of its own type. struct check { int item; struct check n; // Invalid };
Data Structures Using C++ 2E
Lecture 25 Self-Referential Structures Linked Lists
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 15: Linked data structures.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
1 Chapter 16-1 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion.
1 STRUCTURES AND POINTERS. 2 A VARIABLE OF THIS COMPOSITE TYPE CAN HAVE MORE THAN ONE VALUE, GROUPED TOGETHER TO DESCRIBE AN ENTITY. THE COMPONENTS OF.
1 CSE 1342 Programming Concepts Lists. 2 Basic Terminology A list is a finite sequence of zero or more elements. –For example, (1,3,5,7) is a list of.
CS 1031 Linked Lists Definition of Linked Lists Examples of Linked Lists Operations on Linked Lists Linked List as a Class Linked Lists as Implementations.
D ATA S TRUCTURE Ali Abdul Karem Habib MSc.IT. P OINTER A pointer is a variable which represents the location of a data item. We can have a pointer to.
CSC2100B Tutorial 2 List and stack implementation.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 17: Linked Lists.
1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and.
1 Chapter 16 Linked Structures Dale/Weems/Headington.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 17: Linked Lists.
The Hash Table Data Structure Mugurel Ionu Andreica Spring 2012.
Linked List Chapter Data Abstraction separates the logical properties of a data type from its implementation LOGICAL PROPERTIES – What are the.
1 Chapter 13-1 Applied Arrays: Lists and Strings Dale/Weems.
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Chapter Lists Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Elementary Data Structures Linked Lists Linked Lists Dale.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
Linked list: a list of items (nodes), in which the order of the nodes is determined by the address, called the link, stored in each node C++ Programming:
Sudeshna Sarkar, CSE, IIT Kharagpur1 Structure and list processing Lecture
1 Linked List. 2 List A list refers to a sequence of data items  Example: An array The array index is used for accessing and manipulation of array elements.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
Structures CSE 2031 Fall March Basics of Structures (6.1) struct point { int x; int y; }; keyword struct introduces a structure declaration.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Abstract Data Types Queues Dale Roberts, Lecturer
Chapter 16: Linked Lists.
C++ Programming:. Program Design Including
Dynamic Allocation Review Structure and list processing
Linked List :: Basic Concepts
Linked Lists Chapter 6 Section 6.4 – 6.6
Review Deleting an Element from a Linked List Deletion involves:
Chapter 16-2 Linked Structures
Sequences 11/27/2018 1:37 AM Singly Linked Lists Singly Linked Lists.
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists [Chapter 4; Chapter 6, pp ]
Linked List.
Chapter 16 Linked Structures
By C. Shing ITEC Dept Radford University
Structures and List Processing
Structures.
CS148 Introduction to Programming II
Linked Lists.
Presentation transcript:

1 Review of Class on Nov 23:

2 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

3 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. members of the structure structure tag name

4 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;

5 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

6 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;

7 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

8 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

9 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};

10 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”, };

11 Chapter 12: Structures and ADTs  Summary  Declaring Structures  Accessing a Member in a structure variable member operator “.”: ostructure_variable.member_name structure pointer operator “ -> ” : opointer_to_structure -> member_name  Initialization of Structures A structure variable can be followed by oan equal sign = and oa list of constants contained within braces oIf there are not enough values, the remaining members are assigned the value zero. Read Chapter 12.1 – 12. 6

12 Class on Nov. 30

13 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

14 include 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; }

15 Self-Referential Structures  self-referential structures  structures with pointer members that point to 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.

16 Self-Referential Structures  Using self-referential structures to implement linear linked lists 1 &b a 2 &c b 3 NULL c 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; data pNext

17 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

18 Linear Linked Lists  What is linear Linked List?  How to implement linear linked lists  create a list  counting and lookup  insertion  deletion

19 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. 1 &b pHead 2 &c 3 NULL struct list{ int data; struct list *pNext; } a, b, c; data pNext

20 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.

21 Linear Linked Lists  What is linear Linked List?  How to implement linear linked lists  create a list  counting and lookup  insertion  deletion

22 Linear Linked Lists  How to implement linear linked lists  Consider the following list: struct linked_list{ char data; struct linked_list *pNext; }; pHead data ………… NULL

23 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

24 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’); pHead ‘A’ NULL struct linked_list{ char data; struct linked_list *pNext; };

25 #include struct linked_list{ char data; struct linked_list *pNext; }; struct linked_list *create_value(char data); #include "list.h" int main(){ struct linked_list *pHead; pHead = (struct linked_list *) create_value('A'); ……. } list.h main.c #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 pHead ‘A’ NULL

26 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); pHead ‘a’‘b’‘c’‘d’‘e’ NULL struct linked_list{ char data; struct linked_list *pNext; };

27 #include 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); …… } 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.h main.c pHead ‘a’‘b’‘c’‘d’‘e’ NULL list.c