0% found this document useful (0 votes)
28 views

IS 2610: Data Structures

This document provides an overview of elementary data structures in C programming. It discusses built-in and new data types, and how data structures allow organizing collections of data through operations. Arrays are described as the simplest data structure, along with dynamic memory allocation. Linked lists and their traversal, insertion, deletion and exchange operations are covered. Other topics include doubly linked lists, strings, abstraction, abstract data types like stacks and queues, and tree traversal.

Uploaded by

sivavendra
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

IS 2610: Data Structures

This document provides an overview of elementary data structures in C programming. It discusses built-in and new data types, and how data structures allow organizing collections of data through operations. Arrays are described as the simplest data structure, along with dynamic memory allocation. Linked lists and their traversal, insertion, deletion and exchange operations are covered. Other topics include doubly linked lists, strings, abstraction, abstract data types like stacks and queues, and tree traversal.

Uploaded by

sivavendra
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

IS 2610: Data Structures

Elementary Data Structures Jan 12, 2004

Data Type
n

Is a set of values and a collection of operations on those values.


q

Inbuilt data types


n n n

Int Float Character Define values to operate (arguments of a function) Define operation (function definition)

New Data types


n n

Sample function definition


#include <stdio.h> int lg(int); main() { int i, N; for (i = 1, N = 10; i <= 6; i++, N *= 10) printf(%7d %2d %9d\n, N, lg(i), N*lg(N)) } Int lg(int N){ int i; for (i = 0;N > 0; i++, N/= 2); return i; }

Data Structure
n

Goal is to build data structures that allow us to handle collections of data


q q

What operations need to be performed? How to implement these operations? Arrays Structures

Simplest way to organize data in C


q q

Software Engineering practice


n

Interface (header file)


q q

Defines data structures Declare functions to be used to manipulate the data structure Of the functions declared in Interface Program that uses the functions declared in the Interface to work at a higher level of abstraction

Implementation (separate c file)


q

Client (main application program)


q

Arrays
n

Most fundamental data structure


q q q

Fixed collection of same-type data Access is made by using an index Contiguously stored
n n

Direct correspondence with memory systems Entire memory can be considered as an array of memory locations, with memory addresses corresponding to the array indices

In C array definition
q

int A1[N]; int A2[N][M]; char str[50];


n

A1[4]?

A1[i] = *(A1+i)?

Suppose you have to pass huge array as an argument?

Array
n Dynamic Memory #define N 1000 main() { int i, a[N]; } n

Allocation
#include <stdlib.h> main(int argc, char* argv) { int i, N = atoi(argv[1]); int *a = malloc(N*sizeof(int ); if (a==NULL) Insufficient memory

Sieve of Eratosthenes

#define N 20 main() { int i, j, a[N]; for (i = 1; i<N; i++) a[i]=1; for (i = 2; i<N; i++) if (a[i]) for (j = i; i*j<N; j++) a[i*j] = 0; for (j = 2; j<N; j++) if (a[i]) printf (%4d \n, i); }

Finding primes 1 indicates prime 0 indicates nonprime

Linked List
n

A set of items where each item is part of a node that also contains a link to a node
q q q

Self referent structures Cyclic structures possible C code


typedef struct node *link; struct node {char ch; link next;} link h = malloc(sizeof *h);

note that h->next denotes the 2 nd node and h->ch denotes The value a

h a e g m NULL

List traversal
n

Print each element of the list


h a
q q q

m NULL

Use while loop Use for loop Inverting a list?

Linked Lists
t

n
h

Insert operation
f x a e g

(t after x)

m NULL

n
h

Delete Operation
a e

x (delete after x) g m NULL

n
h

Exchange Operation
a t1 (exchange nodes after t1 and t2) e g h t2 m

Doubly Linked List


typedef struct node *link; struct node {char ch; link prev; link next;} link h = malloc(sizeof *h); t (insert t after x) fe h a a (delete after h) x c g m

String
n

Variable length array of characters


q

Has a starting point and a string-termination character (\0) at the end


n n

Array based implementation in C Array of characters different from string associated with length
q

String length may change

q q

Many applications involve processing textual data Computers provide access to bytes of memory that correspond directly to characters of strings

Common String functions


n

strlen(a)
for(i=0; a[i] != 0; i++); return i;

strcpy(a, b)
for(i=0; (a[i] = b[i]) != 0; i++); while (*a++ = *b++);

strcmp(a, b)
for(i=0; (a[i] == b[i]) != 0; i++); if (a[i] == 0) return 0; return a[i] b[i]

strcat(a, b)
strcpy(a+strlen(a), b)

Abstraction
n

Layers of abstraction
q q

Abstract model of a bit with binary 0-1 values Abstract model of a machine from from dynamic properties of the values of a certain set of bits Abstract model of a programming language that we realize by controlling the machine with a machine language program Abstract notion of algorithm implemented in C Develop abstract mechanisms for certain computational tasks at a higher level Define objects we want to manipulate
n

Abstract data types


q

New layer of abstraction


q

Represent data in data structures Implement the algorithm

Define operations that we perform on them


n

Abstract Data Type


n n

A data type that is access only through an interface Refer to a program that uses ADT as a client and program that specifies the data type as an implementation
q q

Interface is opaque clients cannot see implementation Benefits of ADTs


n n

Provide an effective mechanism for organizing large software systems Provide ways to limit the size and complexity of interface between algorithms and associated data structures and programs that use the algorithms and data structures ADTs interface defines a precise means of communication

Pushdown Stack ADT


n

An ADT that comprises two basic operations: insert (push) a new item, and delete (pop) the item that was most recently inserted
q

Last in- first out (LIFO Queue)


pop

push

Pushdown-stack ADT interfaces


n

Use in evaluation of arithmetic expression


q

Infix expression (customary way)


n n

Operator comes between the operands 4 + 5 is written as 4 5 + Operator comes after the operands 4 + 5 is written as 4 5 + Operator comes after the operands 4 + 5 is written as 4 5 +

Postfix expression
n n

Postfix expression
n n

void STACKinit(int); int STACKempty(); void STACKpush(Item); Item STACKpop();

Interfaces: Client may use the four operations


q

store in STACK.h

Postfix notation
n

What is the postfix for the following infix


q

6+5*9? 598+46**7+*? 598 71- *+7*?

What is the infix for the following postfix


q q

Note parentheses are not necessary in postfix

Postfix notation and Pushdown Stack


Input is a number Input is an operator

push 5 push 9 push 8 8 9 5

push 8 push 9 eval 17 push 17 1 7 1 5 1 5 6 1 5

Input sequence q 5 9 8 7 1 * + 7 * = 5 (9 8) (7 1) * + 7 * = 5 ((9 8) * (7 1)) + 7 * = (5 + ((9 8) * (7 1))) 7 * = (5 + ((9 8) * (7 1))) * 7

6 5 11

7 11 77

Stack Implementation (Array)


static Item *s; static int N; void STACKinit(int maxN) {s = malloc(maxN*sizeof(Item)); N = 0;} int STACKempty() {return N==0;} void STACKpush(Item item) {s[N++] = item;} Item STACKpop() { return s[--N];} void STACKinit(int); int STACKempty(); void STACKpush(Item); Item STACKpop();

10

Stack Implementation (Linked-list)


n

Assume auxiliary function


typedef struct STACKnode* link; struct STACKnode {Item item; link next;} static link head; link NEW(Item item, link next;} { link x = malloc(sizeof *x); x->item = item; x->next = next; return x; } void STACKinit(int); int STACKempty(); void STACKpush(Item); Item STACKpop();

Write the functions

First-In First Out Queues


n

An ADT that comprises two basic operations: insert (put) a new item, and delete (get) the item that was least recently used
void QUEUEinit(int); int QUEUEempty(); void QUEUEput(Item); Item QUEUEget(); typedef struct QUEUEnode* link; struct QUEUEnode {Item item; link next;} static link head; link NEW(Item item, link next;} { link x = malloc(sizeof *x); x->item = item; x->next = next; return x; }

11

First-class ADT
q

q q

Clients use a single instance of STACK or QUEUE Only one object in a given program Could not declare variables or use it as an argument

A first-class data type is one for which we can have potentially many different instances, and which can assign to variables whichcan declare to hold the instances

First-class data type Complex numbers


n

Complex numbers contains two parts


q q

(a + bi) where i2 = -1; (a + bi) ( c + di) = (ac bd) + (ad + bc)i

Typedef struct {float r; float i;} Complex; Complex COMPLEXinit(float, float) float Re(float, float); float Im(float, float); Complex COMPLEXmult(Complex, Complex)

Complex t, x, tx; t = COMPLEXInit(cos(r), sin(r)) x = COMPLEXInit(?, ?) tx = COMPLEXInit(t, x)

12

First-class data type Queues


typedef struct queue *Q; void QUEUEdump(Q); Q QUEUEinit(int); int QUEUEempty(Q); void QUEUEput(Q, Item); Item QUEUEget(Q); void QUEUEinit(int); int QUEUEempty(); void QUEUEput(Item); Item QUEUEget();

Q queues[M]; for (i=0; i<M; i++) queues[i ] = QUEUEinit(N); . printf(%3d , QUEUEget(queues[i]));

Recursion and Trees


n

Recursive algorithm is one that solves a problem by solving one or more smaller instances of the same problem
q q q

Recursive function calls itself Factorial? Euclids method for finding the greatest Fibonacci numbers? Common divisor
int gcd(int m, int n){ if (n==0) return m; return gcd(n, m%n); }

13

Tree traversal (binary tree)


n

Preorder
q q q

Visit a node, Visit left subtree, Visit right subtree Visit left subtree, Visit a node, Visit right subtree Visit left subtree, Visit right subtree Visit a node

Inorder
q q q

Postorder
q q q

14

You might also like