Data Structures and Programming Techniques

Slides:



Advertisements
Similar presentations
DATA STRUCTURES USING C++ Chapter 5
Advertisements

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.
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.
CCSB364 Data Structures & Algorithms Pointer & Linked List.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
Introduction to C Programming CE Lecture 19 Linear Linked Lists.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 20: Binary Trees.
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.
Linked List (Part II). Introduction  Definition of equivalence relation: A relation ≡ over a set S, is said to be an equivalence relation over S iff.
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.
1 Chapter 16 Linked Structures Dale/Weems/Headington.
1 Linked Lists (Lec 6). 2  Introduction  Singly Linked Lists  Circularly Linked Lists  Doubly Linked Lists  Multiply Linked Lists  Applications.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
Data Structure & Algorithms
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.
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.
  A linked list is a collection of components called nodes  Every node (except the last one) contains the address of the next node  The address of.
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.
Lists and Strings 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.
CSE 1342 Programming Concepts
Review Array Array Elements Accessing array elements
C++ Programming:. Program Design Including
Linked List :: Basic Concepts
Linked Data Representations
CSCI-255 LinkedList.
Lectures linked lists Chapter 6 of textbook
Linked Lists Chapter 6 Section 6.4 – 6.6
Data Structure and Algorithms
UNIT-3 LINKED LIST.
Linked lists.
Data Structures 7th Week
Lecture Trees Chapter 9 of textbook 1. Concepts of trees
Lecture 22 Binary Search Trees Chapter 10 of textbook
C Basics.
Module 2 Arrays and strings – example programs.
Computer science C programming language Lesson 5
Programming Languages and Paradigms
Programmazione I a.a. 2017/2018.
LINKED LISTS CSCD Linked Lists.
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.
Introduction to Linked Lists
Chapter 16-2 Linked Structures
Strings, Line-by-line I/O, Functions, Call-by-Reference, Call-by-Value
Linked Data Representations
Multi-Way Search Trees
Sequences 11/27/2018 1:37 AM Singly Linked Lists Singly Linked Lists.
Linked Lists Chapter 4.
Linked List.
Chapter 16 Pointers and Arrays
Chapter 16 Linked Structures
Introduction to Data Structure
Data Structures & Algorithms
Structures.
General List.
Linked lists.
cout << str1;  pear
Data Structures Using C++ 2E
LINEAR DATA STRUCTURES
Data Structures and Programming Techniques
Presentation transcript:

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

Data Structures and Programming Techniques A List ADT A list L of items of type T is a sequence of items of type T on which the following operations are defined: Initialize the list L to be the empty list. Determine whether or not the list L is empty. Find the length of a list L (where the length of L is the number of items in L and the length of the empty list is 0). Select the 𝑖-th item of a list L, where 1≤𝑖 ≤𝑙𝑒𝑛𝑔𝑡ℎ 𝐿 . Replace the 𝑖-th item X of a list L with a new item Y where 1≤𝑖 ≤𝑙𝑒𝑛𝑔𝑡ℎ 𝐿 . Delete any item X from a nonempty list L. Insert a new item X into a list L in any arbitrary position (such as before the first item of L, after the last item of L or between any two items of L). Data Structures and Programming Techniques

Data Structures and Programming Techniques 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

Sequential List Representations We can use an array A[0:MaxSize-1] as we show graphically (items are stored contiguously): FirstFree MaxSize-1 A: x1 x2 x3 x4 Data Structures and Programming Techniques

Advantages and Disadvantages Fast access to the 𝑖-th item of the list in O(1) time. Disadvantages: Insertions and deletions may require shifting all items i.e., an O(n) cost on the average. The size of the array should be known in advance. So if we have small size, we run the risk of overflow and if we have large size, we will be wasting space. Data Structures and Programming Techniques

One-Way Linked Lists Representation We can use chains of linked nodes as shown below: . L: x1 x2 x3 Data Structures and Programming Techniques

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

Data Structures and Programming Techniques Accessing the ith 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

Computational Complexity Suppose that list L has exactly 𝑛 items. If it is equally likely that each of these items can be accessed, then the average number of 𝑛 pointers followed to access the ith item is: 𝐴𝑣𝑒𝑟𝑎𝑔𝑒= 1+2+⋯+𝑛 𝑛 = 𝑛 𝑛+1 2 𝑛 = 𝑛 2 + 1 2 Therefore, the average time to access the ith item is O(𝑛). The complexity bound is the same for inserting before or after the ith item or deleting it or replacing it. Data Structures and Programming Techniques

Comparing Sequential and Linked List Representations List Operation Sequential Representation Linked List Representation Finding the length of L O(1) O(𝑛) Inserting a new first item Deleting the last item Replacing the ith item Deleting the ith item 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

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

Data Structures and Programming Techniques 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. L: x1 x2 x3 Data Structures and Programming Techniques

Data Structures and Programming Techniques 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. LLink . Item RLink LLink Item RLink LLink Item RLink . x1 x2 x3 L: Data Structures and Programming Techniques

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

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. Header Node . x1 x2 x3 L: Data Structures and Programming Techniques

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

Data Structures and Programming Techniques Generalized Lists A generalized list is a list in which the individual list items are permitted to be sublists. Example: (a1, a2, (b1, (c1, c2), b3), a4, (d1, d2), a6) 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

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

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: L: Atom SL Link Atom Item Link Atom Item Link Atom SL Link . False True 4 True 5 False Atom SL Link Atom SL Link Atom SL Link Atom Item Link . False True True 6 Atom Item Link Atom Item Link Atom Item Link . True 1 True 2 True 3 Data Structures and Programming Techniques

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

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

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

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

Data Structures and Programming Techniques 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

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

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

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

Data Structures and Programming Techniques Readings T. A. Standish. Data Structures, Algorithms and Software Principles in C. Chapter 8, Sections 8.1-8.5. Robert Sedgewick. Αλγόριθμοι σε C. Κεφ. 3. Data Structures and Programming Techniques