Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

c Programming and Data Structures - Unit III Notes - Copy

Unit III covers linear data structures including Abstract Data Types (ADTs) like List, Stack, and Queue, with implementations in arrays and linked lists. It discusses various operations such as insertion, deletion, and traversal for both array-based and linked list implementations, highlighting their advantages and disadvantages. Additionally, it outlines applications of data structures in fields like compiler design, operating systems, and artificial intelligence.

Uploaded by

Kezial Elizabeth
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

c Programming and Data Structures - Unit III Notes - Copy

Unit III covers linear data structures including Abstract Data Types (ADTs) like List, Stack, and Queue, with implementations in arrays and linked lists. It discusses various operations such as insertion, deletion, and traversal for both array-based and linked list implementations, highlighting their advantages and disadvantages. Additionally, it outlines applications of data structures in fields like compiler design, operating systems, and artificial intelligence.

Uploaded by

Kezial Elizabeth
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 32

UNIT III

LINEAR DATA STRUCTURES (8+1 SKILL) 9


Abstract Data Types (ADTs) – List ADT – Array-Based Implementation –
Linked List – Doubly- Linked Lists – Circular Linked List – Stack ADT –
Implementation of Stack – Applications – Queue ADT – Priority Queues – Queue
Implementation – Applications.

Data Structure
 A data structure is a storage that is used to store and organize data.
 It is a way of arranging data on a computer so that it can be accessed and
updated efficiently.
Classification of Data Structure:

Linear data structure: Data structure in which data elements are arranged
sequentially
 Static data structure: Static data structure has a fixed memory size.
Example : Array
 Dynamic data structure: In dynamic data structure, the size is not
fixed. It can be randomly updated during the runtime. Example :
stack,queue
Non Linear data structure: Data structure in which data elements are
arranged in hierarchical order. Example: Trees and Graph
Application of Data structures
Data structures are widely applied in the following areas:
 Compiler design
 Operating system
 Statistical analysis package
 DBMS
 Numerical analysis
 Simulation
 Artificial intelligence
 Graphic

ADT ( Abstract Data Type)


 Abstract Data type (ADT) is a type for objects whose behavior is
defined by a set of values and a set of operations.
 The ADT defines what operations are to be performed but not how
these operations will be implemented.
 It does not specify how data will be organized in memory and what
algorithms will be used for implementing the operations.
 Example: List , Stack, Queue.

List ADT :
 List is the group of elements and its general form is A1, A2, A3,…….AN
 The size of the list is N.
 The size of empty list is ‘0’.
 The element Ai+1 follows Ai and the element Ai-1 precedes Ai.
 The position of the element is i.

Operations on List
1.Insertion
2.deletion
3.findkth element
4.print list
Implementation
The List can be implemented in three ways
1.Array implementation
2.Linked list implementation
Array implementation
 An array is a collection of similar data elements.
 These data elements have the same data type.
 The elements of the array are stored in consecutive memory locations and are
referenced by an index (also known as the subscript).
Insertion Operation:
 Insertion operation inserts a new element to the list in the specified location.
 If there is an element at the specified location, then the elements from the
specified location to the end are moved forward one by one, starting from the
last element.
 This makes the empty place at the specified location.
 The new element is now inserted into the specified location.
 The size of the list is also increased into ‘n+1’ after the insertion of the
element.
Example:
List a, n=6
Index 0 1 2 3 4 5 6 7 8 9

Element 55 35 26 78 63 49 - - - -

Insert(18,3)

Index 0 1 2 3 4 5 6 7 8 9

Element 55 35 26 78 63 49 - - - -

a[6]=a[5]

Index 0 1 2 3 4 5 6 7 8 9

Element 55 35 26 78 63 - 49 - - -

a[5]=a[4]
Index 0 1 2 3 4 5 6 7 8 9

Element 55 35 26 78 - 63 49 - - -

a[4]=a[3]

Index 0 1 2 3 4 5 6 7 8 9

Element 55 35 26 - 78 63 49 - - -

a[3]=new

Index 0 1 2 3 4 5 6 7 8 9

Element 55 35 26 18 78 63 49 - - -

List a, n=7
Declaration:
int a[20];
void insert(int a[],int n,int pos);
int delete(int a[],int n, int pos)
void printlist(int n,int a[])
Algorithm to insert an element in to the List
void insert(int a[],int n,int x,int pos)

{
int i;
if(n==sizeof(a)
{
printf(“the list is full cant insert”);
return;
}
else
{
for(i=n-1;i>=pos;i--)
{
a[i+1]=a[i];
}
a[pos]=x;
n=n+1;
}
}
Deletion:

 Deletion operation deletes an element at the specified location from the


list.
 This makes empty position at the specified location.
 The elements in the list are moved backward to fill the empty position.
 The size of the list is decreased into ‘n-1’ after the deletion of the
element.
Example:
List a, n=6

Index 0 1 2 3 4 5 6 7 8 9

Element 55 35 26 78 63 49 - - - -

Delete(2)

Index 0 1 2 3 4 5 6 7 8 9

Element 55 35 - 78 63 49 - - - -

a[2]=a[3]

Index 0 1 2 3 4 5 6 7 8 9

Element 55 35 78 - 63 49 - - - -

a[3]=a[4]

Index 0 1 2 3 4 5 6 7 8 9

Element 55 35 78 63 - 49 - - -

a[4]=a[5]

Index 0 1 2 3 4 5 6 7 8 9

Element 55 35 78 63 49 - - - -

List a, n=5
Algorithm to delete an element from the List
int delete(int a[],int n, int pos)
{
int i,d;
if(n==0)
{
printf(“list is empty and cannot delete”);
return;
}
else
{
d=a[pos];
for(i=pos;;i<=n-1;i++)
{
a[i]=a[i+1];
}
n=n-1;
return d;
}

Print List:
It is used to print the elements of the list one by one starting from the element
at index ‘0’ to index ‘n-1’.

Algorithm to display the elements of the List


void printlist(int n,int a[])
{
int i;
if(n==0)
{
Printf(“List is empty”);
}
Else
{
for(i=0;i<n;i++)
{
printf(“\n%d”,a[i]);
}
}
}
Advantages of Array Implementation of the List
• Accessing the element from the list is easy.

Disadvantages of Array Implementation of the List


 Size of the list cannot be increased or decreased dynamically.
 Insertion and deletion is not easy because of lot of data movement.

Linked List Implementation


 Linked List is a linear data structure.
 Linked list elements are not stored at a contiguous location.
 The elements are linked using pointers.
 The linked list is a collection of nodes.
 Each node stores the data and the address of the next node.

Types of Linked Lists:


1.Singly Linked List
2.Doubly Linked List
3.Circular Linked List
1.Singly Linked List
 It is a collection of ordered set of elements.
 Each element is represented by the node.
 Each node contains data and one pointer.
 The pointer contains the address of the next node.
 Traverse the linked list in only one direction.

NULL

Node definition:
struct node
{
int data;
struct node *next;
};
Declaration of Node:
struct node *head, *temp;
Allocating Space for Node:
temp = (struct node *)malloc(sizeof(struct node));
Operations on Singly Linked List:
1. Insertion
2. Deletion
3. Traversal
1. Insertion
The insertion into a singly linked list can be performed at different positions.
1. Insert at the beginning
2. Insert at the end
3. Insert after specified node
1. Insert at the beginning:
 Inserting any element at the front of the list
 The new node is the first node.
 Initially first element is the header node.
 After the insertion of new element, the new element becomes the first element in the
list
After Insertion
Insert 20 at the beginning of the list.

Algorithm to insert an element at the beginning of the List


struct node *insertbegin(struct node *head, int x)
{
struct node *newnode;
newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=x;
if(head==null)
{
newnode->next=null;
}
else
{
newnode->next=head;
}
head=newnode;
return head;
}
2. Insert at the end
 Insertion at the last of the linked list
 Traverse the list until we find the last node.
 Then we insert the new node to the end of the list.
After Insertion
Inserting 20 at the end

Method declaration:
struct node *insertatend(struct node *head, int x)
struct node * insertatposition(struct node *head, int x, int pos)
struct node * insertatposition(struct node *head, int x, int pos)
struct node *deleteatend(struct node *head);
struct node *deleteatbeg(struct node *head);
struct node *deleteatposition(struct node *head,int pos);
void display(struct node *head);
Algorithm to insert an element at the end of the List
struct node *insertatend(struct node *head, int x)
{
struct node *newnode,*temp;
newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=x;
temp=head;
if(head==null)
{
newnode->next=null;
head=newnode;
}
else
{
while(temp->next!=null)
{
temp=temp->next;
}
temp->next=newnode;
newnode->next=null;
}
return head;
}
3. Insert after specified node
 Insert the element after the specified node of the linked list.
 Traverse the list to find the specified node.
After Insertion:
Insert 20 after 10
Algorithm to insert an element at the specified location of the List
struct node * insertatposition(struct node *head, int x, int pos)
{
struct node *newnode,*temp;
newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=x;
temp=head;
if(head==null)
{
newnode->next=null;
head=newnode;
}
else
{
while(temp->next!=null)
{
if(temp->data==pos)
{
newnode->next=temp->next;
temp->next=newnode;
}
temp=temp->next;
}
}
return head;
}
2. Deletion

The Deletion of a node from a singly linked list is performed at different positions.

1. Deletion at beginning
2. Deletion at the end
3. Deletion after specified node
1. Deletion at beginning
 This operation deletes the first element of the list.
 The first node is head node.
 After the deletion of the first node, the new node is the head node.
Algorithm to delete an element at beginning of the List
strtuct node *deletefirst(struct node *head)
{
struct node *temp;
temp=head;
if(head==null)
{
printf(“the list is empty”);
}
else
{
head=temp->next;
free(temp);
}
return head;
}
Before deletion
head

Delete the first element 10

After Deletion
head

2. Deletion at the end


 This operation deletes the last node from the list.
 The last node is identified by traversing the list.
 After the deletion of the last node, the address of the previous node of
last node is set as NULL.

Algorithm to delete an element at the end of the List


Struct node *deleteatend(struct node *head)
{
struct node *temp,*prev;
temp=head;
if(head==NULL)
{
printf(“The list is empty”);
}
Else
{
while(temp->next!=null)
{
prev=temp;
temp=temp->next;
}
prev->next=null;
free(temp);
}
return head;
}

Before Deletion
head

Delete the last element 30.


After Deletion

Head

3. Deletion after specified node


 Deleting the node after the specified node in the list
 Traverse the list to find the node.
 After the deletion of the specified node the previous node is joined with the next
node of specified node.
Before Deletion

Head

Delete 20
After Deletion

Head

Algorithm to delete an element from a specified location


struct node *deleteatposition(struct node *head, int pos)
{
struct node *newnode,*temp;
temp=head;
if(head==null)
{
printf(“the list is empty”);
}
else
{
while(temp->next!=null)
{
prev=temp;
if(temp->data==pos)
{
prev->next=temp->next;
free(temp)
return;
}
temp=temp->next;
}
}
return head;
}
Algorithm to display the List
void display(struct node *head);
{
stuct node * temp;
temp=head;
if(head==NULL)
{
Printf(“The List is Empty”);
}
else
{
while(temp->next!=NULL)
{
printf(“\n%d”,temp->data);
temp=temp->next;
}
printf(“\n%d”,temp->data);
}
}
Advantages of Singly Linked List
 Insertion and deletion operations are very easy.
 The size of the singly linked list increase or decrease at run time so there is no
memory wastage.
Disadvantages of Singly Linked List
 More memory is required in the singly linked list as compared to an array.
 Direct access to an element is not possible
 In a singly linked list reverse traversing is not possible.

Doubly Linked List


 It is a collection of ordered set of elements.
 Each element is represented by the node.
 Each node contains data and two pointers.
 One pointer is a previous pointer which contains the address of the previous node.
 Another pointer is the next pointer which contains the address of the next node.
 Traverse the linked list in both directions (Forward and Backward).

struct node
{
struct node *prev;
int data;
struct node *next;
};

Circular Linked List


 It is a collection of ordered set of elements.
 Each element is represented by the node.
 Each node contains data and one pointer.
 The last node of the linked list contains the address of the first node.

Node definition:
struct node
{
int data;
struct node *next;
};

Application of List
 Polynomial operations (Addition, subtraction and Multiplication)
 Used in radix and bubble sorting
 Used to implement all linear and non-linear data structures.

Stack ADT:
 A stack is a linear data structure
 In stack the elements are added and removed only from one end, called as top.
 A stack is a LIFO (Last in first out) data structure
 The elements that was inserted last is the first one to be accessed.
 The operations performed are push and pop.
Implementation:
A stack can be implemented using I) Array and ii) Linked List
Array Representation of stacks:
 In the computer memory, stacks can be represented as a linear array.
 Every stack has a variable called TOP .
 TOP is the position where the elements are added or deleted.
 Another variable used is MAX, is the maximum number of elements
that the stack can store.

If TOP=0 then the stack is empty and if TOP=MAX-1 then the stack is full.
10 20 30 40 50 60 70
0 1 2 3 4 5 6 7 8 9
Here the value for Max = 10 , TOP = 6
Operations on a stack:
A stack has three basic operations: PUSH, POP ,and display the topmost
element.
PUSH Operation:
 The push operation is the process of inserting a new element to the top
of the stack.
 For every push operation the top is incremented by 1.

Top 20
Top 10 
 10

Empty stack After Inserting an element 10 After inserting an element 20


Top=NULL
POP Operation:
 The pop operation is the process of deleting an element from the top of stack.
 After every pop operation the top pointer is decremented by 1.

Top 30
 Top 20
20  Top 10
10 10 
Initial Stack After deleting 30 After the deletion of 20
Array Implementation of Stack :
Algorithm:
Declaration for array representation of stacks
#define max 10
Int st[max],top=-1;
void push(int st[],intval)
int pop(int st[])
int displaytop(intst[]);
void display(int st[])
int isempty()
int isfull()
Routine to check whether the stack is empty
int isempty()
{
if(top == -1)
return(1);
else
return(0);
}
Routine to check whether the stack is Full
int isfull()
{
if(top==max-1)
return(1);
else
return(0)
}
Routine to Push an element onto a stack
void push(int st[],int val)
{
if(isfull())
printf(“stack is full”)
else
{
top=top+1;
st[top]=val;
}
}
Routine to Pop an element from the stack
int pop(int st[])
{
if(isempty())
{
printf( “ stack is empty”);
}
else
{
x=st[top]
top=top--;
}
return(x)
}
Routine to return the top element of the stack
int displaytop(int st[])
{
if(isempty())
{
printf( “stack is empty”);
}
else
return st[top];
}
Routine to display the elements of the stack
void display(int st[])
{
if(isempty())
{
Printf( “stack is empty”);
}
else
{
for(i=0;i<=top;i++)
printf “%d”,(st[i]));
}
}
Linked Representation of stack:
Linked List:
 Linked List is a linear data structure.
 Linked list elements are not stored at a contiguous location.
 The elements are linked using pointers.
 The linked list is a collection of nodes.
 Each node stores the data and the address of the next node.

Implementation:

 Push operation is performed by inserting an element at the front of the list


 Pop operation is performed by deleting the element at the front of the list.
 Top operation returns the elements at the front of the list
 TOP is a pointer used to store the address of the topmost element of the stack.
Node definition:
struct node
{
int data;
struct node *next;
};

Declaration of Node:
struct node *top, *temp;

Allocating Space for Node:


top = (struct node *)malloc(sizeof(struct node));
Declaration of operations:
struct node *top=null
struct node *push(struct node *,int val);
struct node *pop(struct node *);
void displaytop(struct node*)
void display(struct node *)
Algorithm to push an element
struct node push(struct node *top)
{
struct node *newnode;
newnode=(struct node *)malloc(sizeof(struct node *);
newnode->data=val;
if(top==null)
{
top=newnode;
top->next=null;
}
else
{
newnode->next=top;
top=newnode;
}
return(top);
}
10 NULL
TOP
20 10 NULL
top
Algorithm to Pop the top most element
struct node *pop(struct node *top)
{
if(top==null)
error”stack is empty”
else
{
ptr =top;
top=ptr->next;
free(ptr);
}
return(top);
}
Algorithm to display the top element
void displaytop(struct node *top)
{
printf(“%d”, top->data)
}
Algorithm to print the elements of the stack
void display(struct node *top)
{
struct node *ptr;
if(top==null)
printf( “list is empty”);
else
{
ptr=top;
while(ptr->next!=null)
{
printf(“%d”,ptr->data);
ptr=ptr->next;
}
}
}
Applications of stack:
 Evaluating arithmetic expression
 Balancing the symbols
 Function Calls
 Infix to postfix and prefix conversion.
 Function Calls
 Reversing the string
 Towers of Hanoi
 8 Queens Problem

Algebraic expression:
There are three different ways to representing the algebraic expression
They are :
1) Infix notation
2) Postfix or reverse polish notation
3) Prefix or polish notation

Infix notation:
In Infix notation the operator is placed in between the operands
a+b
Postfix or reverse polish notation:
In the postfix notation, the operator is placed after the operands.
ab+
Prefix notation or polish notation:
In the postfix notation, the operator is placed before the operands.
+ab
Explain the need for infix and postfix expression
 It is easy to write the infix expression.
 But the computer find it difficult to parse because it may needs lot of
information to evaluate the expression.
 The computers work more efficiently with prefix and postfix notation.
 The computers first convert the infix expression into postfix notation and then
evaluate the postfix expression.

DISCUSS ANY TWO APPLICATION OF STACK WITH RELEVANT


EXAMPLES
1) Infix to postfix conversion
2) Postfix expression evaluation
3) Balancing the symbols

INFIX TO POSTFIX CONVERSION


ALGORITHM:
Read the infix expression one character at a time until it encounters the end of
symbol
1) If the character is an operand, place it on to the output.
2) If the character is an operator, compare it with the operator at the top of the stack.
o If the stack operator has a high or equal priority than the incoming
operator then
 Pop that operator from the stack and
 Push the incoming operator in to the stack.
3) If the character is a left parenthesis, push it onto the stack.
4) If the character is a right parenthesis then pop all the operators from the stack till it
encounters left parenthesis, discard parenthesis in the output.
5) Pop the remaining operators from the stack.
PRIORITY:

Operator Description Associativity


*/% Multiplication/division/modulus left-to-right
+- Addition/subtraction left-to-right

Example:
Show the simulation using stack for the following expression to convert infix to
postfix :
p*q+(r-s/t)
Consider the following infix expression
P * q + ( r – s / t)
Scanned Stack Output Description
Character
P p P is an operand, place it on to the output
* * *is an operator place it onto the stack
Q * Pq q is an operand, place it on to the output
+ + P q * Incoming operator : +
Operator at the top of the stack : *
Compare if( priority of ‘ * ‘ > priority of ‘ + ‘)
then pop * and push +
( +( If the character is a left parenthesis, push it onto
the stack
R +( P q *r r is an operand, place it on to the output
- +(- P q * r Compare if( priority of ‘(‘ > priority of ’ – ‘) –
false , so push -
S +(- P q * r s s is an operand, place it on to the output
/ +(-/ P q * r s Compare if priority of ‘ – ‘ > priority of ‘ / ‘,
false , so push /
T +(-/ P q * r s t t is an operand, place it on to the output
) + P q * r s t / - If the character is a right parenthesis then pop
all the operators from the stack till it encounters
left parenthesis
P q * r s t / - Pop the remaining operators from the stack
+

Postfix Expression:
Pq *r s t / - +

POSTFIX EXPRESSION EVALUATION


Read the postfix expression one character at a time until the end of symbol.
1) If the character is an operand , push the value onto the stack
2) If the character is an operator, pop two values from the stack, apply the
operator to them and push the result onto the stack.

Example
Show the simulation using stack for the following expression:
Infix Notation: 12 + 3 *14 –(5*16) + 7
postfix notation : 12 3 14 * + 5 16 * 7 + -
Scanned Stack Output Description
Character
12 12 If the character is an operand , push its
associated value onto the stack. Push ’12 ‘

3 12 3 If the character is an operand , push its


associated value onto the stack. Push ‘ 3 ‘
14 12 3 14 If the character is an operand , push its
associated value onto the stack. Push ‘ 14 ‘

* 12 42 If the character is an operator, pop two values


from the stack, apply the operator to them and
push the result onto the stack.

3 * 14 = 42
+ 54 12 + 42 = 54
5 54 5 If the character is an operand , push its
associated value onto the stack.Push ‘ 5 ‘
16 54 5 16 If the character is an operand , push its
associated value onto the stack.Push ‘ 16 ‘
* 54 80 5 * 16= 80
7 54 80 7 If the character is an operand , push its
associated value onto the stack.Push ‘ 7 ‘
+ 54 87 80 + 7= 87
- - 33 54 – 87 = -33

Queue ADT:
 A queue is a Linear data structure.
 A queue is a FIFO(First in First Out) Data structure
 The element that was inserted first is the first one to be taken out.
 The elements inserted at one end called the rear
 The elements removed from the other end called the front.
Front Rear
10 2 30 40 50 60 70
0 1 2 3 4 5 6 7 8 9
Here front = 0 and rear = 6
Operations on Queue:
1) Enqueue – The process of inserting an element in a queue
2) Dequeue – The process of deleting an element from a queue.

Implementation of Queues:
Queue can be implemented using 1) Array and
2) Linked List
Array Implementation:
Queues can be easily implemented using array. Every queue has front and rear
variables to point the position of insertion and deletion respectively.
The initial value of front = rear = -1

Enqueue :
 To insert an element into the queue,
 First the rear is incremented by 1 ,
 Then the value is stored at the position of
 rear .
 For the first insertion both front and rear will be incremented.
 Before inserting an element in the queue, check for”queue full condition” and
“queue empty “condition.

After the insertion of 10


Front Rear
10
0 1 2 3 4 5 6 7 8 9
Here front = 0 and rear = 0, for the first insertion both front and rear will be
incremented.
After inserting 20
Front Rear
10 20
0 1 2 3 4 5 6 7 8 9
Here front = 0 and rear = 1,now only the rear is incremented .
The queue after the insertion of 30,40,50,60,70 and 80
Front Rear
10 2 30 40 50 60 70 80
0 1 2 3 4 5 6 7 8 9
Now the value for front = 0 and rear = 7

Dequeue:
 To delete an element from the queue, the value for front is incremented.
 Before deleting an element, we must check for ”queue empty “ condition.
The queue after the deletion of 10
front Rear
2 30 40 50 60 70 80
0 1 2 3 4 5 6 7 8 9
Now the value for front=1 and rear= 7

Algorithm for Queue ADT implementation using ARRAY:


Declarations
#define max 10
int q[max],front=-1,rear=-1
void enqueue(void);
void deque(void);
void display(void);
Algorithm to insert an element in the queue:
void enqueue()
{
read num;
if(rear==max -1)
printf( “queue is full”)
else if(front==-1 && rear ==-1)
rear=front=0;
else
rear++
q[rear]=num;
}
Algorithm to delete an element from the queue:
void deque ()
{
if(front==-1 || front > rear)
printf( “queue empty”);
else
{
printf(“%d”, q[front]); // the deleted element
front = front +1;
}
}

Algorithm to Display the queue elements:


void display()
{
for(i=front;i<=rear;i++)
printf(“%d”,q[i]);
}

What is Circular Queue?

Circular Queue is a linear data structure in which the operations are performed based
on FIFO (First In First Out) principle and the last position is connected back to the
first position to make a circle.

What is Double Ended Queue (Dequeue)?


Double Ended Queue is also a Queue data structure in which the insertion and deletion
operations are performed at both the ends (front and rear).

You might also like