Lists and Strings Manolis Koubarakis Data Structures and Programming Techniques 1.

Slides:



Advertisements
Similar presentations
Pointers.
Advertisements

DATA STRUCTURES USING C++ Chapter 5
CSEB324 Data Structures & Algorithms
CS Data Structures Chapter 4 Lists. Chain (1/3) Chain: Chain: A singly linked list in which the last node has a null link A singly linked list in.
CSCI2100B Linked List Jeffrey
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
Programming and Data Structure
Programming Languages and Paradigms The C Programming Language.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Elementary Data Structures: Part 2: Strings, 2D Arrays, Graphs
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are crated using the class definition. Programming techniques.
CCSB364 Data Structures & Algorithms Pointer & Linked List.
CS Data Structures Chapter 4 Lists.
Introduction to C Programming CE Lecture 19 Linear Linked Lists.
Data Structures Using C++ 2E
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 15: Linked data structures.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
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.
CHAPTER 3 Lists, Stacks, and Queues §1 Abstract Data Type (ADT) 【 Definition 】 Data Type = { Objects }  { Operations } 〖 Example 〗 int = { 0,  1, 
Dynamic Memory Allocation The process of allocating memory at run time is known as dynamic memory allocation. C does not Inherently have this facility,
Stacks. A stack is a data structure that holds a sequence of elements and stores and retrieves items in a last-in first- out manner (LIFO). This means.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
Linked Lists. A linear linked list is a collection of objects, called nodes, each of which contains a data item and a pointer to the next node in the.
A first look an ADTs Solving a problem involves processing data, and an important part of the solution is the careful organization of the data In order.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
Department of Computer Science Data Structures Using C++ 2E Chapter 5 Linked Lists.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 17: Linked Lists.
1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and.
1 Chapter 16 Linked Structures Dale/Weems/Headington.
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.
1 Linked Lists (Lec 6). 2  Introduction  Singly Linked Lists  Circularly Linked Lists  Doubly Linked Lists  Multiply Linked Lists  Applications.
C LANGUAGE Characteristics of C · Small size
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 5.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Elementary Data Structures Linked Lists Linked Lists Dale.
Department of Computer Science 1 Some Practice Let’s practice for the final a little bit. OK?
Circular linked list A circular linked list is a linear linked list accept that last element points to the first element.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
Data Structure & Algorithms
Chapter 16 Pointers and Arrays Pointers and Arrays We've seen examples of both of these in our LC-3 programs; now we'll see them in C. Pointer Address.
Doubly Linked List Exercises Sometimes it is useful to have a linked list with pointers to both the next and previous nodes. This is called a doubly linked.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists.
UNIT-II Topics to be covered Singly linked list Circular linked list
LINKED LISTS.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
Queues Manolis Koubarakis Data Structures and Programming Techniques 1.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
C++ Programming:. Program Design Including
Linked List :: Basic Concepts
Linked Data Representations
CSCI-255 LinkedList.
Lectures linked lists Chapter 6 of textbook
Data Structure and Algorithms
UNIT-3 LINKED LIST.
Linked lists.
Data Structures 7th Week
BY GAWARE S.R. COMPUTER SCI. DEPARTMENT
Programmazione I a.a. 2017/2018.
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Chapter 16-2 Linked Structures
Linked Data Representations
Sequences 11/27/2018 1:37 AM Singly Linked Lists Singly Linked Lists.
Data Structures and Programming Techniques
Chapter 16 Pointers and Arrays
Chapter 16 Linked Structures
Data Structures & Algorithms
General List.
Linked lists.
Data Structures and Programming Techniques
Presentation transcript:

Lists and Strings Manolis Koubarakis Data Structures and Programming Techniques 1

A List ADT Data Structures and Programming Techniques 2

Lists Lists are more general kinds of containers than stacks and queues. Lists can be represented by sequential representations and linked representations. Data Structures and Programming Techniques 3

Sequential List Representations We can use an array A[0:MaxSize-1] as we show graphically (items are stored contiguously): A: x1x1 x2x2 x3x3 x4x4 MaxSize-1 FirstFree Data Structures and Programming Techniques 4

Advantages and Disadvantages Data Structures and Programming Techniques 5

One-Way Linked Lists Representation We can use chains of linked nodes as shown below: x1x1. L: x2x2 x3x3 Data Structures and Programming Techniques 6

Declaring Data Types for Linked Lists The following statements declare appropriate data types for our linked lists from earlier lectures: typedef char AirportCode[4]; typedef struct NodeTag { AirportCode Airport; struct NodeTag *Link; } NodeType; typedef NodeType *NodePointer; We can now define variables of these datatypes: NodePointer L; or equivalently NodeType *L; Data Structures and Programming Techniques 7

Accessing the i th Item void PrintItem(int i, NodeType *L) { while ((i > 1) && (L != NULL)){ L=L->Link; i--; } if ((i == 1) && (L != NULL)){ printf(“%s”, L->Item); } else { printf(“Error – attempt to print an item that is not on the list.\n”); } Data Structures and Programming Techniques 8

Computational Complexity Data Structures and Programming Techniques 9

Comparing Sequential and Linked List Representations List OperationSequential RepresentationLinked List Representation Finding the length of LO(1)O(n) Inserting a new first itemO(n)O(1) Deleting the last itemO(1)O(n) Replacing the i th itemO(1)O(n) Deleting the i th itemO(n) The above table gives average running times. But time is not the only resource that is of interest. Space can also be an important resource in some applications. Data Structures and Programming Techniques 10

Other Linked List Representations Circular linked lists Two-way linked lists Linked lists with header nodes Data Structures and Programming Techniques 11

Circular linked lists A circular linked list is formed by having the link in the last node of a one-way linked list point back to the first node. The advantage of a circular linked list is that any node on it is accessible by any other node. x1x1 L: x2x2 x3x3 Data Structures and Programming Techniques 12

Two-Way Linked Lists Two-way linked lists are formed from nodes that have pointers to both their right and left neighbors on the list. x1x1 L: x2x2 x3x3.. LLink Item RLink LLinkItem RLink Item LLink Data Structures and Programming Techniques 13

Two-Way Linked Lists (cont’d) Given a pointer to a node N in a two-way linked list, we can follow links in either direction to access other nodes. We can insert a node M either before or after N starting only with the information given by the pointer to N. Data Structures and Programming Techniques 14

Linked Lists with Header Nodes Sometimes it is convenient to have a special header node that points to the first node in a linked list of item nodes. x1x1. L: x2x2 x3x3 Header Node Data Structures and Programming Techniques 15

Linked Lists with Header Nodes (cont’d) Header nodes can be used to hold information such as the number of nodes in the list etc. Data Structures and Programming Techniques 16

Generalized Lists A generalized list is a list in which the individual list items are permitted to be sublists. Example: (a 1, a 2, (b 1, (c 1, c 2 ), b 3 ), a 4, (d 1, d 2 ), a 6 ) If a list item is not a sublist, it is said to be atomic. Generalized lists can be represented by sequential or linked representations. Data Structures and Programming Techniques 17

Generalized Lists (cont’d) The generalized list L=(((1, 2, 3), 4), 5, 6, (7)) can be represented without shared sublists as follows: False True AtomSL Link Atom Link Item SL L: 5 6 AtomItem Link Atom SL Link Atom ItemLink 4 7 True False Item Atom Link True Data Structures and Programming Techniques 18

Generalized Lists (cont’d) The generalized list L=(((1, 2, 3), (1, 2, 3), (2, 3), 6), 4, 5, ((2, 3), 6) can be represented with shared sublists as follows: False True AtomSL Link Atom Link Item SL L: 4 5 AtomItem Link Atom SL Link Atom Link 6 True False Item Atom Link True AtomSL Link Data Structures and Programming Techniques 19

A Datatype for Generalized List Nodes typedef struct GenListTag { GenListTag *Link; int Atom; union SubNodeTag { ItemType Item; struct GenListTag *Sublist; } SubNode; } GenListNode; Data Structures and Programming Techniques 20

Printing Generalized Lists void PrintList(GenListNode *L) { GenListNode *G; printf(“(“); G=L; while (G != NULL){ if (G->Atom){ printf(“%d”, G->SubNode.Item); } else { printList(G->SubNode.SubList); } if (G->Link != NULL) printf(“,”); G=G->Link; } printf(“)”); } Data Structures and Programming Techniques 21

Applications of Generalized Lists Artificial Intelligence programming languages LISP and Prolog offer generalized lists as a language construct. Generalized lists are often used in Artificial Intelligence applications. More in the courses “Artificial Intelligence” and “Logic Programming”. Data Structures and Programming Techniques 22

Strings Strings are sequences of characters. They have many applications: – Word processors – systems – Databases – … Data Structures and Programming Techniques 23

Strings in C A string in C is a sequence of characters terminated by the null character “\0”. Example: To represent a string S==“canine” in C, we allocate a block of memory B at least seven bytes long and place the characters “canine” in bytes B[0:5]. Then, in byte B[6], we place the character “\0”. Data Structures and Programming Techniques 24

A String ADT In C’s standard library you can access a collection of useful string operations by including the header file in your program. These functions define a predefined string ADT. Data Structures and Programming Techniques 25

Examples of String Operations Let us assume that S and T are string variables (i.e., of type char * ). Then: – strlen(S) : returns the number of characters in string S (not including the terminating character ‘\0’ ). – strstr(S,T) : returns a pointer to the first occurrence of string S in string T (or NULL if there is no occurrence of string S in string T ). – strcat(S,T): concatenate a copy of string T to the end of string S and return a pointer to the beginning of the enlarged string S. – strcpy(S,T) : make a copy of the string T including a terminating last character ‘\0’, and store it starting at the location pointed to by the character pointer S. Data Structures and Programming Techniques 26

Concatenating Two Strings char *Concat(char *S, char *T) { char *P; char *temp; P=(char *)malloc(1+strlen(S)+strlen(T)); temp=P; while ((*P++=*S++)!=‘\0’) ; P--; while ((*P++=*T++)!=‘\0’) ; return(temp); } Data Structures and Programming Techniques 27

Readings T. A. Standish. Data Structures, Algorithms and Software Principles in C. Chapter 8, Sections Data Structures and Programming Techniques 28