Uploaded by thankachan sebastian

module2

advertisement
 An array is a fixed size sequential collection of elements of the
same data type that share a common name
 An array is a derived data type.
 An array is used to represent a list of numbers , or a list of names
 For Example :
 1. List of employees in an organization.
 2. Test scores of a class of students.
 We can use index or subscript to identify each element
or location in the memory
6
 Dimension refers to the array’s size, which is how big the
array is
 Based on dimension there are 3 types of arrays
 1.One-dimensional arrays
 2. Two-dimensional arrays
 3. Multidimensional array
7
 A variable which represent the list of items using only one index
(subscript) is called one-dimensional array.
8
 The general form of array declaration is :
 type array-name[size];
 Here the type specifies the data type of elements contained in the array,
such as int, float, or char.
 The size indicates the maximum numbers of elements that can be stored
inside the array.
 The size should be either a numeric constant or a symbolic constant.
9
 An array may be initialized at the time of declaration,giving initial values to an array.
 Initialization of an array may take the following form,
 type array_name[size] = {a_list_of_value};
 int array[7] = {1, 2, 3, 4, 5, 6, 7};
 char chVowel[6] = {'a', 'e', 'i', 'o', 'u', '\0'};
10
 A variable which represent the list of items using two index (subscript)
is called two-dimensional array.
 The general form of two dimensional array declaration is :
 type array-name[row_size][column_size];
 The general form of initializing two-dimensional array is :
 type array-name[row_size][column_size] = {list of values};
 Example :
 int table[2][3] = {0,0,0,1,1,1};
 Here the elements of first row initializes to zero and the elements of second
row initializes to one.
 This above statement can be written as :
 int table[2][3] = {{0,0,0}, {1,1,1}};
 In two-dimensional array the row_size can be omitted
 Example :
 int table[ ][3] = {{0,0,0}, {1,1,1}};
 If the values are missing in an initializer, they are automatically set
to zero.
 Example :
 int table[2][3] = {1,1,2};
 Here first row initialize to 1,1 and 2, and second row initialize to
0,0 and 0 automatically.
15
 A variable which represent the list of items using more than
two index (subscript) is called multi-dimensional array.
 Multidimensional Arrays can be defined in simple words as
array of arrays
 The general form of multi dimensional array is :
 type array-name[s1][s2][s3]…….[sn];
 Where S is the size. Some examples are :
 int survey[3][5][6];
1
 Traversing an array means accessing each and every element of the array for a
specific purpose.
 Traversing the data elements of an array A can include printing every element,
counting the total number of elements, or performing any process on these elements.
ALGORITHM
Step 1: [INITIALIZATION] SET I= lower_bound
Step 2: Repeat Steps 3 to 4 while I<= upper_bound
Step 3: Apply Process to A[I]
Step 4: SET I=I+1
[END OF LOOP]
Step 5: EXIT
1
1
#include <stdio.h>
printf("\n The array elements are ");
int main()
for(i=0;i<n;i++)
printf("\t %d", arr[i]);
{
return 0;
int i, n, arr[20];
}
printf("\n Enter the number of elements in the array : ");
OUTPUT
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("\n arr[%d] = ", i);
scanf("%d",&arr[i]);
Enter the number of elements in the array : 5
arr[0] = 1
arr[1] = 2
arr[2] = 3
arr[3] = 4
arr[4] = 5
The array elements are 1 2 3 4 5
}
1
2
Write a program to print the position of the smallest of n numbers using arrays
i#include<stdio.h>
printf("\nThesmallestelementis :%d",small);
printf("\nThepositionofthesmallestelementinthearrayis :%d", pos);
Void main()
return0;
{ inti,n,arr[20], small,pos;
}
printf("\nEnterthenoofelementsinthearray:");
scanf("%d",&n);
printf("\nEnter theelements :");
Output
for(i=0;i<n;i++)
Enter the number of elements in
scanf("%d",&arr[i]);
the array : 5 Enter the elements : 7
small= arr[0]
6 5 14 2
pos =0;
The smallest element is : 2
for(i=1;i<n;i++)
The position of the smallest element in
{
the array is : 4
if(arr[i]<small)
{
small= arr[i];
pos = i;
7
}}
 If an element has to be inserted at the end of an existing array, then the task of insertion is
quite simple.
 We just have to add 1 to the upper_bound and assign the value
ALGORITHM
Step 1: Set upper_bound = upper_bound+1
Step 2: Set A[upper_bound] = VAL
Step 3: EXIT
14
#include <stdio.h>
for(i=n–1;i>=pos;i--)
int main()
arr[i+1] = arr[i];
{
arr[pos] = num;
n = n+1;
int i, n, num, pos, arr[20];
printf("\n Enter the no. of elements in the array : "); printf("\n The array after insertion of %d is : ", num);
for(i=0;i<n;i++)
scanf("%d", &n);
printf("\t %d", arr[i]);
for(i=0;i<n;i++)
OUTPUT
return 0;
{
Enter the number of elements in the array : 2
}
arr[0] = 1
printf("\n arr[%d] = ", i);
arr[1] = 3
scanf("%d", &arr[i]);
Enter the number to be inserted : 2
}
Enter the position at which the number has to
printf("\n Enter the number to be inserted : ");
be added : 1
The array after insertion of 2 is :
scanf("%d", &num);
1
2 3
printf("\n Enter the position at which the number has to be added :");
scanf("%d", &pos);
15
#include <stdio.h>
int main()
{
int i, n, j, num, arr[10];
printf("\n Enter the number of elements in the array : ");
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("\n arr[%d] = ", i);
scanf("%d", &arr[i]);
}
printf("\n Enter the number to be inserted : ");
scanf("%d", &num);
for(i=0;i<n;i++)
{
if(arr[i] > num)
{
for(j = n–1; j>=i; j--)
arr[j+1] = arr[j];
arr[i] = num;
break;
}
}
n = n+1;
printf("\n The array after insertion of %d
is : ", num);
for(i=0;i<n;i++)
printf("\t %d", arr[i]);
return 0;
}
11
 Deleting an element from an array means removing a data element from an already existing
array.
 If the element has to be deleted from the end of the existing array, then the task of deletion is
quite simple.
 We just have to subtract 1 from the upper_bound.
ALGORITHM
Step 1: SET upper_bound = upper_bound-1
Step 2: EXIT
17
 The algorithm DELETE will be declared as DELETE(A, N,POS).
 The arguments are:
 (a) A, the array from which the element has to be deleted
 (b) N, the number of elements in the array
 (c) POS, the position from which the element has to be deleted
ALGORITHM
Step 1: [INITIALIZATION] SET I= POS
Step 2: Repeat Steps 3 and 4 while I<=N–1
Step 3: SET A[I] = A[I+1]
Step 4: SET I=I+1
[END OF LOOP]
Step 5: SET N=N–1
Step 6: EXIT
18
#include <stdio.h>
int main()
{
int i, n, pos, arr[10];
printf("\n The array after deletion is : ");
for(i=0;i<n;i++)
printf("\t %d",
arr[i]);
return 0; OUTPUT
printf("\n Enter the number of elements in the array : "); }
scanf("%d", &n);
Enter the number of elements in the array : 4
arr[0] = 1
arr[1] = 2
arr[2] = 3
arr[3] = 4
Enter the position from which the number has to
be deleted :2
The array after deletion is : 1 2 4
for(i=0;i<n;i++)
{
printf("\n arr[%d] = ", i);
scanf("%d", &arr[i]);
}
printf("\nEnter the position from which the number has to be deleted : ");
scanf("%d", &pos);
for(i=pos; i<n–1;i++)
arr[i] = arr[i+1];
n--;
1
4
#include <stdio.h>
for(i=0;i<n;i++)
int main()
{
{
if(arr[i] == num)
{
int i, n, j, num, arr[10];
for(j=i; j<n–1;j++)
clrscr();
printf("\n Enter the number of elements in the array : "); arr[j] = arr[j+1];
}
scanf("%d", &n);
}
for(i=0;i<n;i++)
n = n–1;
{
printf("\n The array after deletion is : ");
printf("\n arr[%d] = ", i);
for(i=0;i<n;i++)
scanf("%d", &arr[i]);
printf("\t %d", arr[i]);
}
return 0;
printf("\n Enter the number to be deleted : ");
}
scanf("%d", &num);
20
 Also known as structure on array (SoA)

Multiple arrays of the same size such that i-th element of each array is closely related and all
i-th elements together represent an object or entity.
 An example parallel array is two arrays that represent x and y co-ordinates of n points.
 Below is another example where we store the first name, last name and heights of 5 people in
three different arrays.
 first_name = [‘Anu', ‘Manu', ‘Tanu’, ‘Sanu’, ‘Hari']
 last_name = ['Smith', ‘Sager’, ‘Varma', ‘Singh', ‘Prasad']
 height
= [169, 158, 170, 183, 172]
 This way we could easily store the information and for accessing, the first index of each array
corresponds to the data belonging to the same person.
 Applications : Searching,Sorting
21
 A matrix is a two-dimensional data object made of m rows and n columns, therefore having
total m x n values.
 If most of the elements of the matrix have 0 value, then it is called a sparse matrix.
 Why to use Sparse Matrix instead of simple matrix ?
 Storage: There are lesser non-zero elements than zeros and thus lesser memory can be used to
store only those elements.
 Computing time: Computing time can be saved by logically designing a data structure
traversing only non-zero elements.
 To limit the processing time and space usage Sparse Matrix Representations can be done in
many ways .following are two common representations:
 1.Array representation
2.Linked list representation
17
 A 2D array is used to represent sparse matrix in which there are 3 columns named as
 a. Row – Row index of non-zero element
 b. Column – Column index of non-zero element
 c. Value – Value at the same row, column index in 2D matrix
 In this representation, the 0th row stores the total number of rows, total number of columns
and the total number of non-zero values in the sparse matrix
23
 In linked list, each node has four fields. These four fields are defined as:
 Row: Index of row, where non-zero element is located
 Column: Index of column, where non-zero element is located
 Value: Value of the non zero element located at index – (row,column)
 Next node: Address of the next node
 This type of representation is for those scenarios where the frequency of insertion and
deletion operation in the matrix is more since it is easier to delete and insert elements in
the linked list as compared to the arrays.
24
 A linked list is a collection of data elements called nodes in which the
linear representation is given by links from one node to the next node
structures.
 Since in a linked list, every node contains a pointer to another node which is of the
same type, it is also called a self-referential data type.
3
 Linked lists contain a pointer variable START that stores the address of the first node in the
list.
 We can traverse the entire list using START which contains the address of the first node; the
next part of the first node in turn stores the address of its succeeding node.
 Using this technique, the individual nodes of the list will form a chain of nodes.
 If START = NULL, then the linked list is empty and contains no nodes.
 In C, we can implement a linked list using the following code:
 struct node
{
int data;
struct node *next;
};
4
 If we want to add a node to an already existing linked list in the
memory, we first find free space in the memory and then use
it to store the information.
 The computer maintains a list of all free memory cells. This list of
available space is called the free pool
 We have a pointer variable AVAIL which stores the address of the first
free space.
 when a new student’s record has to be added, the memory address
pointed by AVAIL will be taken and used to store the desired
information.
 After the insertion, the next available free space’s address will be stored
in AVAIL
5
 When we delete a particular node from an existing linked list or delete the
entire linked list, the space occupied by it must be given back to the free pool
so that the memory can be reused by some other program that needs memory
space.
 The operating system does this task of adding the freed memory to the free
pool.
 The operating system scans through all the memory cells and marks those cells
that are being used by some program.
 Then it collects all the cells which are not being used and adds their address to
the free pool, so that these cells can be reused by other programs.
 This process is called garbage collection
6
Arrays
Linked list
Fixed size: Resizing is expensive
Dynamic size
Insertions and Deletions are inefficient:
Elements are usually shifted
Insertions and Deletions are efficient: No
shifting
Random access i.e., efficient indexing
Does not allow random access of data.
Nodes can be accessed only in a sequential manner
 Not suitable for operations requiring accessing
elements by index such as sorting
No memory waste if the array is full or almost full;
otherwise may result in much memory waste.
Since memory is allocated dynamically(acc. to our
need) there is no waste of memory.
Sequential access is faster [Reason: Elements in
contiguous memory locations]
Sequential access is slow [Reason: Elements not in
contiguous memory locations]
7
 There are different types of linked lists
 SINGLY LINKED LISTS
 A singly linked list is the simplest type of linked list in which every node contains some data
and a pointer to the next node of the same data type ie. the node stores the address of the next
node in sequence.
 A singly linked list allows traversal of data only in one way.
8
 CIRCULAR LINKED LIST
 In a circular linked list, the last node contains a pointer to the first node of the list.
 A circular linked list has no beginning and no ending
 The only downside of a circular linked list is the complexity of iteration.
 There are no NULL values in the NEXT part of any of the nodes of list.
 Circular linked lists are widely used in operating systems for task maintenance
9
 DOUBLY LINKED LISTS
 A doubly linked list or a two-way linked list is a more complex type of linked list which
contains a pointer to the next as well as the previous node in the sequence.
 Therefore, it consists of three parts—data, a pointer to the next node, and a pointer to the
previous node
 In C, the structure of a doubly linked list can be given as,
struct node
{
struct node *prev;
int data;
struct node *next;
};
10
 DOUBLY LINKED LISTS
 The PREV field of the first node and the NEXT field of the last node will contain NULL.
 The PREV field is used to store the address of the preceding node, which enables us to
traverse the list in the backward direction.
 The main advantage of using a doubly linked list is that it makes searching twice as efficient.
11
 CIRCULAR DOUBLY LINKED LIST
 A circular doubly linked list or a circular two-way linked list contains a pointer to the next as
well as the previous node in the sequence.
 The circular doubly linked list does not contain NULL in the previous field of the first node
and the next field of the last node.
 Rather, the next field of the last node stores the address of the first node of the list, i.e.,
START.
 Similarly, the previous field of the first field stores the address of the last node.
12
 HEADER LINKED LISTS
 A header linked list is a special type of linked list which contains a header node at the
beginning of the list. So, in a header linked list, START will not point to the first node of the
list but START will contain the address of the header node.
 The following are the two variants of a header linked list:
 Grounded header linked list which stores NULL in the next field of the last node.

Circular header linked list which stores the address of the header node in the next field of
the last node. Here, the header node will denote the end of the list
13
 Multi-Linked Lists
 In a multi-linked list, each node can have n number of pointers to other nodes. A doubly
linked list is a special case of multi-linked lists.
 We can differentiate a doubly linked list from a multi-linked list in two ways:
 (a) A doubly linked list has exactly two pointers. One pointer points to the previous node and
the other points to the next node. But a node in the multi-linked list can have any number of
pointers.
 (b) In a doubly linked list, pointers are exact inverses of each other, i.e., for every pointer
which points to a previous node there is a pointer which points to the next node. This is not
true for a multi-linked list
14
 Implementation of stacks and queues
 Implementation of graphs
 Dynamic memory allocation: We use a linked list of free blocks.
 Maintaining a directory of names
 Performing arithmetic operations on long integers
 Manipulation of polynomials by storing constants in the node of the linked list
 Representing sparse matrices
 Image viewer – Previous and next images are linked and can be accessed by the next
and previous buttons.
 Previous and next page in a web browser – We can access the previous and next
URL searched in a web browser by pressing the back and next buttons since they are
linked as a linked list.
 Music Player – Songs in the music player are linked to the previous and next songs.
So you can play songs either from starting or ending of the list.
15
#include<stdio.h>
#include<malloc.h>
structnode
{
intdata;
structnode*next;
};
structnode*start=NULL;
intmain(){
structnode*new_node,*ptr;int
num;
printf(“\nEnter-1toend”);
printf(“\nEnterthedata:“);
scanf(“%d”,&num);
while(num!=-1)
{
new_node=(structnode*)malloc(sizeof(structnode));
new_node->data =num;
if(start==NULL)
{
ptr->next=new_node;
new_node->next=NULL;
new_node->next=NULL;
start=new_node;
}
}
printf(“\nEnterthedata:“);
else
scanf(“%d”,&num);
{
}
ptr=start;
returnstart;
while(ptr->next!=NULL)
}
ptr=ptr->next;
4
 Traversing a linked list means accessing the nodes of the list in order to perform some
processing on them.
 For traversing the linked list, we also make use of another pointer variable PTR which points
to the node that is currently being accessed.
ALGORITHM
Step 1: [INITIALIZE] SET PTR = START
Step 2: Repeat Steps 3 and 4 while PTR != NULL
Step 3: Apply Process to PTR DATA
Step 4: SET PTR = PTR NEXT
[END OF LOOP]
Step 5: EXIT
5
#include<stdio.h>
#include<malloc.h>
structnode
{
intdata;
structnode*next;
};
structnode*start= NULL;
intmain()
{
structnode*ptr;
ptr = start;
while(ptr !=NULL)
{
printf(“\t%d”,ptr ->data);
ptr = ptr ->next;
}
6
ALGORITHM
Step 1: [INITIALIZE] SET PTR = START
Step 2: Repeat Step 3 while PTR != NULL
Step 3: IF VAL = PTR DATA
SET POS = PTR
Go To Step 5
ELSE
SET PTR = PTR NEXT
[END OF IF]
[END OF LOOP]
Step 4: SET POS = NULL
Step 5: EXIT
7
 Consider the linked list shown. If we have VAL = 4, then the flow of the
algorithm can be explained as shown
1
6
4
2
X
PTR
 Here PTR  DATA=1. Since PTR  DATA!= 4, we move to the next node
1
6
4
2
X
PTR
 Here PTR  DATA=6. Since PTR  DATA!= 4, we move to the next node
1
6
4
2
X
PTR
 Here PTR  DATA=4. Since PTR  DATA=4, POS = PTR. POS now stores
the address of the node that contains VAL
8
#include<stdio.h>
#include<malloc.h>
structnode
{
intdata;
structnode*next;
};
structnode*start=NULL;
intmain(){
structnode*new_node,*ptr;
intval,pos=0;
ptr=start;
printf(“enterthevaluetobesearched:”);
scanf(“%d”,&val);
while(ptr!=NULL)
{
if(ptr->data==val){
printf(“itemfoundatlocation%d”,pos);exit(0);
}
ptr=ptr->next;
pos++;
}
prinf(“itemnotfound”);
}
9
 When a new node is added into an already existing linked list there are four cases
 Case 1: The new node is inserted at the beginning.
 Case 2: The new node is inserted at the end.
 Case 3: The new node is inserted after a given node.
 Case 4: The new node is inserted before a given node
 Overflow is a condition that occurs when AVAIL = NULL or no free memory cell is
present in the system.
 When this condition occurs, the program must give an appropriate message.
10
 First check whether memory is available for the new node. If the free memory has exhausted,
then an OVERFLOW message is printed.
 Otherwise, if a free memory cell is available, then we allocate space for the new node
ALGORITHM
Step 1: IF AVAIL = NULL
Write OVERFLOW
Go to Step 7
[END OF IF]
Step 2: SET NEW_NODE = AVAIL
Step 3: SET AVAIL = AVAILNEXT
Step 4: SET NEW_NODE DATA = VAL
Step 5: SET NEW_NODE NEXT = START
Step 6: SET START = NEW_NODE
Step 7: EXIT
 Steps 2 & 3 allocate memory for the new node.
 In C, there are functions like malloc(), alloc, and calloc() which automatically do the memory
allocation on behalf of the user.
11
 Consider the linked list .we want to add a new node with data 9 and add it as the
first node of the list.
1
2
4
6
X
START
 Allocate memory for the new node and initialize its DATA part to 9.
9
 Add the new node as the first node of the list by making the NEXT part of the
new node contain the address of START
1
9
6
4
2
X
START
 Now make START to point to the first node of the list.
9
1
6
4
2
X
START
12
#include<stdio.h>
#include
<malloc.h>
structnode
{
intdata;
structnode*next;
};
intmain(){
structnode*new_node;
intnum;
printf(“\nEnterthedata:“);
scanf(“%d”,&num);
new_node= (struct node*)malloc(sizeof(structnode));
new_node->data= num;
new_node->next= start;
start= new_node;
}
13
ALGORITHM
Step 1: IF AVAIL = NULL
Write OVERFLOW
Go to Step 1
[END OF IF]
Step 2: SET NEW_NODE = AVAIL
Step 3: SET AVAIL = AVAIL NEXT
Step 4: SET NEW_NODE DATA = VAL
Step 5: SET NEW_NODENEXT = NULL
Step 6: SET PTR = START
Step 7: Repeat Step 8 while PTRNEXT != NULL
Step 8:
SET PTR = PTRNEXT
[END OF LOOP]
Step 9: SET PTRNEXT =NEW_NODE
Step 10: EXIT
14
 Consider the linked list .we want to add a new node with data 9 and add it as the
last node of the list.
1
2
4
6
X
START
 Allocate memory for the new node and initialize its DATA part to 9
9
X
and next part to NULL.
 Move a pointer PTR from START so that it points to the last node of the list.
1
6
4
2
X
PTR
START
 Add the new node after the node pointed by PTR by storing the address of the
new node in the NEXT part of PTR.
1
START
6
4
2
9
X
PTR
15
#include<stdio.h>
#include
<malloc.h> struct
node
{
intdata;
structnode*next;
};
intmain(){
structnode*ptr,*new_node;
intnum;
new_node= (struct node*)malloc(sizeof(structnode));
new_node->data= num;
new_node->next= NULL;
ptr = start;
while(ptr ->next!=NULL)
ptr = ptr ->next;
ptr ->next= new_node
}
printf(“\nEnter thedata:“);
scanf(“%d”,&num);
16
ALGORITHM
Step 1: IF AVAIL = NULL
Write OVERFLOW
Go to Step 12
[END OF IF]
Step 2:SET NEW_NODE = AVAIL
Step 3: SET AVAIL = AVAIL NEXT
Step 4: SET NEW_NODE DATA = VAL
Step 5: SET PTR = START
Step 6: SET PREPTR = PTR
Step 7: Repeat Steps 8 and 9 while PREPTR DATA != NUM
Step 8: SET PREPTR = PTR
Step 9: SET PTR = PTR NEXT
[END OF LOOP]
Step 10 : PREPTR NEXT = NEW_NODE
Step 11: SET NEW_NODE NEXT = PTR
Step 12: EXIT
17
 Consider the linked list .we want to add a new node with data 9 after the node
having data 4.
1
2
4
6
X
START
 Allocate memory for the new node and initialize its DATA part to 9
9
 Take two pointer variables PTR and PREPTR and initialize them with START
1
START
PREPTR PTR
6
4
2
X
 Move PTR and PREPTR until the DATApart of PREPTR =4.PTR will point to
node after 4
1
START
6
4
2
PREPTR
PTR
X
18
 Add the new node in between the nodes pointed by PREPTR and PTR
6
1
START
4
2
X
PREPTR
PTR
9
1
6
4
9
2
X
START
19
#include<stdio.h>
#include<malloc.h>
structnode
new_node=(structnode*)malloc(sizeof(structnode));
new_node->data=num;
ptr=start;
{
preptr=ptr;
intdata;
while(preptr->data!=val)
structnode*next;
{
};
preptr=ptr;
intmain(){
ptr=ptr->next;
structnode*new_node,*ptr,*preptr;int
}
num,val;
preptr->next=new_node;
printf(“\nEnterthedata:“);
new_node->next=ptr;
scanf(“%d”,&num);
}
printf(“\nEnterthevalueafterwhichthedatahastobeinserted:“);
scanf(“%d”,&val);
20
ALGORITHM
Step 1: IF AVAIL = NULL
Write OVERFLOW
Go to Step 12
[END OF IF]
Step 2:SET NEW_NODE = AVAIL
Step 3: SET AVAIL = AVAIL NEXT
Step 4: SET NEW_NODE DATA = VAL
Step 5: SET PTR = START
Step 6: SET PREPTR = PTR
Step 7: Repeat Steps 8 and 9 while PTR DATA != NUM
Step 8: SET PREPTR = PTR
Step 9: SET PTR = PTR NEXT
[END OF LOOP]
Step 10 : PREPTR NEXT =NEW_NODE
Step 11: SET NEW_NODENEXT = PTR
Step 12: EXIT
21
 Consider the linked list .we want to add a new node with data 9 before the node
having data 4.
1
2
4
6
X
START
 Allocate memory for the new node and initialize its DATA part to 9
9
 Take two pointer variables PTR and PREPTR and initialize them with START
1
START
PREPTR PTR
6
4
2
X
 Move PTR and PREPTR until the DATApart of PTR =4.PREPTR will point to
node before 4
1
6
4
START
PREPTR
PTR
2
X
22
 Add the new node in between the nodes pointed by PREPTR and PTR
1
6
4
START
PREPTR
PTR
2
X
9
1
6
9
4
2
X
START
23
#include<stdio.h>
#include<malloc.h>
structnode
{
intdata;
structnode*next;
};
intmain(){
structnode*new_node,*ptr,*preptr;int
num,val;
new_node=(structnode*)malloc(sizeof(structnode));
new_node->data=num;
ptr=start;
while(ptr->data!=val)
{
preptr=ptr;
ptr=ptr->next;
}
preptr->next=new_node;
new_node->next=ptr;return
start;
printf(“\nEnterthedata:“);
scanf(“%d”,&num);
}
printf(“\nEnterthevaluebeforewhichthedatahastobeinserted:“);
scanf(“%d”,&val);
24
 There are three cases in deletion .
 Case 1: The first node is deleted.
 Case 2: The last node is deleted.
 Case 3: The node after a given node is deleted.
 Underflow is a condition that occurs when we try to delete a node from a linked list that is
empty.
 This happens when START = NULL or when there are no more nodes to delete.
25
ALGORITHM
Step 1: IF START = NULL
Write UNDERFLOW
Go to Step 5
[END OF IF]
Step 2: SET PTR = START
Step 3: SET START = START NEXT
Step 4: FREE PTR
Step 5: EXIT
26
#include<stdio.h>
#include<malloc.h>
structnode
{
intdata;
structnode*next;
};
intmain(){
structnode*ptr;
ptr = start;
start= start->next;
free(ptr);
returnstart;
}
27
ALGORITHM
Step 1: IF START = NULL
Write UNDERFLOW
Go to Step 8
[END OF IF]
Step 2: SET PTR = START
Step 3: Repeat Steps 4 and 5 while PTR NEXT != NULL
Step 4: SET PREPTR = PTR
Step 5: SET PTR = PTR NEXT
[END OF LOOP]
Step 6: SET PREPTR NEXT = NULL
Step 7: FREE PTR
Step 8: EXIT
28
 Consider the linked list .
1
2
4
6
X
START
 Take two pointer variables PTR and PREPTR and initialize them with START
1
START
PREPTR PTR
6
4
2
X
 Move PTR and PREPTR until the NEXT part of PTR = NULL. PREPTR
always points to the node just before the node pointed by PTR.
1
6
4
2
PREPTR
START
X
PTR
 Set the NEXT part of PREPTR node to NULL and free PTR.
1
START
6
4
X
PREPTR
29
#include<stdio.h>
#include<malloc.h>
structnode
{
intdata;
structnode*next;
};
intmain(){
structnode*ptr,*preptr;
ptr = start;
while(ptr ->next!=NULL)
{
preptr= ptr;
ptr = ptr ->next;
}
preptr ->next=NULL;
free(ptr);
returnstart;
}
30
ALGORITHM
Step 1: IF START = NULL
Write UNDERFLOW
Go to Step 1
[END OF IF]
Step 2: SET PTR = START
Step 3: SET PREPTR = PTR
Step 4: Repeat Steps 5 and 6 while PREPTR DATA != NUM
Step 5: SET PREPTR = PTR
Step 6: SET PTR = PTR NEXT
[END OF LOOP]
Step 7: SET TEMP = PTR
Step 8: SET PREPTR NEXT = PTR NEXT
Step 9: FREE TEMP
Step 1 : EXIT
31
 Consider the linked list . delete the node after the node with data value 6.
1
2
4
6
X
START
 Take two pointer variables PTR and PREPTR and initialize them with START
1
START
PREPTR PTR
6
4
2
X
 Move PTR and PREPTR until the PREPTR points to the node containing VAL
and PTR points to the succeeding node
1
6
4
START
PREPTR
PTR
1
6
4
START
PREPTR
PTR
2
X
 Set the NEXT part of PREPTR to the NEXT part of PTR.
2
X
32
#include<stdio.h>
#includemalloc.h>
structnode
while(preptr ->data!=val)
{
{
preptr= ptr;
intdata;
ptr = ptr ->next;
structnode*next;
}
};
preptr->next= ptr ->next;
intmain(){
free(ptr);
structnode*ptr,*preptr;
returnstart;
intval;
}
printf(“\nEnterthevalueofthenodetobedeleted:“);
scanf(“%d”,&val);
ptr = start;
Preptr=ptr;
33
Download