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

Similar documents
Pointers. Introduction

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

Linked-List Basic Examples. A linked-list is Linear collection of self-referential class objects, called nodes Connected by pointer links

Cpt S 122 Data Structures. Data Structures

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

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-0-0)

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

CA341 - Comparative Programming Languages

Pointers and Arrays 1

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

C Programming Language

Pointers and File Handling

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

Introduction to Linked List: Review. Source:

Arrays and Pointers (part 1)

Computer Programming Lecture 12 Pointers

BBM 201 DATA STRUCTURES

DECLARAING AND INITIALIZING POINTERS

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

Dynamic Memory Allocation

ESC101N: Fundamentals of Computing End-sem st semester

Singly linked lists in C.

Darshan Institute of Engineering & Technology for Diploma studies Unit 4

Outline. Briefly review the last class Pointers and Structs Memory allocation Linked lists

Midterm Examination # 2 Wednesday, March 19, Duration of examination: 75 minutes STUDENT NAME: STUDENT ID NUMBER:

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

Midterm Examination # 2 Wednesday, March 18, Duration of examination: 75 minutes STUDENT NAME: STUDENT ID NUMBER:

Ashish Gupta, Data JUET, Guna

AMCAT Automata Coding Sample Questions And Answers

Subject: Fundamental of Computer Programming 2068

Arrays and Pointers (part 1)

CS349/SE382 A1 C Programming Tutorial

C Pointers. Abdelghani Bellaachia, CSCI 1121 Page: 1

Data Structures and Algorithms for Engineers

Arrays and Pointers. CSE 2031 Fall November 11, 2013

(1-2) Introduction to C Data Structures & Abstract Data Types. Instructor - Andrew S. O Fallon CptS 122 (June 6, 2018) Washington State University

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

CSE2301. Dynamic memory Allocation. malloc() Dynamic Memory Allocation and Structs

Programming for Engineers Arrays

CS261: HOMEWORK 2 Due 04/13/2012, at 2pm

Tin học cơ sở 4$ Structures!

Data Structures in C. C Programming and Software Tools. N.C. State Department of Computer Science

CP2 Revision. theme: dynamic datatypes & data structures

CS 241 Data Organization Binary Trees

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

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.

Week 9 Part 1. Kyle Dewey. Tuesday, August 28, 12

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

Cpt S 122 Data Structures. Course Review Midterm Exam # 1

C Programming Review CSC 4320/6320

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

CS113: Lecture 4. Topics: Functions. Function Activation Records

Array Initialization

LSN 3 C Concepts for OS Programming

JTSK Programming in C II C-Lab II. Lecture 3 & 4

Data Structure Series

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

Chapter 2 (Dynamic variable (i.e. pointer), Static variable)

.:: UNIT 3 ::. LIST AND LINKED LIST

C Programming Language: C ADTs, 2d Dynamic Allocation. Math 230 Assembly Language Programming (Computer Organization) Thursday Jan 31, 2008

Chapter 19 Data Structures -struct -dynamic memory allocation

CS 237 Meeting 19 10/24/12

Intermediate Programming, Spring 2017*

C Programming SYLLABUS COVERAGE SYLLABUS IN DETAILS

Unit IV & V Previous Papers 1 mark Answers

In Java we have the keyword null, which is the value of an uninitialized reference type

MCA Semester 1. MC0061 Computer Programming C Language 4 Credits Assignment: Set 1 (40 Marks)

Dynamic Memory Allocation: Basic Concepts

Introduction to Linked Lists

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

Memory Management. CSC215 Lecture

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

Solution for Data Structure

Pointers. Part VI. 1) Introduction. 2) Declaring Pointer Variables. 3) Using Pointers. 4) Pointer Arithmetic. 5) Pointers and Arrays

BSM540 Basics of C Language

Memory Management. a C view. Dr Alun Moon KF5010. Computer Science. Dr Alun Moon (Computer Science) Memory Management KF / 24

EM108 Software Development for Engineers

(13-2) Dynamic Data Structures I H&K Chapter 13. Instructor - Andrew S. O Fallon CptS 121 (November 17, 2017) Washington State University

These problems are provided to you as a guide for practice. The questions cover important concepts covered in class.

PROGRAMMAZIONE I A.A. 2017/2018

Dynamic Memory Allocation

PDS Class Test 2. Room Sections No of students

Recursion, Trees and Tree Traversal. Module CE System Programming & Computer Control Systems

! A data structure representing a list. ! A series of nodes chained together in sequence. ! A separate pointer (the head) points to the first

DAY 3. CS3600, Northeastern University. Alan Mislove

EL2310 Scientific Programming

Lesson #8. Structures Linked Lists Command Line Arguments

Dynamic Data Structures. CSCI 112: Programming in C

Slides adopted from T. Ferguson Spring 2016

Structures. Basics of Structures (6.1) EECS l Now struct point is a valid type. l Defining struct variables: struct point { int x; int y; };

! A data structure representing a list. ! A series of dynamically allocated nodes. ! A separate pointer (the head) points to the first

Dynamic memory allocation

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

Sample Problems for Quiz # 3

Computer Science & Engineering 150A Problem Solving Using Computers

Dynamic Memory Allocation: Basic Concepts

CS240: Programming in C

#3. (Recursion) Write a recursive function to compute f(x) = f(x - 1) + f(x - 2) with f(0) = 0 and f(1) = 1.

Introduction to C/C++ Lecture 5 - String & its Manipulation

CS Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2015

Transcription:

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

Linked List :: Basic Concepts A list refers to a set of items organized sequentially. An array is an example of a list. The array index is used for accessing and manipulating array elements. Problems with array: The array size has to be specified at the beginning. Deleting an element or inserting an element may require shifting of elements in the array. Spring Semester 2011 Programming and Data Structure 2

Contd. A completely different way to represent a list: Make each item in the list part of a structure. The structure also contains a pointer or link to the structure containing the next item. This type of list is called a linked list. Structure 1 Structure 2 Structure 3 item item item Spring Semester 2011 Programming and Data Structure 3

Contd. Each structure of the list is called a node, and consists of two fields: One containing the data item(s). The other containing the address of the next item in the list (that is, a pointer). The data items comprising a linked list need not be contiguous in memory. They are ordered by logical links that are stored as part of the data in the structure itself. The link is a pointer to another structure of the same type. Spring Semester 2011 Programming and Data Structure 4

Contd. Such a structure can be represented as: struct node { int item; } struct node *next; node item next Such structures which contain a member field pointing to the same structure type are called self-referential structures. Spring Semester 2011 Programming and Data Structure 5

Contd. In general, a node may be represented as follows: struct node_name { type member1; type member2; struct node_name *next; } Spring Semester 2011 Programming and Data Structure 6

Illustration Consider the structure: struct stud { int roll; } char name[30]; int age; struct stud *next; Also assume that the list consists of three nodes n1, n2 and n3. struct stud n1, n2, n3; Spring Semester 2011 Programming and Data Structure 7

Contd. To create the links between nodes, we can write: n1.next = &n2; n2.next = &n3; n3.next = NULL; /* No more nodes follow */ Now the list looks like: roll name age next n1 n2 n3 Spring Semester 2011 Programming and Data Structure 8

Some important observations: The NULL pointer is used to indicate that no more nodes follow, that is, it is the end of the list. To use a linked list, we only need a pointer to the first element of the list. Following the chain of pointers, the successive elements of the list can be accessed by traversing the list. Programming and Data Structure 9

Example: without using function #include <stdio.h> struct stud { int roll; char name[30]; int age; struct stud *next; } main() { struct stud n1, n2, n3; struct stud *p; scanf ( %d %s %d, &n1.roll, n1.name, &n1.age); scanf ( %d %s %d, &n2.roll, n2.name, &n2.age); scanf ( %d %s %d, &n3.roll, n3.name, &n3.age); Spring Semester 2011 Programming and Data Structure 10

n1.next = &n2; n2.next = &n3; n3.next = NULL; /* Now traverse the list and print the elements */ } p = &n1; /* point to 1 st element */ while (p!= NULL) { printf ( \n %d %s %d, p->roll, p->name, p->age); p = p->next; } Spring Semester 2011 Programming and Data Structure 11

A function to carry out traversal #include <stdio.h> struct stud { int roll; char name[30]; int age; struct stud *next; } void traverse (struct stud *head) { while (head!= NULL) { printf ( \n %d %s %d, head->roll, head->name, head->age); head = head->next; } } Programming and Data Structure 12

The corresponding main() function main() { struct stud n1, n2, n3, *p; scanf ( %d %s %d, &n1.roll, n1.name, &n1.age); scanf ( %d %s %d, &n2.roll, n2.name, &n2.age); scanf ( %d %s %d, &n3.roll, n3.name, &n3.age); n1.next = &n2; n2.next = &n3; n3.next = NULL; } p = &n1; traverse (p); Spring Semester 2011 Programming and Data Structure 13

Alternative and More General Way Dynamically allocate space for the nodes. Use malloc() or calloc() for allocating space for every individual nodes. No need for allocating additional space unnecessarily like in an array. Spring Semester 2011 Programming and Data Structure 14

Linked List in more detail March 31, 2011 Programming and Data Structure 15

head Introduction A linked list is a data structure which can change during execution. Successive elements are connected by pointers. Last element points to NULL. It can grow or shrink in size during execution of a program. It can be made just as long as required. It does not waste memory space. A B C March 31, 2011 Programming and Data Structure 16

Keeping track of a linked list: Must know the pointer to the first element of the list (called start, head, etc.). Linked lists provide flexibility in allowing the items to be rearranged efficiently. Insert an element. Delete an element. March 31, 2011 Programming and Data Structure 17

head Illustration: Insertion A B C head X Item to be inserted A B C X March 31, 2011 Programming and Data Structure 18

Illustration: Deletion head Item to be deleted A B C head A B C March 31, 2011 Programming and Data Structure 19

For insertion: In essence... A record is created holding the new item. The next pointer of the new record is set to link it to the item which is to follow it in the list. The next pointer of the item which is to precede it must be modified to point to the new item. For deletion: The next pointer of the item immediately preceding the one to be deleted is altered, and made to point to the item following the deleted item. March 31, 2011 Programming and Data Structure 20

Array versus Linked Lists Arrays are suitable for: Inserting/deleting an element at the end. Randomly accessing any element. Searching the list for a particular value. Linked lists are suitable for: Inserting an element. Deleting an element. Applications where sequential access is required. In situations where the number of elements cannot be predicted beforehand. March 31, 2011 Programming and Data Structure 21