PROGRAMMAZIONE I A.A. 2017/2018

Similar documents
Advanced Programming. Lists. A list is a data structure based on usage of pointers and dynamic allocation of memory.

Data Structures and Algorithms for Engineers

Introduction to Data Structures. Systems Programming

Data Structures and Algorithms for Engineers

Ashish Gupta, Data JUET, Guna

Linked List in Data Structure. By Prof. B J Gorad, BECSE, M.Tech CST, PHD(CSE)* Assistant Professor, CSE, SITCOE, Ichalkaranji,Kolhapur, Maharashtra

BBM 201 DATA STRUCTURES

Linked List. April 2, 2007 Programming and Data Structure 1

DC54 DATA STRUCTURES DEC 2014

Solution for Data Structure

DC104 DATA STRUCTURE JUNE Q.2 a. If you are using C language to implement the heterogeneous linked list, what pointer type will you use?

Data Structures (INE2011)

However, in C we can group related variables together into something called a struct.

ECE 2035 A Programming Hw/Sw Systems Spring problems, 8 pages Final Exam Solutions 29 April 2015

NCS 301 DATA STRUCTURE USING C

Cpt S 122 Data Structures. Data Structures

Singly linked lists in C.

Stack. The Stack stores arbitrary objects Insertions and deletions follow the last-in firstout

ECE 2400 Computer Systems Programming, Fall 2017 Prelim 1 Prep

(6-1) Basics of a Queue. Instructor - Andrew S. O Fallon CptS 122 (September 26, 2018) Washington State University

ESc101: (Linear, Circular, Doubly) Linked Lists, Stacks, Queues, Trees. Introduction to Linked Lists

Dynamic Data Structures

Class Information ANNOUCEMENTS

C-types: basic & constructed. C basic types: int, char, float, C constructed types: pointer, array, struct

COP Programming Concepts Spring 1999 CLOSED BOOK Exam #1 100 Points NAME

Lecture 7: Data Structures. EE3490E: Programming S1 2017/2018 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology

(3-2) Basics of a Stack. Instructor - Andrew S. O Fallon CptS 122 (January 26, 2018) Washington State University

CP2 Revision. theme: dynamic datatypes & data structures

CS32 Discussion Week 3

APS105. Structures 11/18/2013. Example A struct of stock items at a store: Structures. Lists. Arrays allow a collection of elements

Linked Lists in C and C++

LINKED LIST IMPLEMENTATION USING C LANGUAGE: A REVIEW

DC54 DATA STRUCTURES JUNE 2013

Linked Lists Structures

ENEE 150: Intermediate Programming Concepts for Engineers Spring 2018 Handout #27. Midterm #2 Review

Linked Structures in C

Linked Lists. .. and other linked structures. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR

struct node{ int info; struct node *left, *right; }; typedef struct node nodeptr; A Linear Doubly Linked List

Structures, Pointers, Linked Lists

Memory. What is memory? How is memory organized? Storage for variables, data, code etc. Text (Code) Data (Constants) BSS (Global and static variables)

Chapter 4. Linked Lists. M hiwa ahmad aziz

Dynamic Data Structures. CSCI 112: Programming in C

The University of Nottingham

DOWNLOAD PDF LINKED LIST PROGRAMS IN DATA STRUCTURE

Chapter 6. Data Structure. /* start contains the address of the first node */ start = &elephant1; print_elephants( start ); return EXIT_SUCCESS;

Darshan Institute of Engineering & Technology for Diploma studies Unit 4

Queues. A queue is a special type of structure that can be used to maintain data in an organized way.

Self-referential Structures and Linked List. Programming and Data Structure 1

Pointers. Array. Solution to the data movement in sequential representation

Algorithms, Data Structures, and Problem Solving

.:: UNIT 4 ::. STACK AND QUEUE

Call The Project Dynamic-Memory

Binary Search Tree 1.0. Generated by Doxygen Mon Jun :12:39

Insertions and removals follow the Fist-In First-Out rule: Insertions: at the rear of the queue Removals: at the front of the queue

ECE 2035 A Programming Hw/Sw Systems Fall problems, 8 pages Final Exam Solutions 9 December 2015

Fall, 2015 Prof. Jungkeun Park

The combination of pointers, structs, and dynamic memory allocation allow for creation of data structures

Introduction to Programming in C Department of Computer Science and Engineering

January 24, Abstract Data Types (ADTs) An ADT is an abstraction of a data structure.

Q1: /8 Q2: /30 Q3: /30 Q4: /32. Total: /100

19-Nov CSCI 2132 Software Development Lecture 29: Linked Lists. Faculty of Computer Science, Dalhousie University Heap (Free Store)

Linked Lists and other Dynamic Data Structures

Mid-term Exam. Fall Semester 2017 KAIST EE209 Programming Structures for Electrical Engineering. Name: Student ID:

Data Structures and Algorithms (DSA) Course 5 Lists. Iulian Năstac

Computer Systems and Networks

Manual Allocation. CS 1622: Garbage Collection. Example 1. Memory Leaks. Example 3. Example 2 11/26/2012. Jonathan Misurda

(Section : Computer Science)

C Programming, Autumn 2013, Exercises for the Second Week

INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Stamp / Signature of the Invigilator

(2-1) Data Structures & The Basics of a Linked List I. Instructor - Andrew S. O Fallon CptS 122 (August 27, 2018) Washington State University

CA341 - Comparative Programming Languages

Lecture 5: Outline. I. Multi- dimensional arrays II. Multi- level arrays III. Structures IV. Data alignment V. Linked Lists

The time and space are the two measure for efficiency of an algorithm.

Homework Assignment #1

ECE 2035 Programming HW/SW Systems Fall problems, 5 pages Exam Three 28 November 2012

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

Stack & Queue on Self-Referencing Structures

ECE 2035 Programming HW/SW Systems Spring problems, 5 pages Exam Three 8 April Your Name (please print clearly)

Data Structures and Algorithms. Chapter 5. Dynamic Data Structures and Abstract Data Types

ECE 15B COMPUTER ORGANIZATION

Lecture 12: Doubly Linked Lists

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 4/18/2013

Data Structures and Algorithms. Roberto Sebastiani

Lists (Section 5) Lists, linked lists Implementation of lists in C Other list structures List implementation of stacks, queues, priority queues

CS32 - Week 2. Umut Oztok. July 1, Umut Oztok CS32 - Week 2

Intermediate Programming, Spring 2017*

Low-Level C Programming. Memory map Pointers Arrays Structures

Discussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage:

CSCE 110 PROGRAMMING FUNDAMENTALS

CS 103 Unit 15. Doubly-Linked Lists and Deques. Mark Redekopp

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION

ECE 2035 A Programming Hw/Sw Systems Spring problems, 8 pages Final Exam 29 April 2015

LINKED IMPLEMENTATION OF LIST

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 2/10/2013

C Language Part 3. Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

1 Deletion in singly linked lists (cont d) 1 Other Functions. 1 Doubly Linked Lists. 1 Circular lists. 1 Linked lists vs. arrays

CS61, Fall 2012 Section 2 Notes

Stack & Queue on Self-Referencing Structures

C Data Structures Stacks. Stack. push Adds a new node to the top of the stack

2. dead code elimination (declaration and initialization of z) 3. common subexpression elimination (temp1 = j + g + h)

Transcription:

PROGRAMMAZIONE I A.A. 2017/2018

LINKED LISTS

LINKED LIST What are the problems with arrays? üsize is fixed üarray items are stored contiguously üinsertions and deletions at particular positions is complex Why linked lists? üsize is not fixed üdata can be stored at any place üinsertions and deletions are simpler and faster

WHAT IS A LINKED LIST A linked list is a collection of nodes with various field It contains üdata fields ülink fields Data fields Link fields Data fields Link fields

DIFFERENT KINDS OF LISTS Singly linked lists Circular singly linked list Doubly linked lists Circular doubly linked list

SINGLY LINKED LIST 7000 Pointer to the first element 10 1000 30 4000 5 NULL Stored at memory address 7000 Stored at memory address 1000 Stored at memory address 4000

CIRCULAR SINGLY LINKED LIST 7000 Pointer to the first element 10 1000 30 4000 5 7000

DOUBLY LINKED LIST 7000 Stored at memory address 7000 NULL 10 1000 Stored at memory address 1000 7000 50 5000 Stored at memory address 5000 1000 30 NULL

CIRCULAR DOUBLY LINKED LIST 7000 5000 10 1000 7000 50 5000 1000 30 7000

REPRESENTATION AND OPERATIONS OF SINGLY LINKED LISTS

REPRESENTATION OF A NODE struct node { int info; struct node* pnext; struct node { int info1; char info2; struct data info3; struct node* pnext; info pnext typedef struct node Node info1 info2 info3 pnext

ALLOCATION IN THE HEAP int main() { Node *p = (Node*) malloc(sizeof(node)); heap 1000 info stack pnext p= 1000

INITIALIZATION int main() { Node *p = (Node*) malloc(sizeof(node)); p->info = 3; p->pnext= NULL; heap p= 1000 stack 1000 info = 3 pnext= NULL

SIDE STRUCTURES One pointer to the first element of the list One pointer to the last element of the list (optional) 7000 Node* pfirst 4000 Node* plast 10 1000 30 4000 5 NULL

MOST COMMON OPERATIONS Print all the elements Insertion ühead ütail üat a given position Deletion ühead ütail üat a given position

PRINT (LIST SCAN) // As a parameter, it takes the pointer to the first node of a list void print_list(node* pfirst) { if(pfirst == NULL) // No node in the list { printf( No node in the list!"); else { // New pointer used to scan the list. Node* pscan = pfirst; do { printf( Info: %d\n", &(pscan->id)); // ptrscan is updated to point to the next node in the // list pscan = pscan->pnext; while(pscan!= NULL); //NULL when this was the last node return;

HOW IT RUNS Info: 10 Info: 30 Info: 5 7000 Node* pfirst pfirst == NULL? No pscan = pfirst 7000 printf pscan->info 10 pscan = pscan ->pnext 1000 pscan!= NULL? Yes printf pscan->info 30 pscan = pscan ->pnext 4000 pscan!= NULL? Yes printf pscan->info 5 pscan = pscan ->pnext NULL pscan!= NULL? No return 10 1000 30 4000 5 NULL

HEAD INSERTION // As a parameter, it takes the pointer to the first node of a list void head_insertion(node* pfirst) { // Creation of a new node in the heap Node *pnew = (Node*) malloc(sizeof(node)); scanf( %d, &(pnew->info)); pnew->pnext= NULL; if(pfirst == NULL) // No node in the list pfirst = pnew; // The first node is the newly created one else { // Else, there is already at least one node in the list pnew-> pnext= pfirst; // the first node becomes the second one pfirst= pnew; // The first node is the newly created one return;

HOW IT RUNS Node *pnew = (Node*) malloc(sizeof(node)); scanf( %d, pnew->info); pnew->pnext= NULL; 3500 3 NULL NULL 7000 3500 Node* pfirst pfirst == NULL? Yes pfirst = pnew 3500

HOW IT RUNS Node *pnew = (Node*) malloc(sizeof(node)); scanf( %d, pnew->info); pnew->pnext= NULL; 3500 3 NULL 7000 7000 3500 Node* pfirst pfirst == NULL? No pnew -> pnext= pfirst 7000 pfirst = pnew 3500 10 1000 30 4000 5 NULL

HEAD DELETION // As a parameter, it takes the pointer to the first node of a list void head_deletion(node* pfirst) { if(pfirst == NULL) // No node in the list { printf( No node in the list!"); else { // Else, there is at least a node in the list // Remember the pointer to the second node, which will become // the first one Node* temp= pfirst-> pnext; // Memory is deallocated (node canceled from memory) free(pfirst); // The first node becomes the former second node pfirst= temp; return;

HOW IT WORKS pfirst == NULL? No temp= pfirst -> pnext 1000 free(pfirst) 7000 pfirst = temp 1000 7000 Node* pfirst 10 1000 30 4000 5 NULL

ADDING AND REMOVING FROM TAIL When we need to add or remove a node from the tail, it is useful to also have a pointer to the last node of a list üit simplifies writing these two functions This pointer (plast) needs to be updated at the end of these operations Of course if we need to have add-or-remove from head and add-or-remove from tail, plast needs to be updated also in add-or-remove from head (as pfirst in previous examples)

TAIL INSERTION // As a parameter, it takes the pointer to the first and last nodes of a list void tail_insertion(node* pfirst, Node* plast) { // Creation of a new node in the heap Node *pnew = (Node*) malloc(sizeof(node)); scanf( %d, &(pnew->info)); pnew->pnext= NULL; if(pfirst == NULL) // No node in the list pfirst = pnew; // The first node is the newly created one plast = pnew; // The last node is the newly created one else { // Else, there is already at least one node in the list plast-> pnext= pnew; // the last node becomes the second one plast= pnew; // The last node is the newly created one return;

HOW IT WORKS 7000 Node* pfirst pfirst == NULL? No plast-> pnext= pnew 500 plast= pnew 500 4000 500 Node* plast 10 1000 30 4000 5 NULL 500 500 15 NULL

TAIL DELETION void tail_deletion(node* pfirst, Node* plast) { if(pfirst == NULL) printf( No node in the list!\n"); else { Node* pprev = NULL; Node* pscan = pfirst; if(pscan->pnext == NULL) {// It means we only have one node in the list free(pscan); // Free memory pfirst= NULL; // Now the list is empty else {// Otherwise, I need to scan the list until I find the last node (plast) do{ if((pscan-> pnext) == plast) {// Reached the node before the end pprev = pscan; break; else pscan= pscan-> pnext; // Otherwise, I need to iterate while((pscan-> pnext)!= NULL); free(pprev-> pnext); // Free memory allocated to the last node pprev-> pnext = NULL; // pprev becomes the last node (no node after it) plast = pprev; // pprev becomes the last node

HOW IT RUNS 7000 Node* pfirst pfirst == NULL? No pscan= pfirst 7000 pscan-> pnext== NULL? No pscan = pscan -> pnext 1000 4000 Node* plast 10 1000 30 NULL 4000 5 NULL (pscan-> pnext) == plast Yes pprev= pscan 1000 free(pprev-> pnext) 4000 pprev -> pnext= NULL plast = pprev 1000

DELETE NODE IN SOME POSITION 7000 4000 Node* pfirst Node* plast 1. Iterate until (pscan -> pnext) -> info == key 2. temp= (pscan-> pnext) -> next 3. free(pscan->next) 4. pscan-> pnext = temp temp = 4000 10 1000 4000 30 4000 5 NULL void delete_if_equal(node* pfirst, Node* plast, int key){ e.g., 30 Update pfirst or plast if instead you remove either the first or last node

ADD NODE IN SOME POSITION 7000 4000 Node* pfirst Node* plast 10 1000 5000 30 4000 35 NULL 5000 15 NULL 1000 Homework!