Professional Documents
Culture Documents
Unit 3 Notes Oct 2014
Unit 3 Notes Oct 2014
Abstract Data Types (ADTs) – List ADT – array-based implementation – linked list implementation –– singly linked lists –
Polynomial Manipulation - Stack ADT – Queue ADT - Evaluating arithmetic expressions
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
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
10 20 40 50 60
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
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
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
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
2500
Step 3: The next field value of the node at position, 2 is assigned the address of new node.
1000 Head
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
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
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
P 2000
7
P 1000 1000 p1
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 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.
10 2000 20 NULL
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;
top 10 0
insert element 10 onto the stack
12
top 1 20 1
4 top 10 0
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
20 1 3
top
10 0 30 2
20 1
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******
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)
c) if new node is the first node to insert, the address should be NULL
top (1000)
10 NULL
15
20 10 NULL
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.
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
Enqueue operation
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