1 / 11

Data Type planet_t and Basic Operations

Data Type planet_t and Basic Operations. Abstract Data Type (ADT) is a data type combined with a set of basic operations. Case Study: Complex Number ADT. Complex data type typedef struct{ double real, imag; } complex_t; Operations int scan_complex(complex_t *c);

Download Presentation

Data Type planet_t and Basic Operations

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Data Type planet_t and Basic Operations • Abstract Data Type (ADT) is a data type combined with a set of basic operations

  2. Case Study: Complex Number ADT • Complex data typetypedef struct{ double real, imag;} complex_t; • Operations • int scan_complex(complex_t *c); • void print_complex(complex_t c); • complex_t add_complex(complex_t c1, complex_t c2); • complex_t subtract_complex(complex_t c1, complex_t c2); • complex_t multiply_complex(complex_t c1, complex_t c2); • complex_t divide_complex(complex_t c1, complex_t c2); • complex_t abs_complex(complex_t c);

  3. Partial Implementation of Complex ADT (I)

  4. Partial Implementation of Complex ADT (II)

  5. Partial Implementation of Complex ADT (III)

  6. Partial Implementation of Complex ADT (IV)

  7. Partial Implementation of Complex ADT (V)

  8. Parallel Array vs. Array of Structures • Assume that there are three recordsfirst_name last_name ID payment dateTom Black 12345 300.00 980130 Alice Smith 23456 1200.50 980220 Jane White 14567 900.00 980701 • Two ways to store the records • Parallel array:char first_name[3][20];char last_name[3][20];int id[3];double payment[3];int date[3]; • Array of Structurestypedef struct{ char first_name[20]; char first_name[20]; int id; double payment; int date;} customer_t;customer_t record[3]; • Advantages and disadvantages

  9. Structures with Pointer Components typedef struct node { char data; struct node *next; }; • The structure type node contains a pointer member of the same type • This type of structure can be used to form a linked list data structure • A linked list is a sequence of nodes each contains the address of the next node except the last node p a b c d

  10. Use linked list to implement a Stack ADT • Node structure type typedef struct node { char data; struct node *next; }; • Operations • node *create(void); • Void freeStack(node *stack); • Void push(char inputdat, node *stack); • Char pop(node *stack); • Char top(node *stack); • Int isempty(node *stack);See demo • Advantages: assign memory location dynamically • assign when needed, while array can not, must assign fixed length before using it • Nodes of a linked list may not stored in continuous in memory, while an array is stored continuously in memory • Disadvantages: • Linear access not random access • More overhead and relatively slower

  11. Functions related to memory allocation in stdlib • Memory allocation function call will assign memory location to variables, arrays in Heap region of memory. • void *malloc(size_t size); • Allocates the requested memory and returns a pointer to it. The requested size is size bytes. The value of the space is indeterminate. On success a pointer to the requested space is returned. On failure a null pointer is returned. • void *calloc(size_t nitems, size_t size); • Allocates the requested memory and returns a pointer to it. The requested size is nitems each size bytes long (total memory requested is nitems*size). The space is initialized to all zero bits. On success a pointer to the requested space is returned. On failure a null pointer is returned. • void free(void *ptr); • Deallocates the memory previously allocated by a call to calloc, malloc, or realloc. The argument ptr points to the space that was previously allocated. If ptr points to a memory block that was not allocated with calloc, malloc, or realloc, or is a space that has been deallocated, then the result is undefined. No value is returned. • sizeof • Is a key word, not a function. e.g. sizeof(int)

More Related