0% found this document useful (0 votes)
5 views

Module 2

Uploaded by

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

Module 2

Uploaded by

ruksarp
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

DATA STRUCTURES AND APPLICATIONS BCS304

Module 2
Syllabus
QUEUES: Queues, Circular Queues, Using Dynamic Arrays, Multiple Stacks and queues.
LINKED LISTS : Singly Linked, Lists and Chains, Representing Chains in C, Linked Stacks
and Queues, Polynomials

Suggested Learning Resources:

Textbook: 1. Ellis Horowitz, Sartaj Sahni and Susan Anderson-Freed, Fundamentals of Data
Structures in C, 2nd Ed, Universities Press, 2014

Reference Books:

1. Seymour Lipschutz, Data Structures Schaum's Outlines, Revised 1 st Ed, McGraw Hill, 2014.

2. Gilberg & Forouzan, Data Structures: A Pseudo-code approach with C, 2 nd Ed, Cengage
Learning,2014.

3. Reema Thareja, Data Structures using C, 3 rd Ed, Oxford press, 2012.

4. Jean-Paul Tremblay & Paul G. Sorenson, An Introduction to Data Structures with


Applications, 2nd Ed, McGraw Hill, 2013

5. A M Tenenbaum, Data Structures using C, PHI, 1989

6. Robert Kruse, Data Structures and Program Design in C, 2 nd Ed, PHI, 1996.

Text Book: Chapter-3: 3.3, 3.4, 3.7 Chapter-4: 4.1 to 4.4

DEPARTMENT OF CSE(AIML),RIT HASSAN


DATA STRUCTURES AND APPLICATIONS BCS304

Queues
Queue: A Queue is an ordered list in which insertions (push) and deletions (pop) are made
at different ends. The elements are added at one end is called rear end. The elements are
deleted or removed from another end is called front end.
If the elements are inserted A,B,C,D and E in this order, then A is the first element deleted
from the queue. Since the first element inserted into a queue is the first element removed,
queues are known as Fisrt-In-First-Out(FIFO) lists.

QUEUE REPRESENTATION USING ARRAY


 Queues are represented by one-way lists or linear arrays.
 Queues will be maintained by a linear array QUEUE and two pointer variables:
FRONT-containing the location of the front element of the queue and REAR-
containing the location of the rear element of the queue.
 The condition FRONT=NULL will indicate the queue is empty.

DEPARTMENT OF CSE(AIML),RIT HASSAN


DATA STRUCTURES AND APPLICATIONS BCS304

QUEUE OPERATIONS:

DEPARTMENT OF CSE(AIML),RIT HASSAN


DATA STRUCTURES AND APPLICATIONS BCS304

DEPARTMENT OF CSE(AIML),RIT HASSAN


DATA STRUCTURES AND APPLICATIONS BCS304

DEPARTMENT OF CSE(AIML),RIT HASSAN


DATA STRUCTURES AND APPLICATIONS BCS304

DEPARTMENT OF CSE(AIML),RIT HASSAN


DATA STRUCTURES AND APPLICATIONS BCS304

DEPARTMENT OF CSE(AIML),RIT HASSAN


DATA STRUCTURES AND APPLICATIONS BCS304

DEPARTMENT OF CSE(AIML),RIT HASSAN


DATA STRUCTURES AND APPLICATIONS BCS304

DEPARTMENT OF CSE(AIML),RIT HASSAN


DATA STRUCTURES AND APPLICATIONS BCS304

DEPARTMENT OF CSE(AIML),RIT HASSAN


DATA STRUCTURES AND APPLICATIONS BCS304

DEPARTMENT OF CSE(AIML),RIT HASSAN


DATA STRUCTURES AND APPLICATIONS BCS304

Singly Linked, Lists and Chains:

The following are the advantages of using a linked list over an array:
 Dynamic data structure: The size of the linked list is not fixed as it can vary according
to our requirements. Array is fixed in size we cannot increase or decrease the size of the
array.
 Insertion and Deletion: Insertion and deletion in linked list are easier than array as the
elements in an array are stored in a consecutive location. In contrast, in the case of a linked
list, the elements are stored in a random location. Insertion and deletion of element in to
linear array requires more shifting of elements. Shifting of elements become expensive.

Data is organized alphabetically in linear array as shown above. Suppose an element GAT
has to be inserted into above mentioned linear array we need to shift position number 4 to
13 elements like insertion deletion of an element LAT in to linear array needs more
number shiftings from 13th location elements to 7th location elements towards 6th location.

Singly Linked List:


Definition: A linked list can also be defined as the collection of the nodes in which one node is
connected to another node, and node consists of two parts, i.e., one is the data part and the
second one is the address part, as shown in the below figure:

DEPARTMENT OF CSE(AIML),RIT HASSAN


DATA STRUCTURES AND APPLICATIONS BCS304

In the above figure, we can observe that each node contains the data and the address of the next
node. The last node of the linked list contains the NULL value in the address part.
REPRESENTING CHAIN IN C

The following capabilities are needed to make linked representation

1. A mechanism for defining a node’s structure, that is, the field it contains. So
self-referential structures can be used
2. A way to create new nodes, so MALLOC functions can do this operation 3.
A way to remove nodes that no longer needed.
3. The FREE function handles this operation.
Defining a node structure
typedef struct listNode *listPointer
typedef struct {
char data[4];
listPointer list;
} listNode;
Create a New Empty list
listPointer first = NULL

To create a New Node


MALLOC (first, sizeof(*first));
To place the data into NODE
strcpy(first→ data,”BAT”);
first→ link = NULL

DEPARTMENT OF CSE(AIML),RIT HASSAN


DATA STRUCTURES AND APPLICATIONS BCS304

Two node linked List: Write a c function to create two nodes


listPointer create2(){
listPointer first,second;
MALLOC(first,sizeof(*first));
MALLOC(second,sizeof(*second));
second->data=20;
second->link=NULL;
first->data=10;
first->link=second;
return first;
}

Write a c function to insert into front of list


listPointer insert(listPointer *first,listPointer x){
listPointer temp;
MALLOC(temp,sizeof(*temp));
MALLOC(second,sizeof(*second));
temp ->data=10;
if(*first){
temp link = x link;
x link = temp;
}else{
Temp link = NULL;
*first = temp;
}
}
Write a c function to delete from a list

DEPARTMENT OF CSE(AIML),RIT HASSAN


DATA STRUCTURES AND APPLICATIONS BCS304

void delete(listPointer *first, listPointer trail, listPointer x){


if(trail){
trail link = x link;
}else{
(*first) = (*first) link;
}
free(x);
}
Write a c function to print a list
void printList(listPointer first){
printf(“The list contains”);
for(; first ; first = first link){
printf(“%d”,first data);
}
printf(“\n”);
}
LINKED STACKS AND QUEUES
The below figure shows stacks and queues using linked list. Nodes can easily add or delete
a node from the top of the stack. Nodes can easily add a node to the rear of the queue and
add or delete a node at the front

DEPARTMENT OF CSE(AIML),RIT HASSAN


DATA STRUCTURES AND APPLICATIONS BCS304

Linked Stack
The representation of n ≤ MAX_STACKS stacks, below is the declarations:

#define MAX_STACKS 10
/* maximum number of stacks */
typedef struct {
int key; /* other fields */
}element;
typedef struct stack *stackPointer;
typedef struct {
element data;
stackPointer link;
} stack;
stackPointer top[MAX_STACKS];

The initial condition for the stacks is: top[i] = NULL, 0 ≤ i < MAX_STACKS

The boundary condition is: top [i] = NULL iff the ith stack is empty

Functions push and pop add and delete items to/from a stack.

void push(int i, element item) {


/* add item to the ith stack */
stackPointer temp;
MALLOC(temp, sizeof(*temp));
temp→data = item;
temp→link = top[i];
top[i] = temp;
}

Program: Add to a linked stack

Function push creates a new node, temp, and places item in the data field and top in the link
field. The variable top is then changed to point to temp. A typical function call to add an
element to the ith stack would be push (i,item).

DEPARTMENT OF CSE(AIML),RIT HASSAN


DATA STRUCTURES AND APPLICATIONS BCS304

element pop(int i) {
/* remove top element from the ith stack */
stackPointer temp = top[i];
element item;
if (! temp)
return stackEmpty();
item = temp→data;
top[i] = temp→link;
free (temp) ;
return item;
}

Program: Delete from a linked stack

Function pop returns the top element and changes top to point to the address contained in
its link field. The removed node is then returned to system memory. A typical function call
to delete an element from the ith stack would be item = pop (i);

Linked Queue
The representation of m ≤ MAX_QUEUES queues, below is the declarations:

#define MAX-QUEUES 10
/* maximum number of queues */ typedef.struct
queue *queuePointer;
typedef struct {
element data;
queuePointer link;
} queue;
queuePointer front[MAX_QUEUES],rear[MAX_QUEUES];

The initial condition for the queues is: front[i] = NULL, 0 ≤ i < MAX_QUEUES
The boundary condition is: front[i] = NULL iff the ith queue is empty
Functions addq() and delete() implement the add and delete operations for multiple queues.

DEPARTMENT OF CSE(AIML),RIT HASSAN


DATA STRUCTURES AND APPLICATIONS BCS304

void addq(i, item)


{
/* add item to the rear of queue i */
queuePointer temp;
MALLOC(temp,sizeof(*temp));
temp→data = item;
temp→link = NULL;
if (front[i])
rear[i] →link = temp;
else
front[i] =temp;
rear[i] = temp;
}

Program: Add to the rear of a linked queue


Function addq is more complex than push because we must check for an empty queue. If the
queue is empty, then change front to point to the new node; otherwise change rear's link field
to point to the new node. In either case, we then change rear to point to the new node.

element deleteq(int i)
{
/* delete an element from queue i */
queuePointer temp = front[i];
element item;
if (! temp)
return queueEmpty();
item = temp→data;
front[i]= temp→link;
free (temp) ;
return item;
}

Program: Delete from the front of a linked queue

Function deleteq is similar to pop since nodes are removing that is currently at the start of
the list. Typical function calls would be addq (i, item); and item = deleteq (i);

DEPARTMENT OF CSE(AIML),RIT HASSAN


DATA STRUCTURES AND APPLICATIONS BCS304

Polynomials
Polynomial Representation: The structure declaration of Polynomial to represent in Singly
Linked List is as follows:
typedef struct polyNode *polypointer;
typedef struct{
int coef;
int expon;
polyPointer link;
}polyNode;
polyPointer a,b;

a = 3x14 + 2x8 + 1
b = 8x14 - 3x18 + 10x6

Representation of polynomial
a = 3x14 + 2x8 + 1

Representation of polynomial
b = 8x14 - 3x18 + 10x6

Resultant Polynomial (adding two polynomials representation in Singly Linked list

DEPARTMENT OF CSE(AIML),RIT HASSAN


DATA STRUCTURES AND APPLICATIONS BCS304

Write a C function to add two polynomials in Singly Linked List

polyPointer padd( polyPointer a, ployPointer b){


polyPointer c,rear,temp;
int sum;
MALLOC(rear,sizeof(*rear));
c=rear;
while( a && b){
switch(COMPARE(a →expon, b→expon){
case -1: //a →expon < b→expon
attach(b →coef , b→expon,&rear);
b = b→link;
break;
case 0: //a →expon = = b→expon
sum= a →coef + b →coef;
if(sum)
attach(sum, a→expon,&rear);
a = a → link;
b = b → link;
break;
case 1: //a →expon > b→expon
attach(a →coef, a→expon,&rear);
a = a → link;
}
}
/* copy rest of list a and then list b */
for(; a ; = a → link)
attach(a →coef, a→expon,&rear);
for(; b ; = b → link)
attach(b →coef, b→expon,&rear);
rear → link = NULL;
temp=c;
c=c → link;
free(temp);
return c;
}

DEPARTMENT OF CSE(AIML),RIT HASSAN


DATA STRUCTURES AND APPLICATIONS BCS304

attach a node to the end of a list


void attach(float coefficient,int exponent,polyPointer *ptr){
polyPointer temp;
MALLOC(temp,sizeof(*temp));
temp → coeff=coefficient;
temp → expon=exponent;
(*ptr) → link=temp;
*ptr=temp;
}

Write a C function to erase a Polynomial from Singly Linked List:

void earse(polyPointer *ptr){


polyPointer temp;
while(*ptr){
temp=*ptr;
*ptr=(*ptr) → link;
free(temp);
}
}

Write a C function to getNode:


polyPointer getNode(){
polyPointer node;
if(avail){
node=avail;
avail = avail → link;
}else{
MALLOC(node,sizeof(*node));
}
return node;

DEPARTMENT OF CSE(AIML),RIT HASSAN


DATA STRUCTURES AND APPLICATIONS BCS304

Write a C function to retNode:

void retNode(polyPointer node){


node → link = avial;
avail = node;
}

Circular Singly Linked List:

In a circular Singly linked list, the last node of the list contains a pointer to the first node of the
list. We can have circular singly linked list as well as circular doubly linked list.

We traverse a circular singly linked list until we reach the same node where we started. The
circular singly liked list has no beginning and no ending. There is no null value present in
the next part of any of the nodes.
The following image shows a circular singly linked list.

Additional List Operatons


 Inverting a Singly linked list
 Concatenating singly linked list

Inverting a Singly linked list

typedef struct listNode *listPointer;


typedef struct{

DEPARTMENT OF CSE(AIML),RIT HASSAN


DATA STRUCTURES AND APPLICATIONS BCS304

char data;
listPointer link;
}listNode;

Write a C Function to invert a singly Linked List

listPointer invert(listPointer lead){


/*invert the list pointed by the lead*/
listPointer middle,trail;
middle=NULL:
while(lead){
trail=middle;
middle=lead;
lead=lead->link;
middle->link=trail;
}
return middle;
}

Write a c function to concatenate two singly linked lists

listPointer concatenate(listPointer ptr1, listPointer ptr2){

listPointer temp;
if(!ptr1) return ptr2;
if(!ptr2) return ptr1;

for( temp=ptr1;temp->link;temp=temp->link);
temp->link=ptr2;
}
Write a c function to Finding the length of the circular list
int length(listPointer last){
listPointer temp;
int count=0;
if(last){
temp=last;
do{

DEPARTMENT OF CSE(AIML),RIT HASSAN


DATA STRUCTURES AND APPLICATIONS BCS304

count++;
temp=temp->link;
}
while(temp!=last);
return count;
}
}

write a C function for inserting at front of Circular Singly Linked list


void insertFront(listPointer *last,listPointer node(){
if(!(*last)){
*last=node;

node ->link=node;

}else{

node ->link= (*last) ->link;


(*last) ->link=node;
}

DEPARTMENT OF CSE(AIML),RIT HASSAN

You might also like