Lect 10 Zaheer Abbas

10
Created by Zaheer Abbas Aghani

Transcript of Lect 10 Zaheer Abbas

Page 1: Lect 10 Zaheer Abbas

Created byZaheer Abbas Aghani

Page 2: Lect 10 Zaheer Abbas

LIST STRUCTURELINKED LIST

Lecture #10

Page 3: Lect 10 Zaheer Abbas

What is a List?

In computer science, a list is a collection of entities/items.

ORList is a collection of short pieces of information

such as names of peoples or list of groceries etc.List is among the most generic of data structure.It is usually used in the daily life routineE.g.

birthday list of friends.shopping listetc.

Page 4: Lect 10 Zaheer Abbas

Nature of List Structure

List is a collection of items that are all of same type.

The items or elements of list are stored in some particular order.

It is possible to insert new elements into various position in list and remove any element of list.

The order is important here. It is not just a random collection it is an ordered collection.

Page 5: Lect 10 Zaheer Abbas

List Implementation

Lists are usually implemented using different data structure.

There are two main ways to implement lists into memory.

One way to store such data is by means of arrays, but array have certain disadvantages

e.g. It is relatively expensive to insert and delete elements. it occupies a block of memory space, one can not

increase the size of array when space is required.

Page 6: Lect 10 Zaheer Abbas

Another way of storing list in memory to have each element in the list contain field called link or pointer.

Pointer contains the address of next element in the list. Thus successive element in the list need not occupy adjacent space in memory.

If some one is interested in searching through data for inserting and deleting as in word processing one would not store data in an array but rather in list using pointers.

This type of data structure is called a linked list

Page 7: Lect 10 Zaheer Abbas

Linked List

A linked list, or one way list is a linear collection of data elements called nodes, where each element contains a pointer to the next element.

ORA linked list is a data structure where every

element contains both a 'value' and a link/reference/pointer to a 'next' element.

The whole list is represented by a pointer/reference/link to the first element of the list.

Page 8: Lect 10 Zaheer Abbas

Linked list continue…..

In computer science, a linked list is one of the fundamental data structure, and can be used to implement other data structure.

Its consists of sequences of nodes, each contain arbitrary data fields and one or two references (links) pointing to the next and /or pervious nodes.

linked lists allocate memory for each element separately and only when necessary.

A linked list is also called self-referential data type because it contains a pointer or link to other datum of same type.

Page 9: Lect 10 Zaheer Abbas

What Linked Lists Look Like

An array allocates memory for all its elements lumped together as one block of memory.

In contrast, a linked list allocates space for each element separately in its own block of memory called a "linked list element" or "node". The list gets is overall structure by using pointers to connect all its nodes together like the links in a chain

Each node contains two fields: a "data" field to store whatever element type the list holds and a "next" field which is a pointer used to link one node to the next node.

Linked lists sometimes have a special dummy or sentinel node at the beginning and/or at the end of the list, which is not used to store data. Its purpose is to simplify or speed up some operations, by ensuring that every data node always has a previous and/or next node, and that every list (even one that contains no data elements) always has a "first" and "last" node.

Page 10: Lect 10 Zaheer Abbas

.Above is a schematic diagram of a linked list with 5

nodes. Each node contain two parts i.e.

the left part represents the information part of the node.

the right part represents the next pointer field of the node.

Start points to the first element of list and the pointer of last node contain a special value, called the null pointer, which is any invalid address.

x

start