Generic lists Vassilis Athitsos. Problems With Textbook Interface? Suppose that we fix the first problem, and we can have multiple stacks. Can we have.

Slides:



Advertisements
Similar presentations
FIFO Queues CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Advertisements

Introduction to Linked Lists In your previous programming course, you saw how data is organized and processed sequentially using an array. You probably.
Elementary Data Structures: Part 2: Strings, 2D Arrays, Graphs
Module R2 Overview. Process queues As processes enter the system and transition from state to state, they are stored queues. There may be many different.
Pointers & Dynamic Memory Allocation Mugurel Ionu Andreica Spring 2012.
M180: Data Structures & Algorithms in Java
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Linked Lists... An introduction to creating dynamic data structures.
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
More on Dynamic Memory Allocation Seokhee Jeon Department of Computer Engineering Kyung Hee University 1 Illustrations, examples, and text in the lecture.
Linked Lists in C and C++ By Ravi Prakash PGT(CS).
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.
Linked Lists Chained nodes of information create what are called linked lists, with each node providing a link to the next node. A useful feature of linked.
CS Data Structures Chapter 4 Lists.
Introduction to C Programming CE Lecture 19 Linear Linked Lists.
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Self Referential Structure. A structure may not contain a member of its own type. struct check { int item; struct check n; // Invalid };
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
Abstract Data Types and Stacks CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
1 Working with Pointers An exercise in destroying your computer.
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
Review 1 Polish Notation Prefix Infix Postfix Precedence of Operators Converting Infix to Postfix Evaluating Postfix.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Abstract Data Types and Stacks CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
CNG 140 C Programming (Lecture set 12) Spring Chapter 13 Dynamic Data Structures.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
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:
Elementary Data Structures: Part 1: Arrays, Lists CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Abstract Data Types and Stacks CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington Last updated: 2/17/
FIFO Queues CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
STACKS & QUEUES for CLASS XII ( C++).
Programming Circular Linked List.
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
ECE Application Programming
Pointers and Linked Lists
Pointers and Linked Lists
5.13 Recursion Recursive functions Functions that call themselves
Programming Abstractions
Lectures linked lists Chapter 6 of textbook
UNIT – I Linked Lists.
Linked Lists Chapter 6 Section 6.4 – 6.6
Doubly Linked List Review - We are writing this code
UNIT-3 LINKED LIST.
Abstract Data Types and Stacks
Dynamic Memory CSCE 121 J. Michael Moore.
Programmazione I a.a. 2017/2018.
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Pointers and Linked Lists
Chapter 16-2 Linked Structures
Stacks, Queues, and Deques
Popping Items Off a Stack Lesson xx
Pointers and Linked Lists
Programming Linked Lists.
Programming Abstractions
Review & Lab assignments
Linked Lists Chapter 4.
Stacks and Queues.
FIFO Queues CSE 2320 – Algorithms and Data Structures Alexandra Stefan
Dynamic Memory CSCE 121.
Abstract Data Types Stacks CSCI 240
Abstract Data Types and Stacks
Presentation transcript:

Generic lists Vassilis Athitsos

Problems With Textbook Interface? Suppose that we fix the first problem, and we can have multiple stacks. Can we have a stack A that contains integers, and a stack B that contains strings? No. Each stack contains values of type Item. While Item can be set (using typedef) to any type we want, all stacks in the code must contain values of that one type. We have the exact same problem with lists. Let's see how to solve this problem, for both lists and stacks. 2

Links Revisited void *: 3 old definition struct node { Item item; link next; }; typedef struct node * link; new definition struct node { void * item; link next; }; typedef struct node * link;

Links Revisited void *: this is a special type in C. – It means pointer to anything. If item is of type void*, it can be set equal to a pointer to any object: – int *, char *, double *, pointers to structures, … 4 old definition struct node { Item item; link next; }; typedef struct node * link; new definition struct node { void * item; link next; }; typedef struct node * link;

Example: Adding an int to a List 5 To insert integer 7 to a list my_list: struct node { void * item; link next; }; link newLink(void * content) { link result = malloc(sizeof(struct node)); result->item = content; result->next = NULL; }

Example: Adding an int to a List 6 To insert integer 7 to a list my_list: – Allocate memory for a pointer to an int. – Set the contents of the pointer equal to 7. – Add to the list a new link, whose item is the pointer. struct node { void * item; link next; }; link newLink(void * content) { link result = malloc(sizeof(struct node)); result->item = content; result->next = NULL; }

Example: Adding an int to a List 7 struct node { void * item; link next; }; link newLink(void * content) { link result = malloc(sizeof(struct node)); result->item = content; result->next = NULL; } int * content = malloc(sizeof(int)); *content = 7; insertAtEnd(my_list, newLink(content)); To insert integer 7 to a list my_list: – Allocate memory for a pointer to an int. – Set the contents of the pointer equal to 7. – Add to the list a new link, whose item is the pointer.

Example: Adding an int to a List 8 struct node { void * item; link next; }; link newLink(void * content) { link result = malloc(sizeof(struct node)); result->item = content; result->next = NULL; } int * content = malloc(sizeof(int)); *content = 7; insertAtEnd(my_list, newLink(content)); Note: instead of insertAtEnd, we could have used any other insertion function.

Example: Accessing an int in a Link 9 struct node { void * item; link next; }; link n = … // put any code here that produces a link. To access the value of an int stored in link n:

Example: Accessing an int in a Link 10 struct node { void * item; link next; }; link n = … // put any code here that produces a link. To access the value of an int stored in link n: – Call linkItem to get the item stored at n. – Store the result of linkItem to a variable of type int* (use casting). – Dereference that pointer to get the number.

Example: Accessing an int in a Link 11 struct node { void * item; link next; }; link n = … // put any code here that produces a link. int * content = (int *) linkItem(n); // note the casting. my_number = *content; // pointer dereferencing. To access the value of an int stored in link n: – Call linkItem to get the item stored at n. – Store the result of linkItem to a variable of type int* (use casting). – Dereference that pointer to get the number.

Example: Removing an int from a List 12 struct node { void * item; link next; }; void * linkItem(link the_link) { return the_link->item; } To remove a link containing an int:

Example: Removing an int from a List 13 struct node { void * item; link next; }; void * linkItem(link the_link) { return the_link->item; } To remove a link containing an int: – Remove the link from the list. – Get the pointer that is stored as the link's item. – Dereference the pointer to get the int. – Free the link (this we were doing before, too). – Free the int * pointer (this we didn't have to do before).

Example: Removing an int from a List 14 struct node { void * item; link next; }; void * linkItem(link the_link) { return the_link->item; } link a = deleteAtBeginning(my_list); int * content = (int *) linkItem(a); int my_number = *content; free(a); free(content); To remove a link containing an int: – Remove the link from the list. – Get the pointer that is stored as the link's item. – Dereference the pointer to get the int. – Free the link (this we were doing before, too). – Free the int * pointer (this we didn't have to do before).

Example: Removing an int from a List 15 struct node { void * item; link next; }; void * linkItem(link the_link) { return the_link->item; } link a = deleteAtBeginning(my_list); int * content = (int *) linkItem(a); int my_number = *content; free(a); free(content); Note: instead of deleteAtBeginning we could have used any other function that deletes links from a list.

Storing Objects of Any Type in a List The previous examples can be adapted to accommodate objects of any other type that we want to store in a list. To store an object X of type T in a link L: T* P = malloc(sizeof(T)); *P = X; link L = newLink(P); To access an object X of type T stored in a link L: T* P = linkItem(L); T value = *P; 16

The New List Interface See files lists.h and lists.c posted on the course website. NOTE: the new interface allows us to store objects of different types even on the same list. Also, the new implementation makes it efficient to insert at the end of the list, which will be useful later (in FIFO queues). 17