1 Structures. 2 User-Defined Types C provides facilities to define one’s own types. These may be a composite of basic types ( int, double, etc) and other.

Slides:



Advertisements
Similar presentations
Pointers.
Advertisements

Linked Lists.
DATA STRUCTURES USING C++ Chapter 5
1 Structures. 2 Structure Basics A structure is a collection of data values, called data members, that form a single unit. Unlike arrays, the data members.
C Language.
C Structures and Memory Allocation There is no class in C, but we may still want non- homogenous structures –So, we use the struct construct struct for.
Structures Spring 2013Programming and Data Structure1.
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Structures. An array allows us to store a collection of variables However, the variables must be of the same type to be stored in an array E.g. if we.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
Introduction to C Programming CE Lecture 10 Data Structures typedef and struct.
Class 3: Linked Lists. cis 335 Fall 2001 Barry Cohen What is a linked list? n A linked list is an ordered series of ‘nodes’ n Each node contains some.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
Programming C/C++ on Eclipe Trình bày : Ths HungNM C/C++ Training.
Self Referential Structure. A structure may not contain a member of its own type. struct check { int item; struct check n; // Invalid };
Data Type. A data type defines a set of values that a variable can store along with a set of operations that can be performed on that variable. Common.
LECTURE 4: INFORMATION REPRESENTATION (PART II) COMP26120: ALGORITHMS AND IMPERATIVE PROGRAMMING.
Pointer Data Type and Pointer Variables
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
Prepared By Ms.R.K.Dharme Head Computer Department.
ABSTRACT DATA TYPES Data types, data structures, abstract data types, Java/C++ classes, C++ structs.
Learners Support Publications Classes and Objects.
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
1 Pointers to structs. 2 A pointer to a struct is used in the same way as a pointer to a simple type, such as an int. Pointers to structs were introduced.
Array, Structure and Union
More C++ Features True object initialisation
Department of Computer Science Data Structures Using C++ 2E Chapter 5 Linked Lists.
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Engineering Problem Solving with C Fundamental Concepts Chapter 7 Structures.
Welcome to Concepts of Pointers. Prepared by:- Sumit Kumar PGT(Computer Science) Kv,Samba.
+ Structures and Unions. + Introduction We have seen that arrays can be used to represent a group of data items that belong to the same type, such as.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
Review 1 Polish Notation Prefix Infix Postfix Precedence of Operators Converting Infix to Postfix Evaluating Postfix.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
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:
CCSA 221 Programming in C CHAPTER 11 POINTERS ALHANOUF ALAMR 1.
Data Structure & Algorithms
C Structs Programming in C++ Fall 2008 Dr. David A. Gaitros
STRUCTURES. INTRODUCTION A structure is same as that of records. It stores related information about an entity. Structure is basically a user defined.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Linked Lists Chapter Introduction To The Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures.
1 Structures & Unions. 2 User-Defined Types C provides facilities to define one’s own types. These may be a composite of basic types ( int, double, etc)
Data Structures and Algorithm Analysis Dr. Ken Cosh Linked Lists.
Pointers and Linked Lists
Chapter 12 – Data Structures
Dynamic Allocation Review Structure and list processing
Linked List :: Basic Concepts
Pointers and Linked Lists
UNIT – I Linked Lists.
Review Deleting an Element from a Linked List Deletion involves:
UNIT-3 LINKED LIST.
Linked lists.
CSCI 3333 Data Structures Linked Lists.
Lecture 9 Structure 1. Concepts of structure Pointers of structures
More About Data Types & Functions
Classes and Objects.
Chapter 17: Linked Lists.
Data Structures & Algorithms
Data Structures & Algorithms
A simple function.
Linked lists.
Presentation transcript:

1 Structures

2 User-Defined Types C provides facilities to define one’s own types. These may be a composite of basic types ( int, double, etc) and other user-defined types. The most common user-defined type is a structure, defined by the keyword struct.

3 Structures A structure is a collection of one or more variables, possibly of different types, grouped together under a single name Structures are user-defined aggregate types. They assist program organisation by –Grouping logically related data, and giving this set of variables a higher-level name and more abstract representation. –Reducing the number of parameters that need to be passed between functions. –Providing another means to return multiple values from a function.

4 Structure Syntax A structure is defined by the keyword struct followed by a set of variables enclosed in braces. Consider the following structure to represent a person’s details. struct Personnel { char name[100]; int age; double height; }; The variables name, age and height are called members of the structure type Personnel.

5 Declaring Structure Variables There are two ways to define variables of a particular structure type. 1.Declare them at the structure definition. struct Personnel { char name[100]; int age; double height; } p1, p2, p3; /* Define 3 variables */ 2.Define the variables at some point after the structure definition. struct Personnel p1, p2, p3; /* Define 3 variables */

6 Initialising Structure Variables A structure may be initialised when it is defined using brace notation. struct Personnel captain = {“Fred”, 37, 1.83}; The order of values in the initialise list matches the order of declarations in the structure.

7 Accessing Members Members of a structure type may be accessed via the “. ” member operator. struct Personnel captain; strcpy(captain.name, “Fred”); captain.age = 37; captain.height = 1.83; printf(“%s is %d years old.”, captain.name, captain.age);

8 Nested Structures Structures may be defined inside other structures. struct first { struct second { int a; }s; double amount; }f; To access lower-level members, need to use member operator multiple times. f.s.a = 2.1; f.amount = 75.4;

9 Operations on Structures Structure types only support one of the operations that are permitted on primitive types. –Assignment (ie., copying) is permitted –All other operations (ie., arithmetic, relational, logical) are not allowed struct Personnel p1 = {“Fred”, 37, 1.83}; struct Personnel p2; p2 = p1; /* Valid. */ if (p1 == p2) /* Invalid. Won’t compile. */ printf(“People are equal\n"); if (strcmp(p1.name, p2.name) == 0 && /* Valid. */ p1.age == p2.age && p1.height == p2.height) printf("People are equal\n");

10 Structures and Functions Structures may be passed to functions and returned from functions. –Like all variables, they are passed by value

11 Pointers to Structures Defining pointers is the same as for variables of primitive types struct Personnel captain = {“Fred”, 37, 1.83}; struct Personnel *pp; pp = &captain; pp->age = 38; /* captain.age is now 38. */

12 Arrays of Structures The definition of arrays of structure types is the same as for arrays of primitive types. struct Personnel pa[10];

13 Self-Referential Structures A structure may not contain a variable of its own type. struct Node { int item; struct Node n; /* Invalid */ }; However, a structure may contain a pointer, self referential structure struct Node { int item; struct Node *pn; /* Valid */ };

14

15 Example: A Linked List Linked lists come in two basic varieties: singly linked and doubly linked. We describe here a simple version of a singly linked list. List consists of a set of nodes, where each node contains an item and a pointer to another list node. struct List { int item; struct List *next; }; (Here we have chosen an int as the contained item. Any other type(s) may be used.)

16 Singly Linked List List is formed by connecting the pointer of one node to the address of the next. We keep a pointer to the head of the list. This permits traversal. The end of the list is marked by a NULL pointer. Example, to start at the head of the list and traverse to the end node: struct List *node = head; while (node->next != NULL) node = node->next; printf(“Last node item: %d“, node->item);

17 Linked-List Properties Linked-Lists are useful because they can be grown (or shrunk) very easily. Unlike arrays, there are no issues of reallocating memory and copying data. Nodes can even be inserted (or removed) from midway along the list without difficulty (and efficiently).

18 Adding Nodes to a List Show example code for adding a node to the end of the list. Show example code for adding a node midway through the list.

19 Splicing in a New Node Remember that everything is manipulated as an address. Consider the pointer variables, eg., –node is address 0x4D –node->next is address 0xA1 –newnode is address 0xB6 0x4D0xA1 Splicing: newnode->next = node->next; assign to 0xA1 node->next = newnode; assign to 0xB6 0xB6 0x4D 0xA1