Linked List4 1725361521928
Linked List4 1725361521928
Extension of modularity
Defines data along with a set of
operations on the data
Mathematical abstraction
How the set of operations
implemented is hidden from user
Encapsulates the implementation
ADT = set of data items + set of
possible
operations
Eg: ADT1 – Set with operations Union
& Intersection
ADT 2 – Set with Union and
Difference operations
2
A finite sequence of elements with order
taken into account
A = a1, a2, a3, …. ai, ai+1, …..a n
first element
second element
i th element
a i is in position ‘i’
3
List ADT – list with operations – insert,
delete, search, display, modify
2 ways of implementing list ADT
Using arrays
Using linked list
4
Operations – insert, search, delete,
modify, IsEmpty, IsFull, finding kth
element
Insert x in location ‘pos’
5 8 arr 12 7
5
arr
5 8 x 12 7
https://www.youtube.com/watch?v=KELq
VT7hjeE
6
Implement list ADT using array with 3
files.
7
Array size has to be fixed initially.
Array size can not be modified
If array has lesser number of
elements, then memory is wasted.
If array has to store more number of
elements, then array is not sufficient
Insert operation needs data
movement
Delete operation needs data movement
Therefore use linked list
8
Node has
data
Pointer to next node
data Pointer
to next
node
3 250 8 350 7
0 0
1000 2500 3500
4000 3500
10
1000 2500 4000 3500
Deleting 5
8 3500
1000 2500 3500
Deleting 7
8
1000 2500
Deleting 3 Deleting 8 List becomes empty
8
Want to insert 10 in the same list??
2500 Therefore dummy header used
11
L
100
4
0
1000 2500 4000 3500
Linked list operations:
1)Display: void Display(list L)
/* Start with first node*/
{
p = L->next;
/* Looping*/
while (p!= NULL) {
/*Display the content */
printf(“%d\n”, pdata);
/* Move to next node*/
p = pnext; }
12
L
100
4
0
1000 2500 4000 3500
2) Search or Find() :
/* Start with first node*/
/*Looping */
4000
Inserting new
L node
prev
4000
100
4
0
1000 2500 3500
temp 2
1
4000 3500 14
15
1000 2500 4000 3500
Deleting 5
8 3500
1000 2500 3500
Deleting 7
8
1000 2500
Deleting 3
8
2500
16
/* Header file “list.h”*/
#ifndef list.h
#include <stdio.h>
typedef int elementType;
struct node
{
elementType data;
struct node *next;
};
typedef struct node * ptrToNode;
typedef ptrToNode list, position;
17
/* Function declaration */
/* checking whether list is empty*/
int IsEmpty(list);
19
/* Function for printing the list */
void Display(list);
20
/*Implementation file – “list.c” */
#include “list.h”
/* Function for printing the list */
void Display(list L)
{
......
.......
}
position Find( elementType x, list L)
{
......
.......
} 21
22
/* Function for inserting the node ‘x’
after the node pointed by ‘prev’ */
void Insert( elementType x, position
prev,
list L)
{
position temp ;
/* Allocate memory space for new node
*/
23
/* Assign data value to temporary node
*/
24
/* Function for finding the previous node
of the node having ‘x’ */
position FindPrev( elementType x, list L)
{
position p ;
/* Start with first node*/
/*looping*/
/*
*/
26
27
28
/* Function for checking whether the given
node is the
last node in the list */
int IsLast( position p, list L)
{
/* Check the next pointer of node and
return 1 or 0*/
29
/* Function for deleting the node having ‘x’
*/
int Delete( elementType x, list L)
{
position prev, temp ;
/* Find the previous node of the node having
x*/
31
/* Function for checking empty list*/
void IsEmpty( list L)
{
32
/* Function for initializing dummy header*/
Initialize( )
{
{
position temp ;
/* Allocate memory space for new node */
}
33
/* Assign data value to temporary node
*/
34
/* Function for deleting entire list*/
void DeleteList( list L)
{
/* looping */
/* delete node*/
/* delete header */
}
35
/* Function for displaying menu*/
void Menu( )
{
/* Display different choices */
}
36
/* Main file ‘listmain.c’ */
#include “list.h”
void main( )
{
int choice, x, y ;
position p;
list l;
/ Initalize the list */
/* display menu */
37
/* Read the choice */
break ;
/* Find */
Case 2 :
printf (“Enter value to be searched\n”) ;
39
/* read value to be searched*/
break ;
40
/* delete*/
case 3 :
/* Check whether list is empty*/
/* call Delete() */
41
/* continue for other cases also */
42
Given two null terminated linked lists of
employee details. The employee details
contain name and age of employees.
Implement all list operations. Also write
a function to combine their nodes so
that the nodes of the new list alternate
between those of the two original
nodes: <first node of first list, first node
of second list, second node of first list,
second node of second list, ... >. Do not
allocate any new nodes.
43
Create two polynomials using linked list.
Implement all linked list operations.
Also write functions for polynomial
addition and multiplication.
44
https://www.youtube.com/watch?v=g2o2
2C3CRfU
45
46