DSA Chapter 03 (List)
DSA Chapter 03 (List)
DSA Chapter 03 (List)
Lecture 04
Manish Aryal
Data Structures and Algorithms
Key Learning Points
Queue
An ordered list in which data can be added from an end
called as rear and removed from other end called as
front
FIFO
Operations
Insert/Enqueue: addition of data from rear
Delete/Dequeue: remove data from front
Linear, Circular and Priority Queue
2
Data Structures and Algorithms
Teach to Learn
Rear points to the topmost and Front points to bottom most element of queue.
[True/False]
Queue is Last-in-Last-Out list. [True/False]
If we insert the elements A, B, C, D, in that order, in a Queue then ……. is the first
element we delete from the queue
In a circular queue, after every insert operation rear is updated as
a. rear++ b. rear = (rear -1) % MAXQSIZE c. rear= front d. rear = (rear +1) % MAXQSIZE
4
List
Objectives
After completion of this chapter you will be able to:
Define list as a Data Structure
Define static and dynamic list
Compare static and dynamic list
Implement the list in form of an array
Implement Queue as a list
Attempt 6 marks’ question in final exams
5
List
Definition
A linear list is a data structure each element of which
has a unique successor:
Restricted list
6
List
Definition {Contd..}
General list:
no restrictions on where data can be inserted/deleted, and on
which operations can be used on the list
Random list:there is no ordering on data
25
10 20 30
10 20 25 30
9
List
Operations {Contd..}
Deletion
Deletion from a general list requires searching the list in
order to locate the data being deleted.
Array requires physical shifting after the data is deleted.
10 20 25 30
25
10 20 30
10
List
Implementation using Array
void menu()
void create();
void insert(int, int);
void del(int);
void find(int);
void display();
int isfull();
int isempty();
11
List
Implementation using Array {Contd..}
int menu()
int ch;
clrscr();
printf("\n\t LIST Implementation using Arrays");
printf("\n\t1. Create\n\t2. Insert\n\t3. Delete\n\t4. Count\n\t5.
Find\n\t6. Display\n\t7. Exit\n\nEnter Your Choice:");
scanf("%d",&ch);
printf("\n\n");
return ch;
12
List
Implementation using Array {Contd..}
void create()
int element;
int flag=1;
while(flag==1)
{
printf("\nEnter an element:");
scanf("%d", &element);
l.list[l.length]=element;
l.length++;
printf("\nPress '1' to insert another element!\n");
scanf("%d",&flag);
}
13
List
Implementation using Array {Contd..}
void insert(int, int)
int i;
if(pos==0)
{
printf("\n\nCannot insert at zeroth position.");
getch();
return;
}
if(pos-1>l.length)
{
printf("\n\nOnly %d elements exit. Cannot insert at %d
position",l.length,pos);
printf("\nPress any key to continue..");
getch();
}
14
List
Implementation using Array {Contd..}
else
{
for(i=l.length;i>=pos-1;i--)
{
l.list[i+1]=l.list[i];
}
l.list[pos-1]=element;
l.length++;
}
15
List
Implementation using Array {Contd..}
void del(int)
int i;
if(pos==0)
{
printf("\n\n No Element at zerothposition");
getch();
return;
}
if(pos>l.length)
{
printf("\n\nOnly%d elements. Cannot delete element at %d position.", l.length, pos);
printf("Press any key to continue..");
return;
}
for(i=pos;i<l.length;i++)
{
l.list[i]=l.list[i+1];
}
l.length--;
16
List
Implementation using Array {Contd..}
Void count()
printf("\nEnter number ofelements in the list is %d",l.length);
printf("\nPress any key to continue..");
17
List
Implementation using Array {Contd..}
void find(int)
int i;
int flag=1;
for(i=0;i<l.length;i++)
{
if(l.list[i]==element)
{
printf("%d exists at %d position", element, i+1);
flag=0;
printf("\nPress any key to continue..");
getch();
break;
}
}
if(flag==1)
{
printf("%d not found.\nPress any key to continue..", element);
getch();
}
18
List
Implementation using Array {Contd..}
void display()
int i;
for(i=0;i<l.length;i++)
{
printf("\nElement: %d\tPosition: %d",l.list[i], i+1);
}
printf("\nPress any key to continue...");
getch();
19
List
Implementation using Array {Contd..}
int isfull()
if(l.length==MAXNODE)
return(1);
else
return(0);
20
List
Implementation using Array {Contd..}
int isempty()
if(l.length==0)
return(1);
else
return(0);
21
Next Lecture:
Linked List
Thank You.