10 Marks Unit-1-1
10 Marks Unit-1-1
1) Big oh notation
2) Omega notation
3) Theta notation
4) Little oh notation
5) Little omega notation
Big oh notation:
Big Oh notation is used to define the upper bound of an algorithm in terms of Time Complexity.
That means Big - Oh notation always indicates the maximum time required by an algorithm for
all input values. That means Big - Oh notation describes the worst case of an algorithm time
complexity.
The function f(n)=O(g(n)), if and only if, there exist positive constants c and no such that f(n) ≤
c * g(n) , for all n, n≥n0.
Example
1
That means Big - Omega notation always indicates the minimum time required by an algorithm
for all input values. That means Big - Omega notation describes the best case of an algorithm
time complexity.
The function f(n)=Ω(g(n)), if and only if, there exists positive constants c and no such that
That means Big - Theta notation always indicates the average time required by an algorithm for
all input values. That means Big - Theta notation describes the average case of an algorithm time
complexity.
The function f(n)=θ(g(n)), if and only if, there exists positive constants c1,c2 and no such that
C1*g(n)≤ f(n)≤c2 *g(n) , for all n, n≥n0.
Gaphical representation
2
Example:
Little oh notation
The function f(n)=o(g(n)), if and only if, there exists positive constants c and no such that f(n) ‹
c * g(n) , for all n, n≥n0.
(or)
the function f(n)=o(g(n)) if and only if
lim f(n)/g(n)= θ
n--->∞
3
2. Explain multidimensional array with an example?
Ans) Multidimensional arrays:
The above example is a three dimensional array. Here, compiler allocates memory as in
terms of tables.
Let m1, m2, - - - , mn are the sizes, then a multidimensional array can be defined as –
“Multidimensional array is a collection of m1 x m2 x - - - - x mn homogeneous data elements
that are stored in m1 x m2 x - - - - x mn successive memory locations”.
Direct Initialization:
1) The general form of initialization of a multidimensional array is:
Syntax: datatype ArrayName[size1][size2]- - - -[sizen] = {List of Values};
2) List of values can also be initialized in the form of a table representation as:
Example: int k[2][3][2] = { { {1,2}, {4,5},{6,7} }, { {8,9},{10,11},{12,13} } };
main()
{
int m, n, p, i, j, k, x[10][10][10];
clrscr();
printf("\nEnter how many Tables:");
4
scanf("%d", &m);
printf("\nEnter how many rows:");
scanf("%d", &n);
printf("\nEnter how many columns:");
scanf("%d", &p);
printf("\nEnter Array Elements:");
for(i=0;i<m; i++)
{
for(j=0;j<n; j++)
{
for(k=0;k<p; k++)
scanf("%d", &x[i][j][k]);
}
}
printf("\n Array Elements Are:");
for(i=0;i<m; i++)
{
for(j=0; j<n; j++)
{
printf("\n");
for(k=0; k<p; k++)
printf("%5d",x[i][j][k]);
}
}
}
1. Insertion: In a double linked list, the insertion operation can be performed in three ways.
They are as follows...
5
Inserting At Beginning of the list
Step1: Create a new node with given value
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode-->data = value;
newNode-->previous=NULL;
newNode-->next=NULL;
Example:
6
Step 4: If the list is not empty then
struct Node *temp=head;
while(temp->next != NULL)
Repeat
temp=temp->next;
end Repeat
temp->next=newNode;
newNode->previous=temp;
Example:
7
Example:
2. Deletion: In a double linked list, the deletion operation can be performed in three
ways. They are as follows...
8
Deleting from the end of the list
STEP 1: Check whether list is empty or not (head==NULL)
9
4. What is linked list? Explain different types of linked list with neat diagrams?
Ans) Definition: Linked list is a linear data structure that contains sequence of elements such
that each element links to its next element in the sequence. Each element in a linked list is called
as "Node".
Example:
10
Example:
Circular single linked list: Circular single linked list is a sequence of elements in which
every element has link to its next element in the sequence and the last element has a link to
the first element in the sequence.
11
5. a) Write a program to implement stack operations using linked list?
Ans) #include<stdio.h>
#include<conio.h>
struct Node
{
int data;
struct Node *next;
}*top = NULL;
void push(int);
void pop();
void display();
void main()
{
int choice, value;
clrscr();
printf("\n:: Stack using Linked List ::\n");
while(1){
printf("\n****** MENU ******\n");
printf("1. Push\n2. Pop\n3. Display\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d",&choice);
switch(choice){
case 1: printf("Enter the value to be insert: ");
scanf("%d", &value);
push(value);
break;
case 2: pop(); break;
case 3: display(); break;
case 4: exit(0);
default: printf("\n Wrong selection!!! Please try again!!!\n");
}
}
}
12
void push(int value)
{
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
if(top == NULL)
{
newNode->next = NULL;
top = newNode;
}
else
{
newNode->next = top;
top = newNode;
}
printf("\n Insertion is Success!!!\n");
}
void pop()
{
if(top == NULL)
printf("\n Stack is Empty!!!\n");
else{
struct Node *temp = top;
printf("\n Deleted element: %d", temp->data);
top = temp->next;
free(temp);
}
}
void display()
{
if(top == NULL)
printf("\n Stack is Empty!!!\n");
else{
struct Node *temp = top;
while(temp->next != NULL){
printf("%d--->",temp->data);
temp = temp -> next;
}
printf("%d--->NULL",temp->data);
}
}
13
b) Design an algorithm to count no.of nodes of a single linked list?
Step1: Check whether list is empty or not (head == NULL)
6. a) Explain the process of inserting an element at various positions of single linked list?
Ans) 1. Insertion: In a single linked list, the insertion operation can be performed in
three ways. They are as follows...
Inserting At Beginning of the list
Step1: Create a new node with given value
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
14
Inserting At End of the list
Step1: Create a new node with given value
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode-> link = null;
15
b) List out the advantages and disadvantages of lined list?
Ans)
Advantages of Linked List-
1. It requires more space as pointers are also stored along with information.
2. Different amount of time is required to access each element.
3. If we have to go to a particular element then we have to go through all those elements that
come before that element.
4. It is not easy to sort the elements stored in the linked list.
16