Linked Structures in C

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

C Programming, Autumn 2013, Exercises for the Second Week

Ashish Gupta, Data JUET, Guna

PROGRAMMAZIONE I A.A. 2017/2018

Introduction to Programming in C Department of Computer Science and Engineering

Introduction to Linked Lists

Lesson 8: Linked List Deque

2

1 P age DS & OOPS / UNIT II

.:: UNIT 4 ::. STACK AND QUEUE

Introduction to Programming in C Department of Computer Science and Engineering

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

Dynamic Data Structures

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

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

15-122: Principles of Imperative Computation, Spring Due: Thursday, March 10, 2016 by 22:00

Singly linked lists in C.

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

NYU SCPS X Section 1 Unix System Calls. Fall 2004 Handout 8. Source code on the Web at mm64/x /src/doubly2.

Linked Lists in C and C++

Linked List Implementation of Queues

CSE 303, Spring 2005, Midterm Examination 29 April Please do not turn the page until everyone is ready.

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

Data Structure with C. List

Question 1. [15 marks]

Phase One: Process and Semaphore Queue Modules

Machine Problem 1: A Simple Memory Allocator

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

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

Linked lists (6.5, 16)

Data Structure Series

Lecture Notes CPSC 122 (Fall 2014) Today Quiz 7 Doubly Linked Lists (Unsorted) List ADT Assignments Program 8 and Reading 6 out S.

LAB 5, THE HIDDEN DELIGHTS OF LINKED LISTS

CSCE 110 PROGRAMMING FUNDAMENTALS

Comp Sci 1MD3 Mid-Term II 2004 Dr. Jacques Carette

CP2 Revision. theme: dynamic datatypes & data structures

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- OCTOBER, 2012 DATA STRUCTURE

CS61, Fall 2012 Section 2 Notes

Programming in C/C Lecture 2

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

CS32 Discussion Week 3

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING B.E SECOND SEMESTER CS 6202 PROGRAMMING AND DATA STRUCTURES I TWO MARKS UNIT I- 2 MARKS

Data Structures and Algorithms for Engineers

Kurt Schmidt. October 30, 2018

: Principles of Imperative Computation Victor Adamchik. Practice Exam - I

Computer Science 62. Midterm Examination

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

CmpSci 187: Programming with Data Structures Spring 2015

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

Datatypes. List of data structures. Datatypes ISC

Computer Science Foundation Exam. Dec. 19, 2003 COMPUTER SCIENCE I. Section I A. No Calculators! KEY

Writing Functions in C

EE 355 Unit 11b. Doubly-Linked Lists and Deques. Mark Redekopp

K is for. Plan for Today. Nodes in a Linked List 10/10/18. Compsci 201 Linked Lists, Removal, Recursion. K-means

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

Data Structures and Algorithms for Engineers

Linked List Practice Questions

COMP26120: Linked List in C (2018/19) Lucas Cordeiro

Lecture 10 Notes Linked Lists

CS32 Discussion Sec.on 1B Week 2. TA: Zhou Ren

CSCI2100B Data Structures Trees

Actually, C provides another type of variable which allows us to do just that. These are called dynamic variables.

Patterns: Working with Arrays

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

That means circular linked list is similar to the single linked list except that the last node points to the first node in the list.

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

Linked Lists. Contents. Steven J. Zeil. July 31, Linked Lists: the Basics 4

Linked Lists and other Dynamic Data Structures

Linked Lists

CMPT 225. Lecture 6 linked list

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

3 Nonlocal Exit. Quiz Program Revisited

MODULE 3: LINKED LIST

Tutorial 2: Linked Lists

Two approaches. array lists linked lists. There are two approaches that you can take:

Computer Science 136. Midterm Examination

Lab 5 - Linked Lists Git Tag: Lab5Submission

Information Science 2

First of all, it is a variable, just like other variables you studied

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified)

Linked Lists. Contents. Steven J. Zeil. July 31, Linked Lists: the Basics 3

Search Trees. Data and File Structures Laboratory. DFS Lab (ISI) Search Trees 1 / 17

Elementary Data Structures: Lists

Computer Science 62. Bruce/Mawhorter Fall 16. Midterm Examination. October 5, Question Points Score TOTAL 52 SOLUTIONS. Your name (Please print)

Queues. October 20, 2017 Hassan Khosravi / Geoffrey Tien 1

Prelim 1 Solutions. CS 2110, March 10, 2015, 5:30 PM Total Question True False. Loop Invariants Max Score Grader

The.Call Interface I. the R side of the.call interface is almost the same as the.c interface

Administrivia. Introduction to Computer Systems. Pointers, cont. Pointer example, again POINTERS. Project 2 posted, due October 6

Laboratory Module Trees

Elementary Data Structures: Lists

SYSC 2006 Winter 2012 Linear Collections: Queues

Here's how you declare a function that returns a pointer to a character:

Binary Trees (and Big O notation)

5 Phases of Software Life Cycle

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

Lecture Notes on Binary Search Trees

Provided by - Microsoft Placement Paper Technical 2012

Lab 4 - Linked Lists

INTERNAL REPRESENTATION OF FILES:

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

Transcription:

Linked Structures in C T. Karvi October 2015 T. Karvi () Linked Structures in C October 2015 1 / 13

Linked Lists I A linked list consists of nodes. A node has a value and a link to a next node. As a diagram: L 1 3 4 7 A list may have a header node which contains information about the list, for example the length of the list, and a link to the first data node. 4 L 1 3 4 7 T. Karvi () Linked Structures in C October 2015 2 / 13

Linked Lists II A list can be doubly linked: T. Karvi () Linked Structures in C October 2015 3 / 13

Linked Lists III Or a list can be circular: L 1 3 4 7 T. Karvi () Linked Structures in C October 2015 4 / 13

Linked Lists IV So what is a link? In C, it is typically a pointer. And what is a node? It is a structure. We have the definitions (singly linked list): typedef struct node { Datatype value; struct node *next; } NodeT, *NodeTpr; Before giving examples of list functions, some words about the use of lists. A list is a data structure which contains several items of data of the same type. It is easy and efficient to add an item to a list as the first item. Deletion of an item an item is more complicated, if the item happens to be in the middle of the list. First, there should be a pointer pointing to a node to be deleted: T. Karvi () Linked Structures in C October 2015 5 / 13

Linked Lists V L to be deleted 1 3 4 7 q p However, how to find the previous node q which is needed when updating the links? Of course, it is always possible to start from the beginning of the list and proceed step by step forward until q and p are found. But this can be time-consuming, if a list is long. That is why you should never use lists for searching purposes, if lists can become long. For searches we have better data structures such as has tables and search trees. T. Karvi () Linked Structures in C October 2015 6 / 13

Linked Lists VI So how to arrange the deletion of a list node in such a way that the operation is efficient? Consider the following diagram: to be deleted 1 3 4 7 q p r In the previous diagram we thought that p is given as a parameter to the delete function and q must be searched for. But if we give q as a parameter, then the node p can be found in one step and r can be found in two steps. So everything can be done in a constant time, independent of the length of the list. One question remains: How do we know q beforehand, if p is to be deleted? T. Karvi () Linked Structures in C October 2015 7 / 13

Linked Lists VII Usually the operations in a list proceed step by step starting in the beginning of the list. For example, we must examine every node in a list and sometimes delete a node. In this case we can keep track of two or more nodes around the node we are dealing with. If we must do search operations, then probably we have a wrong data structure. T. Karvi () Linked Structures in C October 2015 8 / 13

Insert Operation I Next we implement the operation insert(l,p,a) which inserts a value a into the list L. We assume that the list is singly linked and without a header. Then we must define the place into which the new node is put. It must be said that using a header node makes many operations simpler than without a header node. If L==NUL, then the list is empty and the insert operation creates a new list. If (L<>NULL) and (p==null), then we add a as the first node. Finally, if (L<>NULL) and (p<>null), then we add a to the right side of the node p. T. Karvi () Linked Structures in C October 2015 9 / 13

Insert Operation II Remember that all the parameters in a function are value parameters. Suppose we have the insert operation insert(l,p,a) and L is empty. If we create a new node and set L to point to this new node, then when the execution of the function is ended, L still points to its original value, i.e. it is NULL. What we must do in the function is to return the pointer to the new node and catch this return value in the main program. The next code shows the insert function which adds a node into a list. (The outlook of the code seems bad. The verbatim environment in Latex Beamer does not work properly.) T. Karvi () Linked Structures in C October 2015 10 / 13

Insert Operation III NodeT* insert(nodet *L, NodeT *p, int a){ NodeT * q; if ( L== NULL){ if ((q = malloc(sizeof(nodet))) == NULL) return NULL; q -> value = a; q->next = NULL; return q; } T. Karvi () Linked Structures in C October 2015 11 / 13

Insert Operation IV if (p == NULL) { if ((q = malloc(sizeof(nodet))) == NULL) return NULL; q->value = a; q->next = L; return q; } T. Karvi () Linked Structures in C October 2015 12 / 13

Insert Operation V if ((q = malloc(sizeof(nodet))) == NULL) return NULL; q->value = a; q->next = p->next; p->next = q; return L; } T. Karvi () Linked Structures in C October 2015 13 / 13

Delete Operation I We implement also the operation delete(l,p) which deletes the node after p. If p is NULL, then the first node is deleted. If p points to the last node, an error arises. NodeT * delete(nodet *L, NodeT *p){ NodeT * S; NodeT * q; NodeT * r; if (L == NULL) return NULL; if (p == NULL){ S = L->next; free(l); return S; T. Karvi () Linked Structures in C October 2015 14 / 13

Delete Operation II } } if (p->next == NULL) return NULL; r = p->next; q = r->next; p-> next = q; free(r); return L; T. Karvi () Linked Structures in C October 2015 15 / 13