0% found this document useful (0 votes)
23 views

Linked List4 1725361521928

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

Linked List4 1725361521928

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

1

 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

 If pos = 2, it should be inserted as third


element
 Elements from position 2 onwards,
should5
be moved
8
right by 112 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

100 3 250 8 350 7


0 0 0
dummy header 1000 2500
3500
9
 Inserting 5 after 8

1000 2500 3500

Allocating new node


4000

INSERTING NEW NODE


4000

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”, pdata);
/* Move to next node*/
p = pnext; }
12
L
100
4
0
1000 2500 4000 3500

2) Search or Find() :
/* Start with first node*/

/*Looping */

/*Check whether the content is equal to the


searched value*/

/* Move to next node*/


13
1000 2500 3500
Allocating temp
memory for new
node

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);

/* checking whether the given node


in ‘position’ is the last node in the
list */
int IsLast(position, list);

/* Searching for x; The function


returns the position where x is
available */
position Find(elementType, list);
18
/* Inserting node with ‘x’ after the node
pointed by ‘p’ */
void Insert(elementType, position, list);

/*Function for finding the previous node of


the node having ‘x’ */
position FindPrev(elementType, list) ;

/*Function for deleting the node having ‘x’ */


int Delete(elementType, list) ;

19
/* Function for printing the list */
void Display(list);

/*Function for deleting the entire list*/


void DeleteList(list) ;

/*Function for initializing the header node */


position Initialize( ) ;
endif

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
*/

/*Check if memory has been allocated


properly */

23
/* Assign data value to temporary node
*/

/*Adjust the pointers for insertion*/

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*/

/* Check if x is available in the next


node*/

/* If not available, move to next node


*/
25
/* If x is not available in the list ?? */

/*
*/

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*/

/* Check if the previous node is last node &


return*/

/*store the address of node to be deleted in


temporary location*/
30
/* adjust the pointers for deletion */

/* Free the memory location of the


deleted node*/

/* return positive value*/

31
/* Function for checking empty list*/
void IsEmpty( list L)
{

/* Check the next pointer of Header node and


return 1 or 0*/

32
/* Function for initializing dummy header*/
Initialize( )
{
{
position temp ;
/* Allocate memory space for new node */

/*Check if memory has been allocated


properly */

}
33
/* Assign data value to temporary node
*/

/*Assign the pointer to temporary


node */

/* Return the pointer*/

34
/* Function for deleting entire list*/
void DeleteList( list L)
{

/* Start with first node in the list */

/* 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 */

/* switch statement for choice*/


switch(choice)
{
/* Insert */
case 1 :
printf(“Enter value to be inserted\
n”);
scanf(“%d”, &x) ;

/* If list is empty, insert at the


beginning*/
38
/* Otherwise, insert after the node ‘y’*/
else {
/* read the value of y */

/*Find the position of y */

/* Insert after the node y */

break ;
/* Find */
Case 2 :
printf (“Enter value to be searched\n”) ;

39
/* read value to be searched*/

/* call find ()*/

/*Print whether x is available*/

break ;

40
/* delete*/
case 3 :
/* Check whether list is empty*/

/*read the value to be deleted*/


prinf(“Enter value to be deleted\n”)’

/* call Delete() */

/* Check if Delete() is successful */

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

You might also like