0% found this document useful (0 votes)
2 views57 pages

L6__LinkedLists

The document outlines the design, implementation, and operations of linear lists, focusing on linked lists as a flexible data structure. It compares arrays and linked lists, detailing their respective advantages for insertion, deletion, and access. Additionally, it covers basic operations such as insertion, deletion, retrieval, and traversal, along with dynamic memory allocation and the use of pointers in linked list structures.

Uploaded by

xiy29440
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views57 pages

L6__LinkedLists

The document outlines the design, implementation, and operations of linear lists, focusing on linked lists as a flexible data structure. It compares arrays and linked lists, detailing their respective advantages for insertion, deletion, and access. Additionally, it covers basic operations such as insertion, deletion, retrieval, and traversal, along with dynamic memory allocation and the use of pointers in linked list structures.

Uploaded by

xiy29440
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 57

LISTS.

General Linear Lists

1
Objectives.

Explain the design, use, and operation of a linear list

Implement a linear list using a linked list structure

Understand the operation of the linear list ADT

 Write application programs using the linear list ADT

 Design and implement different link-list structures

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

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

4
Linked List- 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.
head
• It does not waste memory space.

A B C

5
6
Linked List- Introduction

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

7
Basic Operations.

Insertion

Deletion

Retrieval

Traversal

8
Insertion.

Insertion is used to add a new element to the list


9
Deletion.

Deletion is used to remove an element from the list.


10
Retrieval.

Retrieval is used to get the information related to an element without changing the
structure of the list. 11
Traversal.

• List traversal processes each element in a list in sequence.

12
Implementation.

13
Data structure.

14
Algorithms.
1 2 3 4

Create Insert Delete List


list node node search

5 6 7

Retrieve Empty Full list


node list

8 9 10

List Traverse Destroy


count list list
15
Create list.

struct node
{
int data;
node *next;
};

// Head pointer always points to


first element of the linked list

node *head = new node;


//create a node

head = NULL;
16
Pointer to structures
• Like other types, structures can also be accessed through pointers

• Accessing structure itself by


*ptr
• ptr contains the address of
the beginning of the
structure
• Now we do not only need to
use structure name with
member operator such as
sam1.x
• we can also use (*ptr).x
Figure 12-14 Pointers to structures
Selection operator

• However, there is a selection operator -> (minus sign and greater than
symbol) to eliminate the problem of pointer to structures
• The priority of selection operator (->) and member operator(.) are the
same
• The expressions :
• (*pointerName).fieldName is same as pointerName ->fieldName
• But pointerName -> fieldName is preferred
Figure 12-16 pointer selection operator
Pointers and Structures I
struct inventory
{
char name[30];
int number;
float price;
};

• inventory i; inventory *ptr = &i;

• Its members are accessed using the following notation


ptr→name
ptr→number
ptr→price
P o i n t e r s a n d S t r u c t u r e s II

• The symbol → is called arrow operator (also known as


member selection operator)
• The data members can also be accessed using
(*ptr).number

• Parentheses is required because ’.’ has higher precedence than


the operator *
Dynamic M e m o r y A l l o c a t i o n I
• Dynamic memory allocation and deallocation is done using two operators: new and
delete. An object can be created using new operator and destroyed by delete operator.
• A data object created inside a block with new will remain in existence until it is
destroyed by using delete.
Pointer_variable = new data_type;

• For example:
p = new int;
q = new float;

• Alternatively, int *p = new int;


• float *q = new float;
Create list.

24
Insert node.
Only its logical predecessor is needed.

There are three steps to the insertion:


Allocate memory for the new node and move data to the
node.
Point the new node to its successor.
Point the new node’s predecessor to the new node.

25
Insert node.

1. Insert into empty list

2. Insert at beginning

3. Insert in middle

4. Insert at end

26
Insert into empty list.

27
Insert into empty list

• Allocate memory for new node


• Store data
• Change next of new node to point to head
• Change head to point to recently created node

node *head = NULL;

node *newNode = new node; //create a node


newNode->data = value;

newNode->next = head;
head = newNode
28
Insert at beginning.
node *newNode = new node; //create a
node
newNode->data = value;

newNode->next = head;
head = newNode

29
Insert at end.
Insert at the End
•Allocate memory for new node
•Store data
•Traverse to last node
•Change next of last node to recently created
node

30
Insert at the End
• Allocate memory for new node
• Store data
• Traverse to last node
• Change next of last node to recently created node

node *newNode = new node; // Create a new node


newNode->data = data;
newNode->next = NULL;
node *temp = head;
while(temp->next != NULL)
{
temp = temp->next;
}
temp->next = newNode;
31
Insert in middle.

32
Insert in the Middle
• Allocate memory and store data for new node
• Traverse to node just before the required position of new node
• Change next pointers to include new node in between

node *newNode = new node; //create a node


newNode->data = value;
node *temp = head;
for(int i=1; i < position-1; i++)
{
if(temp->next != NULL)
{
temp = temp->next;
}
}
newNode->next = temp->next;
temp->next = newNode;
33
34
Delete node.

1. Delete first node

2. General delete case

35
Delete first node

36
Delete from beginning
• Point head to the second node

node *temp = head;


head = head->next

//free memory used by temp


temp=NULL;
delete temp;

37
Delete general case.

38
Delete from middle
• Traverse to element before the element to be deleted
• Change next pointers to exclude the node from the chain

node *cur=head;
node *prev = cur;
for(i=2; i<=position ;i++)
{
prev = cur;
if(cur->next != NULL)
cur=cur->next;
}
prev->next= prev->next->next;
39
40
List search

41
42
43
Retrieve node.

44
Empty list.

45
Full list.

46
List count.

47
Traversal list.

48
49
50
STACK
IMPLEMENTATIONS:
USING ARRAY AND
LINKED LIST

51
STACK USING ARRAY
PUSH

top
top

52
STACK USING ARRAY
POP

top
top

53
Stack: Linked List Structure

PUSH OPERATION

top

54
Stack: Linked List Structure

POP OPERATION

top

55
QUEUE: LINKED LIST STRUCTURE
DEQUEUE

front rear

56
THE END.
57

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy