C Programming Lecture 11 : Structure. Data Type struct union enum typedef.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
Templates in C++ Template function in C++ makes it easier to reuse classes and functions. A template can be viewed as a variable that can be instantiated.
Enumeration Data Type enum Day {SUN, MON, TUE, WED, THU, FRI, SAT}; Day today; today = WED; if (today == FRI) cout
1 Stacks Chapter 4. 2 Objectives You will be able to: Describe a stack as an ADT. Build a dynamic-array-based implementation of stacks. Build a linked-list.
Data Structures (Second Part) Lecture 3 : Array, Linked List, Stack & Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering.
STACKS AND QUEUES. A LINKED LIST IMPLEMENTATION OF A QUEUE data next data next NULL data next cnt front rear queue node Queue: First-In-First-Out (FIFO)
Exercise 6 : Stack 1.Stack is a data structure that supports LIFO (Last In First Out) operations. - In this exercise, you implement Stack data structures.
CSE1303 Part A Data Structures and Algorithms Lecture A3 – Basic Data Structures (Stacks)
Stacks. What is a stack? Last-in first-out data structure (LIFO) New objects are placed on top Removal restricted to top object Examples?
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
1 CSCD 326 Data Structures I Stacks. 2 Data Type Stack Most basic property: last item in (most recently inserted) is first item out LIFO - last in first.
Stacks.
Structures and Lists (and maybe stacks) Chapters 9-10.
Pointers Example Use int main() { int *x; int y; int z; y = 10; x = &y; y = 11; *x = 12; z = 15; x = &z; *x = 5; z = 8; printf(“%d %d %d\n”, *x, y, z);
Systems Programming Concepts
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
1 C++ Plus Data Structures Nell Dale Chapter 4 ADTs Stack and Queue Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus.
Data Structures. The Stack: Definition A stack is an ordered collection of items into which new items may be inserted and from which items may be deleted.
PRIMITIVE DATA TYPES -Integer -Floating Point -Decimal -Boolean -Character STRINGS -Character Array -Class -String Length -Static -Limited Dynamic -Dynamic.
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.
14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures, ADT Lecture 25 14/3/2002.
1 C++ Plus Data Structures Nell Dale Chapter 4 ADTs Stack and Queue Modified from the slides by Sylvia Sorkin, Community College of Baltimore County -
CSC2100B Tutorial 2 List and stack implementation.
Chapter 19: Program Design Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 19 Program Design.
EASTERN MEDITERRANEAN UNIVERSITY Stacks EENG212 –Algorithms and Data Structures.
Data Structure and c K.S.Prabhu Lecturer All Deaf Educational Technology.
Ch Chapter 4 Basic Data Types and Variables 4.1 Basic Data Types In C TABLE 4.1 Introduction to Basic Data Types in C Type SizeDescription char 1.
CPT: Types/ Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to look at how new types can be created 6. User-defined.
Principles of programming languages 10: Object oriented languages Isao Sasano Department of Information Science and Engineering.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Abstract Data Types and Stacks CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
1 Lecture06: Complex Types 4/18/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
1 Lecture10: Structures, Unions and Enumerations 11/26/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Basic Data Structures (Stacks). 2 Basic Data Structures Stacks Queues Lists.
1 Pointers: Parameter Passing and Return. 2 Passing Pointers to a Function Pointers are often passed to a function as arguments  Allows data items within.
Structures CSE 2031 Fall March Basics of Structures (6.1) struct point { int x; int y; }; keyword struct introduces a structure declaration.
ENEE150 – 0102 ANDREW GOFFIN Abstract Data Types.
114 3/30/98 CSE 143 Collection ADTs [Chapter 4] /30/98 Collection ADTs  Many standard ADTs are for collections  Data structures that manage groups.
Chapter 6. Character String Types It is one in which the values consists of sequences of characters. How to Define a variable contain a string? In a programming.
Chapter 6 Structures Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
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)
Abstract Data Types and Stacks CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington Last updated: 2/17/
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A3 – Basic Data Structures (Stacks)
Stacks.
Abstract Data Types and Stacks
typedef typedef int Index; typedef char Letter; Index i; i = 17;
Some examples.
Abstract Data Types (ADTs)
Stack ADT & Modularity 2 implementations of the Stack abstract data type: Array Linked List Program design: modularity, abstraction and information hiding.
MON TUE WED THU
Programming Linked Lists.
Introduction to Programming
Chapter 1: Introduction to Data Structures(8M)
Structures CSE 2031 Fall February 2019.
Sun Mon Tue Wed Thu Fri Sat
ECE 103 Engineering Programming Chapter 62 Stack Implementation
Stack.
Sun Mon Tue Wed Thu Fri Sat
Structures CSE 2031 Fall April 2019.
Structures CSE 2031 Fall May 2019.
General List.
2016 | 10 OCT SUN MON TUE WED THU FRI SAT
Sun Mon Tue Wed Thu Fri Sat
Structures EECS July 2019.
Abstract Data Types Stacks CSCI 240
Structures Chapter 4.
Abstract Data Types and Stacks
Presentation transcript:

C Programming Lecture 11 : Structure

Data Type struct union enum typedef

structure A collection of one or more variables possibly of different types, grouped together under a single name. ex) employee payroll record name, address, salary, …

struct Create a new data type that has a structure Ex) point (x,y) coordinate struct point { double x; double y; }; struct point pt; 1) struct point pt = {320.0,200.0}; 2) pt.x=320.0; pt.y=200.0;

struct example using in function : distance from origin (0,0) double dist_from_origin(struct point pt1) { return sqrt(p1.x*p1.x + p2.x*p2.x); } point return struct point makepoint(double x, double y) { struct point temp; temp.x = x; temp.y = y; return temp; }

struct array and pointer struct point pointarray[100]; pointarray[0].x=10; pointarray[0].y=20; struct point* pointPtr; struct point pt = makepoint(20.0,30.0); pointPtr = &pt; pointPtr->x = 30.0; // same as (*pointPtr).x = 30.0; pointPtr->y = 50.0; // same as (*pointPtr).y = 50.0;

struct and class class Extend struct to the concept of abstract data type(ADT) ADT = attributes + methods OOP (Object Oriented Programming)

union A user-defined data type that may hold objects of different types and sizes At any given time, contains only one object from its members Variable Size : the largest member in its member-list. union u_tag { int ival; float fval; char cval; } u; if (utype == INT) u.ival=3; else if (utype == FLOAT) u.fval=3.5; else if (utype == CHAR) u.cval=‘c’;

union example #include union NumericType { int iValue; long lValue; double dValue; }; int main() { union NumericType Values = { 10 }; // first member(iValue)=10 printf("%d\n", Values.iValue); Values.dValue = ; printf("%f\n", Values.dValue); printf("%d\n",sizeof(Values)); return 0; } Output

enum enumerate?, enumerator? A user-defined type defining a set of named constants (integer) Ordered list of integers Default integer starts from 0 Makes code easier to read Can use in expressions like ints

enum enum days {mon, tue, wed, thu, fri, sat, sun}; // Same as: // #define mon 0 // #define tue 1 //... // #define sun 6 enum days {mon=3, tue, wed, thu=9, fri, sat, sun}; // Same as: // #define mon 3 // #define tue 4 // #define wed 5 // #define thu 9 // #define fri 10 // #define sat 11 // #define sun 12

enum example code #include enum days {mon, tue, wed, thu, fri, sat, sun}; int main() { enum days day; // Same as: int day; for(day = mon; day <= sun; day++) { if (day == wed) { printf(“WEDNESDAY\n"); } else { printf("day = %d\n", day); }

typedef Creates new data type names typedef int Length; Length len, maxlen; typedef char *Str; Str p, *lineptr; const int MAXLINES=100; lineptr = new Str[MAXLINES];

typedef and struct typedef struct p { double x; double y; } Point; Point x, y; Point makepoint(double x, double y); A new data type, Point, is created. Point *z; z=(Point*)malloc(sizeof(Point)); // dynamic allocation z->x=3.5; z->y=2.7;

Ex) complex number #include typedef struct { float re; // real number float im; // imaginary number } COMPLEX ; int main() { COMPLEX x, y, z, *p; x.re = 1.0; x.im = 2.0; y.re = 5.0; y.im = 10.0; p = &z; p->re = x.re + y.re; p->im = x.im + y.im; printf("x = %3.1f + %3.1f i \n", x.re, x.im); printf("y = %3.1f + %3.1f i \n", y.re, y.im); printf("x + y = %3.1f + %3.1f i \n", p->re, p->im); return 0; } output: x = i y = i x + y = i

Stack LIFO(Last In First Out) Last data pushed is popped out first. Stack Operations (stack : s, d : data) Push(s,d) Insert data at top of stack Pop(s) Delete and return the data at top of stack IS_EMPTY(s) IS_FULL(s)

Implementation of Stack (using array and structure) Interface design Stack *mkStack(); void push(Stack *s, int item); int pop(Stack *s); int isEmpty(const Stack *s); int isFull(const Stack *s);

Stack.h #define MAX_SIZE 10 struct _stack { int data[MAX_SIZE]; int top; }; typedef struct _stack Stack; Stack *mkStack(); void push(Stack *s, int item); int pop(Stack *s); int isEmpty(const Stack *s); int isFull(const Stack *s); void error(const char *msg);

Stack.c #include "stack.h" #include void error(const char *msg) { printf(“error : %s “,msg); printf(“Program Finished.\n"); exit(-1); } Stack * mkStack() { Stack *s = (Stack *) malloc(sizeof(Stack)); s->top = 0; return s; } void push(Stack *s, int item) { if (isFull(s)) error(“Stack Full”); s->data[s->top++] = item; } int pop(Stack *s) { if (isEmpty(s)) error(“Stack Empty"); return s->data[--s->top]; } int isEmpty(const Stack *s) { if (s->top == 0) return 1; else return 0; } int isFull(const Stack *s) { if (s->top == MAX_SIZE) return 1; else return 0; }

main.c #include #include "stack.h" int main() { int data; int n, i; Stack *s; printf(“Get # of input :\n"); printf(“(between 1 and 10)"); scanf("%d", &n); if (n 10) error(“wrong number of stack size"); s = mkStack(); printf("%d integer input : ", n); for (i = 0; i < n; ++i) { scanf("%d", &data); push(s, data); } printf(“printed in reverse order: "); while (! isEmpty(s)) { data = pop(s); printf("%d ", data); } printf("\n"); return 0; }

Implementation of Stack (using dynamic allocation) Interface design Stack *mkStack(); void push(Stack *s, int item); int pop(Stack *s); int isEmpty(const Stack *s); int isFull(const Stack *s);

Stack2.h struct _stack { int *data; int top; int size; }; typedef struct _stack Stack; Stack *mkStack(int size); void push(Stack *s, int item); int pop(Stack *s); int isEmpty(const Stack *s); int isFull(const Stack *s); void error(const char *msg);

Stack2.c #include "stack2.h" #include void error(const char *msg) { printf(“error: %s “,msg); printf(“program exit\n"); exit(-1); } Stack * mkStack(int size) { Stack *s = (Stack *) malloc(sizeof(Stack)); assert(size > 0); assert(s); s->size = size; s->data = (int *) malloc(sizeof(int)*size); assert(s->data); s->top = 0; return s; } void push(Stack *s, int item) { if (isFull(s)) error(“stack full."); s->data[s->top++] = item; } int pop(Stack *s) { if (isEmpty(s)) error(“stack empty."); return s->data[--s->top]; } int isEmpty(const Stack *s) { if (s->top == 0) return 1; else return 0; } int isFull(const Stack *s) { if (s->top == MAX_SIZE) return 1; else return 0; }

main2.c #include #include "stack2.h" int main() { int data; int n, i; Stack *s; // pointer that points to stack printf(“how many inputs? "); scanf("%d", &n); if (n <= 0) error(“wrong # of inputs."); s = mkStack(n); printf("%d integer input : ", n); for (i = 0; i < n; ++i) { scanf("%d", &data); push(s, data); } printf(“printed in reverse order: "); while (! isEmpty(s)) { data = pop(s); printf("%d ", data); } printf("\n"); return 0; }