Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 18

UNIT III LINEAR DATA STRUCTURES 10

Abstract Data Types (ADTs) – List ADT – array-based implementation – linked list implementation –– singly linked lists –
Polynomial Manipulation - Stack ADT – Queue ADT - Evaluating arithmetic expressions

Introduction to Abstract Data Types:


An Abstract Data type (ADT) is a set of operations that can be viewed as an extension of modular design. Objects like lists,
sets and graphs, along with their operation, can be viewed as abstract data types. The basic idea is that implementation of these
operations is written once in the program, and any other part of the program that needs to perform an operation on the ADT can do so
by calling the appropriate function. Examples are,
 List ADT
 Stack ADT
 Queue ADT, …..
LIST
A list is a sequence of zero or more elements of a given type. The list is represented as sequence of elements separated by
comma.
A 1 , A 2 , A 3 ,…., A N
Where, N > 0 and Ai is of type element.
4.3 Implementation of list:
The list can be implemented in the following ways:
1. Array implementation
2. Linked – list implementation
3. Cursor implementation.
4.4 Array Implementation of list:
 The very common linear structure is array. An array is a list of a finite number n of similar data such that:
a) The elements of the array are referenced respectively by an index consisting of n consecutive numbers.
b) The elements of the array are stored respectively in successive memory locations.
 Since arrays are usually easy to traverse, search and sort, they are frequently used to store relatively permanent collections of
data.

Operations of list:
The various operations that can be performed on a list are as follows:
 Insertion
 Deletion
 Locate
 Retrieve
 Displaying list
4.2.2.1 Insert operation:
Inserting an item in the list involves traversing till the position, ‘p’ where item, ‘x’ is to be inserted and all the items starting from
the position ‘p’ is moved one position down the list and the item, ‘x’ inserted at position, ‘p’
Consider the list element shown below,
0 1 2 3 4 Position of item
10 20 30 50 60

The item, x to be inserted is 40 and the position, p = 3.so, all the items starting from the position ‘p’ is moved one position down the list
0 1 2 3 4 5

10 20 30 50 60

Then, the item, x = 40 is inserted at the position, p = 3

0 1 2 3 4 5
10 20 30 40 50 60
4.2.2.2 Deletion operation:
Deleting an item, x from the list involves traversing the list till the position, p. Delete the item in the position, p and moving items
next to the deleted item, one position ahead. Consider the elements shown below.
0 1 2 3 4 5

10 20 30 40 50 60

Here, the position, p = 2 and the item, ‘x’ to be deleted = 30


1
Delete the item in the position,p = 2
0 1 2 3 4 5
10 20 40 50 60
All items starting from the position, p is moved one position ahead.
0 1 2 3 4

10 20 40 50 60

Hence, the item, x = 30 is deleted from the list


4.2.2.3 Location operation:
Locating the position of the given element, if it is present in the list is location operation. Consider the list shown below,
0 1 2 3 4

10 20 30 40 50 60

Here, the position of an item, 30 it to be located. Traverse the list, to find the position of an item, 30. The item, 30 is present in the list
its position is returned as 2. Consider the list shown below,
0 1 2 3 4

10 20 30 40 50 60

Here, the position of an item, 70 is to be located. The item, 70 is not present in the list. So, it cannot be located.
4.2.2.4 Retrieve operation:
For a given position and a given list, the element has to be retrieved corresponding to the position. Consider the following list,
0 1 2 3 4
10 20 30 40 50 60
The entire list is checked for the given position. If the position is not present, display “position does not exist”. Otherwise, the element
in that position is printed. Here, the retrieved element is 30.
4.2.2.5 Print list operation:
Printing all items or elements in the list. Consider the list show below
0 1 2 3 4

10 20 30 40 50 60
The element starting from the first position of the list to the last position of list is printed as 10, 20, 30, 40, 50, and 60

Write a C++ program for array implementation of list ADT – REFER LAB EXERCISE

4.5 Linked lists Implementation:


In order to avoid the linear cost of insertion and deletion, we need to ensure that the list is not stored contiguously, since
otherwise entire parts of the list will need to be moved. The general idea of linked lists is as follows:
The linked list consists of a series of structures /nodes
 Each node contains two fields:
o Data (element)
o Next (address of the next node)
 The last cell’s next pointer points to NULL.
There are three types of the linked lists, which are as follows.
i) Singly Linked List
ii) Doubly Linked List
iii) Circularly Linked List
4.5.1 Singly Linked Lists:
The representation of a singly linked list is shown below:

Head

Node
NULL

Node
Data Next
2
Figure 4.2 Singly Linked List
 Each node contains two fields.
o The first field contains the data and second contains the address of the next node.
 Head will always contain one field, i.e., the address of the first
 Last node’s next field is NULL, to indicate the end of the list
1000 Head

10 2000 20 3000 30 NULL

1000 2000 3000


Figure 4.3 Example for Singly Linked List
4.5.1.1 Operations of Singly linked list:
We have to specify to specify the operations that can be performed on a singly linked list. The operations that can be
performed on a singly linked list are given below.
4.5.1.2 Insert operation in singly linked list:
Inserting an element into a linked list insertion in a singly linked list falls under three categories and they are as follows,
a) Insertion at beginning
b) Insertion at given position
c) Insertion at end
Routine for Insertion at beginning:
void list:: insbeg(int x)
{
node *q;
q=p;
p=new node;
p->data=x;
p->link=q;
}
Insertion at beginning: For example, consider the singly linked list shown below:
1000 p

10 2000 20 3000 30 NULL

1000 2000 3000


Figure 4.4 Singly Linked List for inserting first
Suppose, we want to add a new node whose address is 1000 at the beginning of the list. The steps given below are to be followed.
Step1: Assign the address of the first node p to the q. Now, both p and q holds the address of the first node..
1000 p q

10 2000 20 3000 30 NULL

1000 2000 3000


Step 2: Create a new node p. Assume the address to be 500 and data to be placed is 5. Assign the data, to the data field of the new node
p.
1000
q

p
10 2000 20 3000 30 NULL
5
3
500 1000 2000 3000
Step 3: assign the address of the first node q to the “next field” of the new node p.
1000
q

p
10 2000 20 3000 30 NULL
5 1000
500 1000 2000 3000
Step 4: Now, p holds the address of the first node which has 500.

p 500

10 2000 20 3000 30 NULL


5 1000

1000 2000 3000


500
Insertion at the Given Position:
Suppose, we want to add a new node at the given position in the list, we must specify the position of the list the node
has to be inserted. For inserting the node at the given position the list, the steps given below are to be followed. For
example, consider the singly linked list shown below, and insert the element 25 at position, 3.
1000 p

10 2000 20 3000 30 NULL

1000 2000 3000


Figure 4.5 Singly Linked List for inserting 25 at position 3
Step1: Create a new node. Assign the data that is read to the data field of the new node.
1000
p

25 10 2000 20 3000 30 NULL

2500
1000 2000 3000
Step 2: Traverse the list up to the position 2(3-1). The result of traversing up to the position 2.
1000 p

10 2000 20 3000 30 NULL

1000 2000 3000


25 2000

2500
Step 3: The next field value of the node at position, 2 is assigned the address of new node.
1000 Head

10 2000 20 3000 30 NULL

2000 3000
4
1000

25 3000
Step 4: The next field value of the node at position, 2 is replaces with the new node’s address
1000 Head

10 2000 20 2500 30 NULL

1000 2000 3000


Routine for Insertion at the Given Position
void list :: insnext(int value,int position)
25 3000
{
node *temp,*temp1;
temp=p; 2500
if(p==NULL)
{
temp1= new node;
temp1->data=value;
temp1->link=NULL;
p=temp1;
}
for(int i=0;((i<position)&&(temp->link!=NULL)) ;i++)
{
if(i==(position-1))
{
temp1= new node;
temp1->data= value;
temp1->link=temp->link;
temp->link=temp1;
}
temp=temp->link;
}
}
Insertion at the end:
Suppose, we want to insert an element of the end of the list we have to follow the steps give below. Consider the singly linked list
shown below,
1000
p

10 2000 20 3000 30 NULL

1000 2000 3000


Figure Singly Linked List for inserting 40 at end
Step 1: Create a new node and place the data in the data field of new node.

1000 p

40 20 3000 30 NULL
10 2000
4000 5
1000 2000 3000
Step2: Assign the address of the first node p to the q. Now, both p and q holds the address of the first node.
1000 p q
40

4000
10 2000 20 3000 30 NULL

1000 2000 3000


Step 2: Traverse the list, until the last node is reached.
1000 p

10 2000 20 3000 30 NULL

1000 2000 3000


40
Step 3: Next field of the last node is assigned the address of the new node
4000
1000 Head

10 2000 20 3000 30 4000

1000 2000 3000


40 NULL
Step 4: new node’s next field is assigned NULL. Since, it is a last node in the list.
4000
1000 p

10 2000 20 3000 30 4000

1000 2000 3000


40
Routine for Insertion at the end
void list::inslast(int x)
{ 4000
node *q,*t;
if(p==NULL)
{
p=new node;
p->data=x;
p->link=NULL;
}
else
{
q=p;
while(q->link!=NULL)

6
q=q->link;
t=new node;
t->data=x;
t->link=NULL;
q->link=t;
}
}
4.5.1.3 Delete Operations in singly linked list:
Deleting an element from the list. Deletion in a singly linked list falls under three categories
i) Deletion at beginning.
ii) Deletion at given position.
iii) Deletion at end.
Deletion at Beginning: Deleting a node at the first position involves the following steps. Consider the singly linked list shown below,
1000 P

10 2000 20 3000 30 NULL

1000 2000 3000


Figure 4.7 Singly Linked List for Deleting at first
Step 1: Assign the first nodes a next field value to the P node.

P 2000

10 2000 20 3000 30 NULL

1000 2000 3000


Step 2: Delete the node at the beginning

Routine for Deletion at Beginning P 2000


void list:: delbeg()
{
cout<<"\nThe list before deletion: ";
node *q; 20 3000 30 NULL
q=p;
if(q==NULL) 2000 3000
cout<<"\nNo data is present..";
p=q->link;
delete q;
}
Deletion at the given position:
For deleting the node at the given position in the list, the steps given below are to be followed. Consider the singly linked list shown
below, and delete the node at position 2
1000 P

10 2000 20 3000 30 NULL

1000 2000 3000


Figure 4.8 Singly Linked List for Deleting node at position 2
Step 1: Create two dummy pointers. One dummy pointer contains the same address as the head.

7
P 1000 1000 p1

10 2000 20 3000 30 NULL

Step 2: Traverse the list using the dummy pointer p1 till it reaches the previous node of the node to be deleted and assign the next field
1000 2000 3000
of p1 to p2.
p 1000 1000 p1 2000 p2

10 2000 20 3000 30 NULL

1000 2000 3000


Step3: Assign the address of p2’s next field to p1 next field. By assigning 3000 to p1’s next field automatically a link will be made as
1000 1000 p1 2000 shown below:
Head p2

10 3000 20 3000 30 NULL

1000 2000 3000


Step 4: Delete both p1 and p2
1000 Head

10 2000 30 NULL

1000 3000
Routine for Deletion at the given position
void list::delelement(int x)
{
node *q,*r;
q=p;
if(q->data= =x) {
p=q->link;
delete q;
return; }
r=q;
while(q!=NULL)
{
if(q->data==x) {
r->link=q->link;
delete q;
return; }
r=q;
q=q->link;
} cout<<"\nElement you entered "<<x<<" is not found..";
}
Deletion at the end:
Suppose, we want to delete an element at the end of the list we have to follow the steps given below. Consider the singly linked
list shown below:
1000 Head

8
10 2000 20 3000 30 NULL

2000 3000
Figure Singly Linked List for Deleting at end.

Step 1: Create two pointers p1 and p2 and assign the address of head to p1.

Head 1000 1000 p1

10 2000 20 3000 30 NULL

1000 2000 3000


Step 2: Traverse the list using p1 until it reaches the previous node of the last node in the list.
Head 1000
2000 p1

10 2000 20 3000 30 NULL

1000 2000 3000


Step 3: Assign the next field of p1 to p2
Head 1000
2000 p1 3000 p2

10 2000 20 3000 30 NULL

1000 2000 3000


Step 4: make the next field of p1 to NULL
Head 1000
2000 p1 3000 p2

10 2000 20 NULL 30 NULL

1000 2000 3000


Step 5: Delete p1 and p2
1000 Head

10 2000 20 NULL

void list:: dellast()


{ 1000 2000
cout<<"\nThe list before deletion: ";
disp();
node *q,*t;
q=p;
if(q==NULL)
cout<<"\nThere is no data in the list..";

9
if(q->link==NULL)
{
p=q->link;
delete q;
}
while(q->link->link!=NULL)
q=q->link;
q->link=NULL;
}
Linked list implementation of List ADT - ******REFER LAB EXERCISE******

Polynomial Manipulation
 A polynomial is a sum of terms. Each term consists of a coefficient and a variable raised to an exponent.
 Polynomial can implemented by using Linked List.
 Each node of the linked list stores the coefficient and the exponent and also store in the sorted order of exponents.
Example: P(x) = 4x3+6x2+7x+9

Addition on polynomials :
 Let P1 and P2 be two polynomials stored as linked lists in sorted order of exponents
 Add terms of like-exponents.We have P1 and P2 arranged in a linked list in decreasing order of exponents.
 We can scan these and add like terms.
 Need to store the resulting term only if it has non-zero coefficient.
 The number of terms in the result polynomial P1+P2 need not be known in advance.We'll use as much space as there are terms
in P1+P2.
Ex: (3x5 – 9x3 + 4x2) + (–8x5 + 8x3 + 2)
= 3x5 – 8x5 – 9x3 + 8x3 + 4x2 + 2
= –5x5 – x3 + 4x2 + 2
Polynomial ADT for Addition
The most important work of linked list is the polynomial addition. In this, linked list is used to add two or more polynomial equations.
To add a polynomial equation it checks three conditions, which is explained in three cases.
CASE I:
When the power of the polynomial 1 is greater than the power of the polynomial 2, then the resultant polynomial’s first
coefficient will have the power of the polynomial 1.
Pseudo code:
If(P1->PWR >P2-> PWR)
{
P -> PWR = P1-> PWR;
P ->Coef= P1-> Coef;
P1= P1->next;
}
CASE II:
When the power of the polynomial 2 is greater than the power of the polynomial 1, then the resultant polynomial’s first
coefficient will have the power of the polynomial 2.
Pseudo-code:
If(P1-> PWR <P2-> PWR)
{
P -> PWR = P2-> PWR;
P -> Coef= P2>Coef;
P1= P1->next;
}
CASE 3:
When the power of the polynomial 1 is equal to the power of the polynomial 2, then normal addition is carried out and the
coefficient have the same power.
Pseudo code:
If(P1-> PWR = =P2-> PWR)
{
P-> PWR = P1-> PWR;
P->Coef = P1->Coef + P2->Coef;
P1= P1-> Next;
10
P2= P2-> Next;
}
//Program for Addition of Two Polynomial cin>>newl->coeff;
#include <iostream.h> newl->next=NULL;
#include <iomanip.h> if(poly2==NULL)
struct poly poly2=newl;
{ else
int coeff; end->next=newl;
int pow; end=newl;
poly *next; }
}; poly *p1=poly1,*p2=poly2; //Addition Logic
class add2poly end=NULL;
{ while(p1 !=NULL && p2!=NULL)
poly *poly1, *poly2, *poly3; {
public: if(p1->pow == p2->pow)
add2poly() {
{ newl=new poly;
poly1=poly2=poly3=NULL; newl->pow=p--;
} newl->coeff=p1->coeff + p2->coeff;
void addpoly(); newl->next=NULL;
void display(); if(poly3==NULL)
}; poly3=newl;
void add2poly :: addpoly() else
{ end->next=newl;
int i,p; end=newl;
poly *newl=NULL,*end=NULL; }
cout<<"Enter highest power for x\n"; p1=p1->next;
cin>>p; p2=p2->next;
cout<<"\nFirst Polynomial\n"; //Read first poly }
for(i=p;i>=0;i--) }
{ void add2poly :: display()
newl=new poly; {
newl->pow=p; poly *t=poly3;
cout<<"Enter Co-efficient for degree"<<i<<":: "; cout<<"\n\nAnswer after addition is : ";
cin>>newl->coeff; while(t!=NULL)
newl->next=NULL; {
if(poly1==NULL) cout.setf(ios::showpos);
poly1=newl; cout<<t->coeff;
else cout.unsetf(ios::showpos);
end->next=newl; cout<<"X"<<t->pow;
end=newl; t=t->next;
} }
cout<<"\n\nSecond Polynomial\n"; //Read Second poly }
end=NULL; void main()
for(i=p;i>=0;i--) {
{ add2poly obj;
newl=new poly; obj.addpoly();
newl->pow=p; obj.display();
cout<<"Enter Co-efficient for degree"<<i<<":: "; }
Output:
Enter highest power for x
3 Second Polynomial
First Polynomial Enter Co-efficient for degree3:: 3
Enter Co-efficient for degree3:: 4 Enter Co-efficient for degree2:: 2
Enter Co-efficient for degree2:: 5 Enter Co-efficient for degree1:: 1
Enter Co-efficient for degree1:: 1 Enter Co-efficient for degree0:: 3
Enter Co-efficient for degree0:: 7
Answer after addition is : +7X3+7X2+2X1+10X0

STACK
Stack is a type of linear data structure in which insertion and deletion of an element takes place at one end called top.  The last
entry which is inserted is the first one that will be removed.
It’s basically works on the principle of LIFO – Last In First Out. Or FILO – First In Last Out.
STACK MODEL

11
Primitive operations on the stack.
 To create a stack
 Push operation - Inserting an element to the top of the stack. Before performing push we should check whether the stack is
full.
 Pop operation - Deleting an element from the top of the stack. Before popping the elements from stack we should check
whether stack is empty.
EXCEPTIONAL CONDITIONS
Overflow: Attempt to insert an element when the stack is full. During overflow element can’t be inserted.
Underflow:Attempt to delete an element when the stack is empty. During underflow element can’t be deleted.
Implementation of Stack in two ways
1. Array implementation of Stack
2. Linked List (Pointers) implementation of Stack
1. Array implementation of Stack
To create a stack
 A stack is a sequential representation (ordered list).
 Use one dimensional array, We need to define an array of some maximum size.
int stk[5];
 We need an integer variable top which will keep track of the top of the stack.
int top;

Since there is no element in the stack, top will be -1.


Since index of array always with zero, here no element so, top=-1.
stack top=-1
Operations on Stack
1. Push operation
 Push operation, which inserts new elements at the top of the stack.
 We need to check the stack is full or not called overflow condition, if the stack is not full then only the insertion of
void push(int x) the element can be achieved by means of push operation
{
if (top>=4) here total array index is 4 (from 0 to 4). If greater that it display stack overflow.
cout<<stack overflow”;
top++; if not, stack pointer i.e.) top will be incremented by 1 and the corresponding
stk[top]=x; value will be assigned to the stack
}
EXAMPLE - PUSH OPERATION

1) initially stack is empty.

stack is empty top= -1


2) for pushing element 10 onto the stack

top 10 0
insert element 10 onto the stack

3) for pushing element 20


4
top 0
increment top value now top=0
3

12
top 1 20 1

4 top 10 0

3 insert element 20 onto the stack


2
10 0
increment top value now top=1 increment top value now top=2
4) for pushing element 30

top 30 2
top 2
20 1
20 1
10 0
10 0
insert element 30 onto the stack
5) for pushing element 40

top 40 3
top 3
30 2
30 2
20 1
20 1
10 0
10 0
increment top value now top=3 insert element 40 onto the stack
6) for pushing element 50
top 4 top 50 4

40 3
40 3
30 2
30 2
20 1
20 1
10 0
10 0
increment top value now top=4 Insert element 50 onto the stack
7) Now when pushing any element onto the stack, stack is overflow condition, during overflow element can’t be inserted.
2. POP OPERATION
 Pop operation, which deletes the element at the top of the stack.
 We need to check the stack is empty or not called underflow condition, if the stack is not empty, then only the element can be
achieved by means of pop operation
EXAMPLE - POP OPERATION
1) For pop an element 50 4
top 50 4

top 40 3
40 3
30 2
30 2
20 1
20 1
10 0
10 0
Decrement top value now top=3
Delete an element 50 on top of the stack

2) For pop an element 40 4


13
top 40 3
4
30 2

20 1 3
top
10 0 30 2

20 1

Delete an element 40 on top of the stack 10 0


Decrement top value now top=2
3) For pop an element 30
4 4

3
3
2
top 30 2
top 20 1
20 1
10 0
10 0
Decrement top value now top=1
Delete an element 30 on top of the stack
4) For pop an element 20
4
4
3
3
2
2
top 1
top 20 1
top 10 0
10 0
Decrement top value now top=1
Delete an element 20 on top of the stack
5) For pop an element 10 4

4 3

3 2
top=-1
2 1

1
0
top 10 0
Decrement top value now top=-1
Delete an element 10 on top of the stack
6) Now when popping element from the stack, stack is underflow condition. During underflow element can’t be deleted.
void pop()
{
if(top <0)
cout <<"stack under flow";
cout <<"deleted: " <<stk[top];
top--;
}
Write A C++ Program For Array Implementatio Of A Stack - ******REFER LAB EXERCISE******

2. Linked List Implementation of Stack


 A Linked list allocates memory for storing elements and connects elements together using pointers.
 Here memory is allocated only when an element is to be pushed and this memory can be freed when we pop the element.
14
 As the basic operations (push and pop operations) of the stack are done at the top end.

Figure - A Linked List Implementation of Stack ADT

Declaration of a node
struct node
{
int data;
node *next;
}*top=NULL;
PUSH OPERATION
The push operation inserts a node at the beginning of the list, so that top pointer points to the first node of the list. The bottom most
node’s next (pointer) value will be NULL to denote the end of the first.
It is performed by inserting an element at the front of the list.
void push(int x)
{
if(top==NULL)
{
top=new node;
top -> data=item;
top -> next=NULL;
}
else
{
ptr=top;
top=new node;
top->data=item;
top->next=ptr;
}
}
EXAMPLE PUSH OPERATION
1. initially top=NULL
2. for pushing element 10 onto the stack
a) when top=NULL, create a new node called top,
top (1000)

b) copy the data into the new node, i


top (1000)
c) 10

c) if new node is the first node to insert, the address should be NULL
top (1000)
10 NULL

3. for pushing element 20 onto the stack


a) when top !=NULL,
top (1000) top is not equal to null , because top holds the address 1000.
10 NULL

b) assign the top pointer to ptr,


ptr(1000) top (1000) now ptr and top holds the address 1000
10 NULL
c) create a new node called top,
top (1500) top holds the address 500 ptr(1000)
20 10 NULL
d) copy the data (20) into the new node
top (1500) ptr(1000)

15
20 10 NULL

e) the address of the ptr is assign to top points to next.


top (1500) ptr(1000)
20 1000 10 NULL

4. for pushing element 30 onto the stack


a) when top !=NULL,
top (1500) top is not equal to null , because top holds the address 1500.
20 1000 10 NULL

b) assign the top pointer to ptr,


ptr(1500) top (1500) now both ptr and top holds the address 1500
20 1000 10 NULL

c) create a new node called top,


top (2000) ptr (1500)
20 1000 10 NULL
d) copy the data (30) into the new node,
top (2000) ptr (1500)
30 20 1000 10 NULL
e) the address of the ptr is assign to top points to next.
top (2000) ptr (1500)
30 1500 20 1000 10 NULL
Pop Operation
It is performed by deleting an element at the front of the list.
To delete

QUEUE
 A queue is a linear data structre, in which, insertion can take place only at one end called rear and deletion takes place at one
end called front.
 It is also known as First In First Out (“FIFO”)
 Eg. i) Line of people waiting at a ATM machine. ii) People purchase tickets from cinema make a queue.

Operation on queue
1) Create Queue
By this operation the structure of the queue is generated.
2) Enqueue
The insertion of element in the queue is called enqueue.
3) Dequeue
The deletion of element from the queue is called dequeue.
4) Overflow Condition
When no more elements can be inserted in the queue then this condition is called overflow condition.
5) Underflow Condition
When queue is empty and no more elements can be deleted from the queue then this condition is called underflow
condition.

Queue diagram
Queue implementation in two ways:
1) Array implementation of queue
2) Linked list implementation of queue.

1. Array implementation of queue


queue( )
{
rear=-1;
16
front=-1;
}

ENQUEUE OPERATION
Void enqueue
{
if(rear>4)
{
cout<<queue is overflow
front=-1;
rear=-1;
}
else
{
rear=rear+1;
que[rear]=x;
}
}

Dequeue
void dequeue()
{
if(front= =rear)
{
cout<<”queue underflow”;
}
else
{
front=front+1;
x=que[front];
}

Display
void display( )
{
if(front= =rear)
{
cout<<”queue is empty”;
return
}
for(int i=front+1;i<=rear;i++)
cout<<que[i];
}

front =-1

Write a C++ program for array implementation of a queue - see lab exercise

2. Linked list implementation of queue.

Enqueue operation

void enqueue( int x)


{
if(rear= =NULL)
{
newnode->data =x;
newnode->next=NULL;
front=newnode;
rear=newnode;
}
else

17
{
newnode->data=x;
newnode->next=NULL;
rear->next=newnode;
rear=newnode;
}
}

Dequeue operation
void dequeue( )
{
front=temp;
if(front= =NULL)
{
cout<<”queue is empty”;
}
else
{
front=front->next;
delete temp;
}
}

18

You might also like