Linked Lists CSM 1220 - Linked Lists.

Slides:



Advertisements
Similar presentations
Singly linked lists Doubly linked lists
Advertisements

Stacks, Queues, and Linked Lists
CSCE 3110 Data Structures & Algorithm Analysis
Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List.
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
CS252: Systems Programming Ninghui Li Program Interview Questions.
CS Data Structures Chapter 4 Lists. Dynamically Linked Stacks and Queues (1/8)  When several stacks and queues coexisted, there was no efficient.
Comp 249 Programming Methodology Chapter 15 Linked Data Structure - Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
1 CSC 222: Computer Programming II Spring 2004 Pointers and linked lists  human chain analogy  linked lists: adding/deleting/traversing nodes  Node.
ليست هاي پيوندي Linked Lists ساختمان داده ها و الگوريتم ها.
Generic lists Vassilis Athitsos. Problems With Textbook Interface? Suppose that we fix the first problem, and we can have multiple stacks. Can we have.
Linked lists. Data structures to store a collection of items Data structures to store a collection of items are commonly used Typical operations on such.
Data Structure & Algorithms
CSC 143 P 1 CSC 143 Recursion [Chapter 5]. CSC 143 P 2 Recursion  A recursive definition is one which is defined in terms of itself  Example:  Compound.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
STACKS & QUEUES for CLASS XII ( C++).
CSE 1342 Programming Concepts
Review Array Array Elements Accessing array elements
The List ADT.
Data Structures Using C, 2e
Pointers and Linked Lists
Data Structure By Amee Trivedi.
Linked List :: Basic Concepts
Pointers and Linked Lists
Sorted Linked List Same objective as a linked list, but it should be sorted Sorting can be custom according to the type of nodes Offers speedups over non-sorted.
Linked List Stacks, Linked List Queues, Dequeues
Lists CS 3358.
Data Structure Dr. Mohamed Khafagy.
Doubly Linked List Review - We are writing this code
CSCE 3110 Data Structures & Algorithm Analysis
UNIT-3 LINKED LIST.
CSE 143 Linked Lists [Chapter , 8.8] 3/30/98.
Data Structures 7th Week
Queues Queues Queues.
Programming Abstractions
Array Array is a variable which holds multiple values (elements) of similar data types. All the values are having their own index with an array. Index.
EEE2108: Programming for Engineers Chapter 4. Linked Lists
Efficiency add remove find unsorted array O(1) O(n) sorted array
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.
Stacks and Queues.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Introduction to Linked Lists
Pointers and Linked Lists
Arrays and Linked Lists
A Data Structure Bestiary
Chapter 13 Collections.
A Data Structure Bestiary
Lesson Objectives Aims
Linked List (Part I) Data structure.
Further Data Structures
Linked Lists.
Lecture 7: Linked List Basics reading: 16.2
Queues: Implemented using Arrays
Programming Abstractions
Linked Lists in C and C++
LINKED LISTS.
Stacks and Queues.
Using a Queue Chapter 8 introduces the queue data type.
Using a Queue Chapter 8 introduces the queue data type.
Linked Lists.
Queues: Implemented using Linked Lists
Data Structures Chapter 4: Linked Lists.
slides created by Marty Stepp
17CS1102 DATA STRUCTURES © 2018 KLEF – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED.
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
COS 151 Bootcamp – Week 4 Department of Computer Science
Data Structures & Programming
Problem Understanding
Presentation transcript:

Linked Lists CSM 1220 - Linked Lists

‘Variable-length arrays’? Arrays are fixed-length To expand them, you create a new, longer array, and copy the contents of the old array into the new array This is slow! Solution: Attach a pointer to each item in the array, which points to the next item This is a linked list An data item plus its pointer is called a node CSM 1220 - Linked Lists

The Linked List data structure [0] [1] [2] array A B C Array node linked A B C Linked list Linked lists are unbounded (maximum number of items limited only by memory) CSM 1220 - Linked Lists

The Node data structure Contains two things: A piece of data (any object) The next node in the list Write the code for a node… CSM 1220 - Linked Lists

The Linked List data structure Same operations as for arrays. Here are the ones we know: addToFront, addToBack: add an object to the front/back of the list removeFromFront/Back: remove an object from the front/back getFront/Back: examine the object at the front/back (no removal) isEmpty: determines whether or not the list is empty length: returns the number of objects in the list equals: tests two lists for equality toString: converts a list to a String 'Ordered' means that the order matters CSM 1220 - Linked Lists

linked list Some common examples of a linked list Hash tables use linked lists for collision resolution Any "File Requester" dialog uses a linked list Binary Trees Stacks and Queues can be implemented with a doubly linked list Relational Databases (eg. Microsoft Access) Exercise: method signatures for the class (see previous slide) CSM 1220 - Linked Lists

Intialization Node head, Tail; Head=tail=null; class Node { int data; Node next; }

2. Adding a Node Adding an element to the head of a linked list public void CreateNode (int element) { Node current=new Node (element); if (head==null) head=current; tail=current; } else current.next=head;

Head == null Head Tail

Head == null Current Head A Tail Node current=new Node (element);

head=current; tail=current; Head A Tail

if (head !=null) Node current=new Node (B); Head B A current Tail

current.next=head; head=current; Head B A Tail

current.next=head; head=current; Head C B A current Tail

Adding an element to the tail public void CreateNode(int element) { Node current=new Node(element); current.next=null; if (head==null) head=current; tail=current; } else tail.next=current;

tail.next=current; tail=current; Head A B current Tail

tail.next=current; tail=current; Head A B Tail Tail

removeFromHead head=head.next; Initial list Remove Head (C) Head C B A Tail Remove Head (C) Implementation is left as an exercise. Remember to return the removed object. head=head.next; Head B A Tail CSM 1220 - Linked Lists

Removing an element from a linked list public int DeleteNode(int position) { int ptr=1; if (position==1) { head=head.next; return(1); } CSM 1220 - Linked Lists

current=head; while (current.next!=null) { if(ptr==position-1) break; current=current.next; ptr++; } if (ptr==position-1) { current.next=current.next.next; return (1); else return (-1);

Consider the polynomial expression: A(x)= amxem+…….+a1xe1 , Polynomial Addition Consider the polynomial expression: A(x)= amxem+…….+a1xe1 , where ai are non-zero coefficients and ei are exponents

The polynomial can be represented using linked list. Each term in the expression will be represented by a node. A node will be of fixed size having 3 fields which represent the coefficient and exponent of a term plus a pointer to the next term. Coeff Exp Next

For eg: A= 3x14 + 2x8 +1 would be stored as

We have two polynomial. A= 3x14 + 2x8 +1 B= 8x14 – 3x10 + 10x6 Their sum will be 11x14 – 3x10 + 2x8 +10x6 + 1.

We have two pointers p and q that points to A and B respectively. if the exponents of the two terms are equal , then the coefficients are added and a new term is created for the result. If the exponents of the current term in A is less than the current term in B, then a duplicate of B is created and attached to C. the pointer q is advanced to the next term. Similar action is taken on A if exp(p) > exp(q).

Each time a new node is generated its coeff and exp fields are set and it is appened to the end of the list C. d is a pointer that points to the last node in the resultant node.

Procedure ATTACH(c,e,d) //c – coeff, e – exp, d – pointer call GETNODE(I) exp(I)  e coeff (I) c link(d)  I d  I end ATTACH

procedure PADD(A,B,C) // A and B – singly linked list are summed to form the new // list name C p  A q  B call GETNODE (C) d  C while p  0 and q  0 do

case : exp (p) = exp(q) x  coeff(p) + coeff(q) if x  0 then call ATTACH( x, exp(p), d) p  link(p) q  link(q)

case : exp (p) < exp (q) call ATTACH( coeff (q), exp(q), d) q  link(q) else call ATTACH( coeff (p), exp(p), d) p  link(p) end case endwhile

while p  0 do call ATTACH( coeff(p), exp(p), d) p  link(p) endwhile while q  0 do call ATTACH( coeff(q), exp(q), d) q  link(q) link(d)  0;t  C ; C link(C) call RET(T) end PADD

Thank you……