Linked List

Only available on StudyMode
  • Topic: Linked list, Array data structure, Abstract data type
  • Pages : 27 (3270 words )
  • Download(s) : 15
  • Published : March 10, 2013
Open Document
Text Preview
Linked List

July 21, 2009

Programming and Data Structure

1

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 July 21, 2009

head

B
Programming and Data Structure

C
2

• 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.

July 21, 2009

Programming and Data Structure

3

Illustration: Insertion
A B C

X

Item to be inserted

A

B

C

X
July 21, 2009 Programming and Data Structure 4

Illustration: Deletion
Item to be deleted

A

B

C

A

B

C

July 21, 2009

Programming and Data Structure

5

In essence ...
• For insertion:
– 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. July 21, 2009 Programming and Data Structure 6

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. Programming and Data Structure 7

July 21, 2009

Types of Lists
• Depending on the way in which the links are used to maintain adjacency, several different types of linked lists are possible. – Linear singly-linked list (or simply linear list)
• One we have discussed so far.
head

A

B

C

July 21, 2009

Programming and Data Structure

8

– Circular linked list
• The pointer from the last element in the list points back to the first element.

head

A

B

C

July 21, 2009

Programming and Data Structure

9

– Doubly linked list
• Pointers exist between adjacent nodes in both directions. • The list can be traversed either forward or backward. • Usually two pointers are maintained to keep track of the list, head and tail. head tail

A

B

C

July 21, 2009

Programming and Data Structure

10

Basic Operations on a List
• • • • • Creating a list Traversing the list Inserting an item in the list Deleting an item from the list Concatenating two lists into one

July 21, 2009

Programming and Data Structure

11

List is an Abstract Data Type
• What is an abstract data type?
– It is a data type defined by the user. – Typically more complex than simple data types like int, float, etc.

• Why abstract?
– Because details of the implementation are hidden. – When you do some operation on the list, say insert an element, you just call a function. – Details of how the list is implemented or how the insert function is written is no longer required. July 21, 2009 Programming and Data Structure 12

Conceptual Idea

Insert Delete Traverse List implementation and the related functions

July 21, 2009

Programming and Data Structure

13

Example: Working with linked list
• Consider the structure of a node as follows:
struct stud { int roll; char name[25]; int age; struct stud *next; }; /* A user-defined data type called “node” */ typedef struct stud node; node *head; July 21, 2009 Programming and Data Structure 14

Creating a List

July 21, 2009

Programming and Data Structure...
tracking img