C Programming : Elementary Data Structures 2009/04/22 Jaemin

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Linked Lists CSE 2451 Matt Boggus. Dynamic memory reminder Allocate memory during run-time malloc() and calloc() – return a void pointer to memory or.
Data Structure Lecture-5
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 7 : September 8.
Linked Lists. Outline Why linked lists? Linked lists basics Implementation Basic primitives ­Searching ­Inserting ­Deleting.
C Programming : Dynamic memory allocation & Structures 2008/11/19 Made by Jimin Hwa Edited and presented by Souneil Park
1 Chapter 6 Priority Queues (Heaps) General ideas of priority queues (Insert & DeleteMin) Efficient implementation of priority queue Uses of priority queues.
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.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L12 (Chapter 20) Lists, Stacks,
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Introduction to C Programming CE Lecture 19 Linear Linked Lists.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
Lists ADT (brief intro):  Abstract Data Type  A DESCRIPTION of a data type  The data type can be anything: lists, sets, trees, stacks, etc.  What.
 2007 Pearson Education, Inc. All rights reserved C Data Structures.
ECE 103 Engineering Programming Chapter 61 Abstract Data Types Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material.
Chapter 14 Dynamic Data Structures Instructor: Kun-Mao Chao ( 台大資工 趙坤茂 )
Lists 1. Introduction Data: A finite sequence of data items. Operations: Construction: Create an empty list Empty: Check if list is empty Insert: Add.
CSC2100B Tutorial 2 List and stack implementation.
Lists, Stacks and Queues in C Yang Zhengwei CSCI2100B Data Structures Tutorial 4.
Data Structure & Algorithm
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Programming Practice 3 - Dynamic Data Structure
Introduction to Data Structures and Algorithms
Subject Name : Data Structure Using C Title : Linked Lists
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
ENEE150 – 0102 ANDREW GOFFIN Dynamic Memory. Dynamic vs Static Allocation Dynamic  On the heap  Amount of memory chosen at runtime  Can change allocated.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
 Memory from the heap  Dynamic memory allocation using the new operator  Build a dynamic linked list  Another way of traversing a linked list 
1 Midterm 1 on Friday February 12 Closed book, closed notes No computer can be used 50 minutes 4 questions Write a function Write program fragment Explain.
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.
Data Structures - Prabir Sarkar. AGENDA Stack Queue Linked List Trees Graphs Searching and Sorting Algorithm.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
CS6045: Advanced Algorithms Data Structures. Dynamic Sets Next few lectures will focus on data structures rather than straight algorithms In particular,
UNIT-II Topics to be covered Singly linked list Circular linked list
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R2. Elementary Data Structures.
Data Structure By Amee Trivedi.
Chapter 12 – Data Structures
Linked List :: Basic Concepts
Sorted Linked List Same objective as a linked list, but it should be sorted Sorting can be custom according to the type of nodes Offers speedups over non-sorted.
Lectures linked lists Chapter 6 of textbook
Data Structure Interview Question and Answers
12 C Data Structures.
Data Structures and Algorithms
Stack and Queue APURBO DATTA.
CSCE 210 Data Structures and Algorithms
Chapter 20: Binary Trees.
CMSC 341 Lecture 5 Stacks, Queues
Lists.
Chapter 21: Binary Trees.
Lesson Objectives Aims
Chapter 8: Data Abstractions
Review & Lab assignments
Data Structures and Algorithms
Pointers & Dynamic Data Structures
LINEAR DATA STRUCTURES
Presentation transcript:

C Programming : Elementary Data Structures 2009/04/22 Jaemin

Contents Data Structure Stack Queue Dynamic Memory Allocation Linked List Tree 2

Data Structure Data + Structure Data Handling rule Point Input Output Properties Examples Stack Queue Tree 3

Stack Storage layout: Last-In, First-Out (LIFO) Primitive operations: push, pop, isEmpty, isFull 4 82 Stack: isEmpty() == true push(8) push(2); push(1); push(7); isFull() == true Stack grows pop() == 7 Stack grows

Stack Implementation(1/3) A simple implementation in C Use an array to hold elements stored in a stack. “top” points the most recently pushed element. Primitive functions int isEmpty(); int isFull(); void push(int x); int pop(); 5 Stack: 8 top

Stack Implementation(2/3) 6 int isEmpty() { if (top<0) return 1; /* true */ else return 0; /* false */ } int isFull() { return ( (top==7) ? 1 : 0 ); } /* Stack */ int stack[8]; /* top refers the array index of the most recently pushed item */ int top = -1;

Stack Implementation(3/3) 7 void push(int x) { if (top>=7) { /* STACK OVERFLOW */ return; } stack[++top] = x; /* top refers the most recently pushed item */ return; } int pop() { int err = ; if (top<0) { /* STACK IS EMPTY */ return err; } return stack[top--]; }

Queue Storage layout: First-Come, First-Served (FCFS) Primitive operations: put, get, isEmpty, isFull 8 82 Queue: isEmpty() == true put(8) put(2); put(1); put(7); isFull() == true Queue grows get() == 8

Queue Implementation(1/4) A simple implementation in C Use an array to hold elements stored in a queue. “head” points the next empty slot. “tail” points the oldest element. Primitive functions int isEmpty(); int isFull(); void put(int x); int get(); 9 Queue: 8217 tailhead

Queue Implementation(2/4) Circular Queue get() == 8; get() == 2; get() == 1; put(14); put(11); head=1tail= head=1tail= get() == 7; tail=3head=7 tail=0 head=7

Queue Implementation(3/4) 11 int isEmpty() { if (head==tail) return 1; else return 0; } int isFull() { if (((head+1) % 8) == tail) return 1; else return 0; } /* Circular queue */ int queue[8]; /* head refers the next empty slot */ int head = 0; /* tail refers the oldest element */ int tail = 0;

Queue Implementation(4/4) 12 void put(int x) { if (is_full()) { /* QUEUE OVERFLOW */ return; } queue[head] = x; head = (head+1) % 8; /* if (head == 7) head = 0; else head++; */ } int get() { int err = ; int ret; if (is_empty()) { /* STACK IS EMPTY */ return err; } ret = queue[tail]; tail = (tail+1) % 8; return ret; }

C Array The allocated space is not flexible Data insertion/deletion is difficult index: ?

Dynamic Memory Allocation Static Allocation Determined when program starts. Dynamic Memory Allocation Determined during the running time. 14

Dynamic Memory Allocation Synopsis Allocates size bytes and returns a pointer to the allocated memory. 15 int *pi; int size, i; scanf(“%d”, &size); pi = (int *)malloc(sizeof(int) * size); // (type): static type casting for (i = 0; i < size; i++)// Initialization pi[i] = -1; void* malloc(size_t size) /* : malloc/calloc/realloc */

Dynamic Memory Deallocation Synopsis Frees the memory space pointed by ptr. Frees(Deallocates) the dynamically allocated memory space 16 void free(void *ptr) /* : free */ int *pi; int size, i; scanf(“%d”, &size); pi = (int *)malloc(sizeof(int) * size); if (pi != NULL) { free(pi); pi = NULL; }

Linked List An alternative to array. Data structure in which objects are arranged in a linear order Consists of nodes, each containing arbitrary data fields and link pointing to the next nodes. The size of a linked list would be changed in runtime Node LinkData field HEAD X

Linked List Implementation An node is represented in a C structure. malloc() is used to dynamically create node structure 18 typedef struct node_t { int data; struct _node_t *next; } node_t; node_t head; head.next = NULL; /* Linked list is empty */ node_t* create_node(int d) { node_t *n = (node_t*)malloc( sizeof(node_t)); if (!n) return NULL; n->data = d; n->next = NULL; return n; }

Primitive Functions - Search 19 void search (int d) { node_t *p; for (p = head.next; p != NULL ; p = p->next) { if (p->data == d) break; } if (p == NULL) return NULL; else return p; }

Primitive Functions – Insert prev HEAD X originally prev  next n void insert (node_t *prev, node_t *n) { node_t *next = prev->next; prev->next = n; n->next = next; }

Primitive Functions – Delete 21 int delete (node_t *d) { int x = d->data; node_t *prev; for (prev = &head; prev->next != d; prev = prev->next) /* DO Nothing */ ; prev->next = d->next; free(d); return x; } 821 prev HEAD X originally d  nextd

Doubly Linked List Has two links pointing to the next and previous nodes 22 Next Prev HEAD 8 821

Doubly Linked List Implementation 23 void insert (node_t *prev, node_t *n) { node_t *next = prev->next; prev->next = n; n->prev = prev; n->next = next; next->prev = n; } int delete (node_t *d) { node_t *prev = d->prev; node_t *next = d->next; int x = d->data; prev->next = next; next->prev = prev; free(d); return x; }

Array vs. Linked List 24 About memory savings? ArrayLinked List Indexing (random access)O(1)O(n) Inserting / Deleting at endO(1)O(1) or O(n) Inserting / Deleting at middleO(n)O(1) ResizeDifficultEasy LocalityGreatBad

Tree Connected, acyclic, and undirected graph. Binary tree, heap tree, B+ tree, etc… Root node Leaf node Parent Child

Binary Tree Each node has at most two children. Implementation An node is represented as a structure. Use malloc() to dynamically create an node structure. 26 root typedef struct _node { int data ; /*struct _node *parent ; */ struct _node *left ; struct _node *right ; } node_t ; node_t *root = NULL; /* Tree is empty */ node_t* create_node (int d){ node_t *n; n = (node_t*)malloc( sizeof(node_t)); if (!n) return NULL; n->data = d; /* n->parent = NULL; */ n->left = NULL; n->right = NULL; return n; }

Binary Tree null right root left right root

Tree Primitive Functions null left right root node_t *search(int d); node_t *insert(int d); void delete(int d);

Tree Primitive Functions Depth-first search (DFS) One of the algorithm for traversing a tree or graph. 29 node_t *DFS(int d) { return _DFS(d, root); } node_t *_DFS(int d, node_t *cur){ node_t *n; if (cur == NULL) return NULL; else if (cur->data == d) return cur; n = _DFS(d, cur->left); if (n != NULL) return n; else return _DFS(d, cur->right); }

Lab #7 Implement ‘Binary tree’ With input numbers, make binary tree. In this case, left children must less than parent, and right greater. There is no same number in the input sequence. Input is entered by keyboard. (use scanf()) Implement with linked list scheme. Print the list in the increasing order Answer that given number is in which level Function declaration void insert_number(int num); void delete_number(int num); void print(); // increasing order int level(int num); 30

The End Any Question? 31