You are on page 1of 47

DATA STRUCTURES: INTRODUCTION

For data processing applications, large amount of data needs to be operated upon.
Typical operations on these data from data processing viewpoint are:
1. Insert a new value/ record
2. Delete and existing value/ record
3. Search for a value/ record
4. Sort all values/ records
5. Traverse all values/ records
We need some ways to organize these data so that all above operations can be performed
efficiently. This organisation of data is studied in Data Structures.
For example: consider arrays having sorted values in ascending order.
Insert operation: Algorithm:
1. Search for the required location.
2. Pull remaining values down by one place to make room for new element.
3. Place the value there.
Seems good. Consider population of India stored in an array. To insert a new element 130
crore values to be searched and remaining values to be pulled down to create space for new
element. Complexity O(N). Agreed. Also to pull down remaining values, code as follows
required: (no. of elements=n; new element inserted at position: x)
for(i=n-1; i>=x; i--) a[i+1]=a[i];
DATA STRUCTURES: INTRODUCTION
Delete operation: Same as Insert operation except now the remaining values are to be
pushed up to fill the space created by deleted element. Complexity O(N). Code:
for(i=x;i<n;i++) a[i]=a[i+1];
Search Operation: Three algorithms:
1. Linear Search: Compare with each value starting from beginning. Worst case it is at the
last position. Best case at the beginning. So
Best case complexity: Omega(1);
Worst case complexity: O(N);
Average case complexity: Theta(N/2) or Theta(N).
2. Binary Search: It will be discussed after this part.
3. Hashing: It will discussed later.
Sorting: To sort the given data in an array, we will study number of algorithms.
Non-recursive algorithms: Selection sort, Insertion sort, Bubble sort
Recursive algorithms: Quick sort, Merge sort, Heap sort.
Traversing: Scan each element one by one starting from the first.
Complexity: O(N), Theta(N), Omega(N).
CAN WE DO BETTER THAN THIS? YES. (THAT IS THE
PURPOSE OF THIS COURSE).
BASIC OPERATIONS ON ARRAYS:
#include<stdio.h>
#define N 20
void insertarray(int *a,int x,int *n)
{
int i;
i=*n;
while(i>0 && a[i-1]>x)
{
a[i]=a[i-1];
i--;
}
a[i]=x;
(*n)++;
}
void deletearray(int *a,int x,int *n)
{
int i=0,j;
while(i<*n && a[i]!=x)
i++;
if (i<*n)
{
for(j=i;j<*n-1;j++)
a[j]=a[j+1];
(*n)--;
}
}
BASIC OPERATIONS ON ARRAYS:
int linearsearch(int *a,int x,int n)
{
int i=0;
while(i<n && a[i]!=x)
i++;
if (i==n)
return -1;
else
return i;
}
void selectionsort(int *a,int n)
{
printf("will be implemented later\n");
}
void traversearray(int *a,int n)
{
int i;
for(i=0;i<n;i++)
printf("%d\n",a[i]);
}
BASIC OPERATIONS ON ARRAYS:
int main()
{
int p[N]={1,3,5,7,9};
int no=5,choice,v;
do{
printf("1. Enter a value\n");
printf("2. Delete a value\n");
printf("3. Search a value\n");
printf("4. Sort values\n");
printf("5. traverse and print\n");
printf("6. EXIT\n");
printf("Enter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Give value to enter: ");
scanf("%d",&v);
if (no<N)
insertarray(p,v,&no);
else
printf("array full, cannot enter\n");
break;
case 2:
printf("give value to delete: ");
scanf("%d",&v);
deletearray(p,v,&no);
break;
BASIC OPERATIONS ON ARRAYS:
case 3:
printf("give value to search: ");
scanf("%d",&v);
printf("value found at location: %d\n",linearsearch(p,v,no));
break;
case 4:
selectionsort(p,no);
printf("Sorting will be implemented later\n");
break;
case 5:
traversearray(p,no);
break;
case 6:
printf("Thank you, BYE\n");
break;
default:
printf("invalid choice, enter again\n");
}
} while(choice!=6);
}
BASIC OPERATIONS ON ARRAYS:
Linear Search:
1. For unsorted array:
int linearsearch(int *a,int x,int n) // n is the number of elements in the array
{ // index will range from 0 to n-1
int i=0;
while(i<n && a[i]!=x)
i++;
if (i==n)
return -1;
else
return i;
}
2. Sorted array: Can we improve it for Sorted array (increasing order):
int linearsearch(int *a,int x,int n)
{
int i=0;
while(i<n && x>a[i])
i++;
if (x==a[i]))
return i;
else
return -1;
}
BASIC OPERATIONS ON ARRAYS:
Linear Search: (A more general approach: starting index s and ending index e is given)
1. For unsorted array:
int linearsearch(int *a,int x,int s,int e) // number of elements in array = e-s+1
{ // s: starting index; e: ending index
int i=s;
while(i<=e && a[i]!=x)
i++;
if (i==e+1)
return -1;
else
return i;
}
2. Sorted array: Can we improve it for Sorted array (increasing order):
int linearsearch(int *a,int x,int s,int e)
{
int i=s;
while(i<=e && x>a[i])
i++;
if (x==a[i]))
return i;
else
return -1;
}
BASIC OPERATIONS ON ARRAYS:
Recursive Linear Search:(For unsorted array):
int rlinearsearch(int *a,int x,int s,int e)
{
If (a[s]==x)
return s;
else if (s>e)
return -1;
else
return rlinearsearch(a,x,s+1,e);
}

Recursive Linear Search:(For sorted array: ascending order):


int rlinearsearch(int *a,int x,int s,int e)
{
If (a[s]==x)
return s;
else if (a[s]>x)
return -1;
else
return rlinearsearch(a,x,s+1,e);
}
BASIC OPERATIONS ON ARRAYS:
Binary Search:
1. It works only on sorted arrays.
2. It is assumed that array is sorted in ascending order.
3. Concept:
BASIC OPERATIONS ON ARRAYS:
Binary Search:
1. It works only on sorted arrays.
2. It is assumed that array is sorted in ascending order.
int binarysearch(int *a,int x,int s,int e)
{
do{
m=(s+e)/2;
If (a[m]>x)
e=m-1;
If (a[m]<x)
s=m+1;
} while(s<=e && a[m]!=x);
If (s>e)
return -1;
else
return m;
}
BASIC OPERATIONS ON ARRAYS:
Recursive Binary Search:
1. It works only on sorted arrays.
2. It is assumed that array is sorted in ascending order.
int rbinarysearch(int *a,int x,int s,int e)
{
int m;
m=(s+e)/2;
If (s>e)
return -1;
else if(a[m]==x)
return m;
else
(
If (a[m]>x)
return rbinarysearch(a,x,s,m-1);
else
return rbinarysearch(a,x,m+1,e);
}
}
BASIC OPERATIONS ON ARRAYS:
Hashing:
1. Role of Check digits in numbers: Simple Example:
A number 24156. Add check digit in the end as (sum of digits)%10 i.e., 18%10=8
So the number is now 241568. Now it can check one digit typing errors.
2. Basic idea of error detection, error detection and correction codes
a. Two types of errors:
i. Single bit error
To transmit 0 : send 00
To transmit 1 : send 11
Now if there is only one bit in error, it can be detected. (Hamming Distance :h=2). But it cannot be
corrected.
To transmit 0 : send 000
To transmit 1 : send 111
Now it can detect one and two bit errors and can correct one bit errors. (Hamming Distance : h=3)
ii. Burst errors : More than one bit in error are covered under this topic. Ex. CRC Codes.
No further discussion on this now.
3. Hash Functions
a. MOD function
b. Mid-Square
c. Others
4. How these can be used to store values
BASIC OPERATIONS ON ARRAYS:
HASH FUNCTIONS:
REFERENCES FROM WIKIPEDIA:
A hash function is any function that can be used to map data of arbitrary size to fixed-size values.
The values returned by a hash function are called hash values, hash codes, digests, or simply
hashes. The values are used to index a fixed-size table called a hash table. Use of a hash function
to index a hash table is called hashing or scatter storage addressing.
Hash functions and their associated hash tables are used in data storage and retrieval applications
to access data in a small and nearly constant time per retrieval, and storage space only fractionally
greater than the total space required for the data or records themselves. Hashing is a
computationally and storage space efficient form of data access which avoids the non-linear
access time of ordered and unordered lists and structured trees, and the often exponential storage
requirements of direct access of state spaces of large or variable-length keys.
Hash functions are related to (and often confused with) checksums, check digits, fingerprints, lossy
compression, randomization functions, error-correcting codes, and ciphers. Although the concepts
overlap to some extent, each one has its own uses and requirements and is designed and
optimized differently.
BASIC OPERATIONS ON ARRAYS:
Selection Sort:
Concept: Number of passes: N-1, In each pass one number is selected and placed at
correct place.
void selectionsort(int *a,int n) //for void selectionsort(int *a,int s,int e)
{ // replace 0 with s
int i,j,s,t; // replace n-1 with e
for(i=0;i<n-1;i++) // and n with e+1 or <n with <=e
{
s=i;
for(j=i+1;j<n;j++)
if (a[j]<a[s])
s=j;
//exchange a[i] and a[s]
t=a[i];
a[i]=a[s];
a[s]=t;
}
}
Complexity Analysis:
BASIC OPERATIONS ON ARRAYS:
Insertion Sort:
Concept: It also requires N-1 passes. In each pass, one number is inserted in a
already sorted list of numbers.
void insertionsort(int *a,int n) //for void insertionsort(int *a,int s,int e)
{
int i,j,s;
for(i=1;i<n;i++) //n-1 passes
{
j=i-1;
s=a[i];
while(j>=0 && s<a[j])
{
a[j+1]=a[j];
j--;
}
a[j+1]=s;
}
Complexity Analysis:
BASIC OPERATIONS ON ARRAYS:
Bubble Sort:
Concept: Number of passes: 1 to N-1. In each pass N-i comparisons, where i is the
pass number. Introduction of Concept of flag.
void bubblesort(int *a,int n) //for void bubblesort(int *a,int s,int e)
{ // flag=0 :Not sorted;
int i,j,t,flag; // flag=1: sorted
i=1;
do{
flag=1;
for(j=0;j<n-i;j++)
{
If (a[j]>a{j+1])
{
//swap them and make flag=0
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
flag=0;
}
}
I++;
}while(flag!=1);
}
Complexity Analysis:
COMPLEXITY CONSIDERATIONS OF ARRAY OPERATIONS:
ASYMPTOTIC BEST(OMEGA) WORST(O) AVERAGE(THETA)
-------------------------------------------------------------------------------------------------------------------------
INSERT N N N

DELETE N N N

SEARCH:
LINEAR 1 N N
BINARY 1 log2N log2N
HASHING 1 N N

SORT (NON-RECURSIVE)
SELECTION N2 N2 N2
INSERTION N N2 N2
BUBBLE N N2 N2

TRAVERSE N N N
WHAT NEXT?
IMPLEMENTATION OF FOLLOWING DATA STRUCTURES USING ARRAYS:
(STATIC MEMORY ALLOCATION):
1. STACK AND ITS APPLICATIONS (POLISH NOTATIONS)
2. QUEUE - LINEAR AND CIRCULAR
3. LINKED LIST

IMPLEMENTATION OF FOLLOWING DATA STRUCTURES USING DYNAMIC


MEMORY ALLOCATION:
1. STACK
2. QUEUE AND ITS VARIATIONS
3. LINKED LISTS AND THEIR VARIATIONS
4. TREES - BINARY SEARCH TREES (BST)

RECURSIVE SORTING ALGORITHMS:


QUICK, MERGE AND HEAP SORT (USING ARRAYS)

GRAPHS AND RELATED ALGORITHMS (USING ARRAYS)


STACK:
1. A LIFO DATA STRUCTURE (Last-In-First-Out)
As against Queue which is FIFO (First-In-First-Out)
2. What is FILO: First-In-Last-Out
& What is LILO: Last-In-Last-Out Guess?
3. Terminology from Stack of Plates of Dictionary
4. Used in number of algorithms which require LIFO operations.
5. Operations defined on stack:
- Initialize the stack
- Push
- Pop
- Emptystack
- Fullstack - if applicable
- Peek (some implementations allow it).
6. Role of Stack-Top (also referred to as Top)
7. Implementation using Arrays
#include<stdio.h> int emptystack(int t)
#define N 20 {
void initstack(int *t) If (t==-1)
{ return 1;
*t=-1; else
} return 0;
void push(int *a,int x,int *t) }
{ int fullstack(int t)
*t=*t+1; {
a[*t]=x; If (t==N-1)
} return 1;
void pop(int *a,int *x,int *t) else
{ return 0;
*x=a[*t]; }
*t=*t-1; int main()
} {
int peek(int *a,int t) Int sta[N],topa;
{ initstack(&topa);
return a[t]; MENU DRIVEN SECTION;
} }
POLISH NOTATION:

1. Given by Polish mathematician Jan Lukasiewicz in the 1920's


2. The Prefix notation is known as Polish Notation (PN or NPN).
3. The Postfix notation is called Reverse Polish Notation (RPN).
4. Reference from Wikipedia:
[1]
Polish notation (PN), also known as normal Polish notation (NPN), Łukasiewicz notation, Warsaw
notation, Polish prefix notation or simply prefix notation, is a mathematical notation in which operators
precede their operands, in contrast to the more common infix notation, in which operators are placed
between operands, as well as reverse Polish notation (RPN), in which operators follow their operands. It
does not need any parentheses as long as each operator has a fixed number of operands. The
[2]
description "Polish" refers to the nationality of logician Jan Łukasiewicz, who invented Polish notation in
[3][4]
1924.
INFIX TO POSTFIX CONVERSION:
Assumptions:
1. All variables are of single character from a-z,A-Z.
2. No values are used in the expression directly. Only variables as above.
3. Expression is syntactically correct. No unary operators are used.
4. Priority of an operator is shown as priority(operator).
ALGORITHM:
1. Scan the input string from left to right. Only one pass is sufficient.
2. If the next symbol is a operand (a-z or A-Z), directly print it.
3. If the next symbol is ‘(‘, push it on the stack.
4. If the next symbol is a ‘)’, pop all symbols from the stack and print them, till you get a
‘(‘. Further discard this pair of brackets.
5. If the next symbol is an operator (^,/,*,+,-,=):
- If the stack is empty or symbol at stack-top is a ‘(‘, push this operator on the stack;
- Else as long as the stack is not empty and symbol at the top of the stack is not a ‘(‘
and the priority of the operator on the stack top is >= the priority of the new operator,
pop operators from stack and print them;
- Now push the new operator on stack.
6. After the pass is over, still some operators will be on stack. Pop them and print them
till the stack is empty.
INFIX TO PREFIX CONVERSION:
Assumptions:
1. All assumptions as in previous algorithm.
2. Two stacks A and B are used. Stack A for operators as in previous algorithm. Stack B
for final expression to be printed in forward direction.
ALGORITHM:
1. Scan the input string from RIGHT TO LEFT. Only one pass is sufficient.
2. If the next symbol is a operand (a-z or A-Z), PUSH THEM ON STACK B.
3. If the next symbol is ‘)‘, push it on the stack A.
4. If the next symbol is a ‘(‘, pop all symbols from the stack A and print them, till you get a
‘)‘. Further discard this pair of brackets.
5. If the next symbol is an operator (^,/,*,+,-,=):
- If the stack A is empty or symbol at stack-top(A) is a ‘)‘, push this operator on the stack
A;
- Else as long as the stack A is not empty and symbol at the top of the stack A is not a ‘)‘
and the priority of the operator on the stack is > the priority of the new operator, pop
operators from stack A and push them on stack B;
- Now push the new operator on stack A.
6. After the pass is over, still some operators will be on stack A. Pop them and push them
on stack B till the stack A is empty.
7. Pop elements from stack B and print them.
PROGRAM FOR INFIX TO POSTFIX: int emptystack(int t)
#include<stdio.h> {
#define N 80 if (t==-1)
void initstack(int *t) return 1;
{ else
*t=-1; return 0;
} }
void push(char *a,char x,int *t) int fullstack(int t)
{ {
*t=*t+1; if (t==N-1)
a[*t]=x; return 1;
} else
void pop(char *a,char *x,int *t) return 0;
{ }
*x=a[*t]; int prio(char x)
*t=*t-1; {
} if (x=='=') return 0;
char peek(char *a,int t) if (x=='+' || x=='-') return 1;
{ if (x=='*' || x=='/') return 2;
return a[t]; if (x=='^') return 3;
} }
//INFIX TO POSTFIX CONVERSION //INFIX TO PREFIX CONVERSION
int main() int main()
{ {
char s[N],a[N]; char s[N],a[N],b[N];
int topa,i,j,k; int topa,topb,i,j,k;
char c; char c;
initstack(&topa); initstack(&topa);
printf("give the input expression: "); initstack(&topb);
scanf("%s",s); printf("give the input expression: ");
i=0; scanf("%s",s);
while(s[i]!='\0') i=strlen(s);
{ while(i>=0)
if (s[i]>='a' && s[i]<='z') {
printf("%c",s[i]); if (s[i]>='a' && s[i]<='z')
else if (s[i]=='(') push(b,s[i],&topb);
push(a,s[i],&topa); else if (s[i]==')')
else if (s[i]==')') push(a,s[i],&topa);
{ else if (s[i]=='(')
pop(a,&c,&topa); {
while(c!='(') pop(a,&c,&topa);
{ while(c!=')')
printf("%c",c); {
pop(a,&c,&topa); push(b,c,&topb);
} pop(a,&c,&topa);
} }
else }
else
//INFIX TO POSTFIX CONVERSION //INFIX TO PREFIX CONVERSION

{ {
while(!emptystack(topa) && while(!emptystack(topa) &&
peek(a,topa)!='(' && prio(peek(a,topa))>=prio(s[i])) peek(a,topa)!=')' && prio(peek(a,topa))>prio(s[i]))
{ pop(a,&c,&topa); { pop(a,&c,&topa);
printf("%c",c); push(b,c,&topb);
} }
push(a,s[i],&topa); push(a,s[i],&topa);
} }
i++; i--;
} }
while (!emptystack(topa)) while (!emptystack(topa))
{ {
pop(a,&c,&topa); pop(a,&c,&topa);
printf("%c",c); push(b,c,&topb);
} }
printf("\n"); while(!emptystack(topb))
} {
pop(b,&c,&topb);
printf("%c",c);
}
printf("\n");
}
QUEUES:
1. A FIFO structure (First-In-First-Out)
2. A person who has come first, will get served first.
3. Represented by a pipe open from both ends.
4. Has a ‘FRONT’ and a ‘REAR’ pointer
5. Service is given from FRONT.
6. New elements are added at the REAR.
7. FRONT points to the first filled location.
8. REAR points to the next to last filled position.
9. As in normal queue, elements move forward whenever an element is served.
10. Problem with above implementation ?
11. Algorithm for entering an element in the queue:
ENTERQ:
- Insert element at REAR Position. Make REAR=REAR+1
12. Algorithm for deleting an element from queue:
DELETEQ:
Remove the element from FRONT position. Make FRONT=FRONT+1
13. Problem with this implementation ?
14. How to solve this problem ?
QUEUE - IMPLEMENTATION 1: (SIZE OF ARRAY IS N)
1. LINEAR Q, FRONT ALWAYS AT INDEX 0; ELEMENTS MOVE FORWARD WHENEVER SERVICE IS GIVEN.
2. FRONT IS AT FIRST FILLED LOCATION(always at index 0) REAR IS AT NEXT TO LAST FILLED LOCATION.
3. INITIALLY FRONT=REAR=0. (INITQ). EMPTYQ IS WHEN REAR=0, FULLQ IS WHEN REAR=N
4. FUNCTIONS FOR THIS IMPLEMENTATION:
#include<stdio.h>
#define N 5 int emptyq(int r)
void initq(int *f,int *r) {
{ if (r==0)
*f=0; return 1;
*r=0;
}
else
void enterq(int *a,int x,int *r) return 0;
{ }
a[*r]=x; int fullq(int r)
*r=*r+1; {
} if (r==N)
void deleteq(int *a,int *x,int *r)
return 1;
{
int i; else
*x=a[0]; return 0;
for(i=0;i<*r;i++) }
a[i]=a[i+1];
*r=*r-1;
}
int main()
{
int a[N];
int front,rear;
int choice,v,i;
initq(&front,&rear);
do{
printf("1. Enter a value in queue\n");
printf("2. Delete a value from queue\n");
printf("3. Print queue values\n");
printf("4. EXIT\n");
printf("Enter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Give value to enter: ");
scanf("%d",&v);
if (fullq(rear)==0)
enterq(a,v,&rear);
else
printf("queue full, cannot enter\n");
break;
case 2:
if (emptyq(rear)==0)
{
deleteq(a,&v,&rear);
printf("delete queue element :%d\n",v);
}
else
printf("queue empty, cannot delete\n");
break;
case 3:
printf("queue values are : ");
for(i=0;i<rear;i++)
printf("%d. %d,",i,a[i]);
printf("\n");
printf("current front= %d, current rear= %d\n",front,rear);
break;
case 4:
printf("Thank you, BYE\n");
break;
default:
printf("invalid choice, enter again\n");
}
} while(choice!=4);
}
QUEUE - IMPLEMENTATION 2: (SIZE OF ARRAY IS N)
1. LINEAR Q, FRONT ALSO MOVES FORWARD WHEN SERVICE IS GIVEN. ELEMENTS DON’T MOVE.
2. FRONT IS AT FIRST FILLED LOCATION, REAR IS AT NEXT TO LAST FILLED LOCATION.
3. INITIALLY FRONT=REAR=0. (INITQ). EMPTYQ IS WHEN FRONT==REAR, FULLQ IS WHEN REAR=N
4. FUNCTIONS FOR THIS IMPLEMENTATION:

void deleteq(int *a,int *x,int *f) CHANGES IN main():


{ case 2:
if (emptyq(front,rear)==0)
*x=a[*f];
{
*f=*f+1; deleteq(a,&v,&front);
} printf("delete queue element :%d\n",v);
int emptyq(int f,int r) }
{ else
if (f==r) printf("queue empty, cannot delete\n");
return 1; break;
case 3:
else printf("queue values are : ");
return 0; for(i=front;i<rear;i++)
} printf("%d. %d,",i,a[i]);
printf("\n");
printf("current front= %d, current rear= %d\n",front,rear);
break;
QUEUE - IMPLEMENTATION 3: (SIZE OF ARRAY IS N) (BEST IMPLEMENTATION)
1. CIRCULAR Q, FRONT ALSO MOVES FORWARD WHEN SERVICE IS GIVEN. ELEMENTS DON’T MOVE.
2. FRONT IS AT FIRST FILLED LOCATION, REAR IS AT NEXT TO LAST FILLED LOCATION.
3. INITIALLY FRONT=REAR=0. (INITQ). EMPTYQ IS WHEN FRONT==REAR.
4. FULLQ IS WHEN (REAR+1)%N == FRONT
5. FUNCTIONS FOR THIS IMPLEMENTATION:
void enterq(int *a,int x,int *r)
CHANGES IN main():
{
case 3:
a[*r]=x;
printf("queue values are : ");
*r=(*r+1)%N;
i=front;
}
while(i!=rear)
void deleteq(int *a,int *x,int *f)
{
{
printf("%d. %d,",i,a[i]);
*x=a[*f];
i=(i+1)%N;
*f=(*f+1)%N;
}
}
printf("\n");
int fullq(int f,int r)
printf("current front= %d, current rear= %d\n",front,rear);
{
break;
if ((r+1)%N==f)
return 1;
else
return 0;
}
QUEUE - IMPLEMENTATION 4: (SIZE OF ARRAY IS N)
1. CIRCULAR Q, FRONT ALSO MOVES FORWARD WHEN SERVICE IS GIVEN. ELEMENTS DON’T MOVE.
2. FRONT IS AT FIRST FILLED LOCATION, REAR IS AT LAST FILLED LOCATION.
3. INITIALLY FRONT=0,REAR=N-1. (INITQ). EMPTYQ IS WHEN (REAR+1)%N=FRONT
4. FULLQ IS WHEN (REAR+2)%N=FRONT
5. FUNCTIONS FOR THIS IMPLEMENTATION:
#include<stdio.h>
int emptyq(int f,int r)
#define N 5
{
void initq(int *f,int *r)
if ((r+1)%N==f)
{
return 1;
*f=0;
else
*r=N-1;
return 0;
}
}
void enterq(int *a,int x,int *r)
int fullq(int f,int r)
{
{
*r=(*r+1)%N;
if ((r+2)%N==f)
a[*r]=x;
return 1;
}
else
void deleteq(int *a,int *x,int *f)
return 0;
{
}
*x=a[*f];
*f=(*f+1)%N;
}
LINKED LIST (USING ARRAYS - STATIC MEMORY ALLOCATION):
Declaration of a node:
Algorithm for Enter an Element:
#include<stdio.h> 1. Get a node from the avail, if available.
#define N 10 2. Store the new value in info of it.
struct node{ 3. Search for it correct location in existing list.
int info; 4. Insert it there by manipulating the next part of the previous
int next; node and next part of the current node.
};
Algorithm for Delete an Element:
typedef struct node node1; 1. Search for the element in the info part of nodes of the list.
int main() 2. If found, take it out by manipulating the next part of the
{ previous node.
node1 a[N]; 3. Attach this node to the existing avail at the beginning.
int avail,list;
initlist(a,&list,&avail); Algorithm for Search an Element:
1. Start from list and compare each item till search value is
found.
MENU DRIVEN SECTION FOR 2. If found, return 1, else if end of list is reached or a value
1. ENTER A ELEMENT greater than search value is obtained, return 0.
2. DELETE AN ELEMENT
3. SEARCH AN ELEMENT Algorithm for Traverse all Elements:
4. TRAVERSE ALL ELEMENTS 1. Start from list and print values (info) of each node.
5. EXIT 2. Use the next part of the node to move to the next node.
3. Do the above till last node is done.
LINKED LIST (USING ARRAYS - STATIC MEMORY ALLOCATION):
#include<stdio.h>
#define N 10
struct node{
int info;
int next;
};
typedef struct node node1;

void initlist(node1 *a,int *list,int *avail)


{
int i;
*list=-1;
*avail=0;
for(i=0;i<N-1;i++)
a[i].next=i+1;
a[N-1].next=-1;

}
LINKED LIST (USING ARRAYS - STATIC MEMORY ALLOCATION):
int emptylist(int list)
{
if (list==-1)
return 1;
else
return 0;
}
int fulllist(int avail)
{
if (avail==-1)
return 1;
else
return 0;
}
LINKED LIST (USING ARRAYS - STATIC MEMORY ALLOCATION):
int searchlist(node1 *a,int list,int x)
{
while(list!=-1 && a[list].info<x)
list=a[list].next;
if (a[list].info==x)
return 1;
else
return 0;
}
void traverselist(node1 *a,int list)
{
while(list!=-1)
{
printf("%d. %d--> ",list,a[list].info);
list=a[list].next;
}
printf("\n");
}
LINKED LIST (USING ARRAYS - STATIC MEMORY ALLOCATION):
void enterlist(node1 *a,int *list,int *avail,int x)
{
int l,back,q;
l=*avail;
*avail=a[*avail].next;
a[l].info=x;
if (*list==-1 || x<a[*list].info)
{
a[l].next=*list;
*list=l;
}
else
{
back=*list;
q=*list;
while(q!=-1 && a[q].info<x)
{
back=q;
q=a[q].next;
}
a[back].next=l;
a[l].next=q;
}
}
LINKED LIST (USING ARRAYS - STATIC MEMORY ALLOCATION):
void deletelist(node1 *a,int *list,int *avail,int x)
{
int l,back;
l=*list;
while (l!=-1 && a[l].info<x)
{
back=l;
l=a[l].next;
}
if (l!=-1 && a[l].info==x)
{
if (l==*list)
{
*list=a[l].next;
}
else
{
a[back].next=a[l].next;
}
a[l].next=*avail;
*avail=l;
}
else
printf("value not present in the list\n");
}
LINKED LIST (USING ARRAYS - STATIC MEMORY ALLOCATION):
int main() case 2:
{ printf("Give value to delete: ");
node1 a[N]; scanf("%d",&v);
int list,avail; if (emptylist(list)==0)
int choice,v,i; deletelist(a,&list,&avail,v);
initlist(a,&list,&avail); else
do{ printf("list empty, cannot delete\n");
printf("1. Enter a value in list\n"); break;
printf("2. Delete a value from list\n"); case 3:
printf("3. Search in list\n"); printf("Give value to search: ");
printf("4. Traverse the list\n"); scanf("%d",&v);
printf("5. EXIT\n"); if (searchlist(a,list,v)==1)
printf("Enter your choice:"); printf("Value found in list\n");
scanf("%d",&choice); else
switch(choice) printf("value not found in list\n");
{ break;
case 1: case 4:
printf("Give value to enter: "); printf("The Avail(%d)-->",avail);
scanf("%d",&v); traverselist(a,avail);
if (fulllist(avail)==0) printf("\n");
enterlist(a,&list,&avail,v); printf("The List(%d)-->",list);
else traverselist(a,list);
printf("list full, cannot enter\n"); printf("\n");
break; break;
LINKED LIST (USING ARRAYS - STATIC MEMORY ALLOCATION):
case 5:
printf("Thank you, BYE\n");
break;
default:
printf("invalid choice, enter again\n");
}
} while(choice!=5);
}
FORMAT FOR MID-TERM EXAMINATION:

1. TOTAL 7 QUESTIONS. MAX. MARKS: 50


2. TIME 1 HOUR AND 30 MINUTES+ 20 MINUTES
FORM UPLOADING.
3. 3 QUESTIONS DESCRIPTIVE TYPE
4. 4 QUESTIONS ON C PROGRAMMING OF
DATA STRUCTURES
5. 10 MARKS FOR QUIZZES DONE SO FAR
a. 5 MARKS FOR QUIZ 1-10 BEST FIVE TO BE
TAKEN INTO ACCOUNT
b. 5 MARKS FOR QUIZ 11-18 BEST FIVE TO BE
TAKEN INTO ACCOUNT
6. 5 MARKS FOR CLASS NOTES COPY FOR
LECTURE 1-18
TWO-DIMENSION ARRAYS:
int main()
#include<stdio.h>
#define N 5
{
void readarray(int a[][N],int r,int c) int a[N][N];
{ Int p[20]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
int i,j; int i,choice,v;
for(i=0;i<r;i++) do{
for(j=0;j<c;j++) printf("1. Enter values of array\n");
{ printf("2. Print the array\n");
printf("Give a[%d][%d]:",i,j); printf("3. Convert array\n");
scanf("%d",&a[i][j]); printf("4. Print one dimension array using pointers\n");
} printf("5. EXIT\n");
} printf("Enter your choice:");
void printarray(int a[][N],int r,int c) scanf("%d",&choice);
{ switch(choice)
int i,j; {
for(i=0;i<r;i++) case 1:
{ printf("Give value to enter: \n");
for(j=0;j<c;j++) readarray(a,2,3);
printf("%4d ",a[i][j]); break;
printf("\n");
}
}
TWO-DIMENSION ARRAYS: OUTPUT:
1. Enter values of array
case 3: 2. Print the array
printf("The converted array is : 3. Convert array
\n"); 4. Print one dimension array using pointers
printarray(p,2,3); 5. EXIT
break; Enter your choice:3
case 4: The converted array is :
printf("The array values using 1 2 3
pointer notation are :\n"); 6 7 8
for (i=0;i<10;i++) 1. Enter values of array
printf("%d %d\n",p[i],*(p+i)); 2. Print the array
break; 3. Convert array
case 5: 4. Print one dimension array using pointers
printf("Thank you, BYE\n"); 5. EXIT
break; Enter your choice:4
default: The array values using pointer notation are :
printf("invalid choice, enter 1 1
again\n"); 2 2
} 3 3
} while(choice!=5); ……...
} 9 9
10 10

You might also like