Data Stracture'S: Stacks: LIFO-Last in First Out
Data Stracture'S: Stacks: LIFO-Last in First Out
Data Stracture'S: Stacks: LIFO-Last in First Out
For eg: when one function called anther function and presses some parameters,
these parameters are passed using the stack.
A stack is called Last In First Out [LIFO] because the item that entered
the stack last is the first one to get out.
In these stack’s there are two operation’s is their
1. PUSH (write) operation
2. POP (read) operation
A stack is the best eg: is “LIBRARY”. We assume that in shyly the
stack is empty the bellow operation is shows who to read and write in a stack
OPERATION AFTER THE OPERATION
PUSH(A) A
PUSH(B) A,B
POP(B) A
PUSH(C) A,C
PUSH(D) A,C,D
PUSH(E) A,C,D,E
POP(E) A,C,D
POP(D) A,C
POP(C) A
POP(A) EMPTY
Application’s of stack
1. It can be used in function cells’
2. It can be used in implement a clacqulater, by sorting the
Apperans&operators
3. A stack is useful in writing recursive calls
4. A stack is useful for the compilers (or) o.s
Sravan cse 2nd batch
Queue’s : A queue is a linear data structure .it is a linear sequential
-list of items that are accessed in the order of First in First out [LIFO]
-that is, the first item inserted in a queue in all so the first item is accessed,
the second item inserted in queue is all so second one to be accessed, and
the last one to be inserted is also the last one to be accessed. We can’t store
(or) access the items in a queue is arbitrarily (or) in any random fashion.
For eg: a queue is used in Train reservation counters, Bank’s, Bus counters
These are two operation’s is in queue
1. Write operation
2. Read operation
The write operation adds a new item to the queue, whereas the read item
reads one item from the queue. The bellow figure shows how the queue operation
is going.
Circular list/queue:
A list/queue can also be Circular it is called as Circular
list/queue in queue the items from a queue get deleted, the space for that item
is reused that is it is not reused. Those queues positions are continue to be
empty. This problem is salved by a Circular linked list. The items in a Circular
list are arranged to form a Circular shape. That is why a Circular list doesn’t
have a beginning or end. The logical representation of a Circular queue is
shown bellow
Sravan cse 2nd batch
Library functions:
The “c language” is a number of
library functions that performs various task the ANSI-the
ANSI commit has standardized header file which contain
these functions
1.reading strings: -
The input function scan can be used with personage of % format specification to
read in a string of char’s
Eg: char address [15];
Scan [“%s “, address];
Writing strings: -
We have used extensively the printf function with %s format to print
strings to the screen, the format %s can be used to display an array of chars that is
terminated by the null chars.
Eg: printf (“%s”, name)
2. Combinigstring together: -
We can’t assign one string to another string directly; we can’t join
two strings together by the simple arithmetic addition
Eg: string3=string1+string2;
The parses of combining two strings together is called concatenation OR
putting string together
If (name1==name2)
Are not permeated it is necessary to comparison of two strings to be tested,
Character-by-character.
FUNCTION ACTION
Str cat () Combining two strings
Str cmp () Compares two strings
Str cpy () Copy one string to another
Str Len () Finds length of a string
1.String concatenation: -
The strcat function joints two strings toghather
FOR E.g.
: Syntax:
Strcat (str1, str2);
2.Strcmp function: -
String comparison compares two string identified by the arguments and has a value zero
if they rent between the no matching characters in the strings
For E.g.:
Strcmp (name,”C.S.E.”);
Syntax:
Strcmp (str1, str2);
String1&string2 may be string variables or string constants.
3.strcpy function: -
The strcpy function works almost like a string assignment operator
For E.g.:
Strcpy (city,”HYD”);
: Syntax:
Strcpy (str1, str2):
4.strlen function: -
These function countess and returns the number of character in a string
For E.g.:
N=strlen (“GJC”);
: Syntax:
N=strlen (string);
Struct tag_name;
{
Type1 data 1;
Type2 data 2;
Type3 data 3;
.
.
.
.
Typen data n;
}
For E.g.:
Struct student:
{
Int reg_no;
Char name;
Float avg;
}
OPERATION MENINING
Person1=person2 Assign person2 to person1
Person1==person2 Compare all members of person1 and
person2, return if they are equal, other wise
“0” [zero]
Person1! =Person2 Returen1 if they are not equal, other wise
“0” [zero]
3.array of structures: -
We use structures to describe the format of a no’ of related variables
analizeining the marks obtained by a class of students, we use to describe student
name marks may declare on array of student and each element of the array
representing a structure variable
Syntax:
Struct marks
{
Int sub 1;
Int sub2;
Int sub3;
};
Main ()
{
Stract struct marks student [3]={{45,68,81},{75,53,69},{57,36,71}};
Sravan cse 2nd batch
4.stracture with in stricture: -
Structure with in a structure means nesting of structure’s .a
structure member it self may be a structure i.e; making structure as member of
another structure, then these can be accessed by writing variable member, sub
member.
Syntax: -
Struct salary
{
Char name [10];
Char dept [10];
int basic_ pay;
int dearness_allowance;
int house_rent_allowence;
int city_allowence;
}
Employee;
Sravan cse 2nd batch
RECURSION
When a called function in turn calls another function a praises of
‘changing’ accurse. Recursion is a special case of these process, where a function
calls it self
Main ()
{
Printf (“this is an example of recursion \n”);
Main ()
}
[10,000*(10,000-1)/2]
4.tree sort: - the tree sort is based on the mechanism of sorting a binary tree in
to its proper order.it is related to placing a value in it’s appropriate position os the
tree binary tree as a root at the top and elements down the tree. In tree sorting we
organize in althea elements in such a position that they are sorted from left to
right.
Q:- write the time and space complexicite,time complexicite for all sorting
techniques
sorting techniques case Time complexicite Space complexicite
1.selection sort average 0 (n2) 0
Worest 0 (n2) 0
2.Bubble sort Average 0 (n2) 0
Worest 0 (n2) 0
3.insertion sort Average 0 (n2) 0
Worest 0 (n2) 0
4.merge sort Average 0 (n log n) 0 (n)
Worest 0 (n log n) 0 (n)
5.Quick sort Average 0 (n log n) 0 (n)
Worest 0 (n log n) 0 (n)
Sravan cse 2nd batch
SEARCHING TEACHNIQUES
SEARCHING TEACHNIQUES: - searching for information is a very common
and widely used in computer applications.there are numerous occasions when
information needs to be searched for
Eg:
a. Find out the list of employee who earns more than rs.10, 000 for month.
b. List all the customers who were serviced by a particular sales person in the
last month
There are two searching methods is available
1.secuentiol search (al so called as linear search)
2.Binary search
1.sequentiol search: - sequential search in the traditional mechanism of searching
for information .it is very simple under stands, but can be very poor in
performance some times.as the name says, the process of searching for
information in a sequential search is sequential (or one ofter the other) given a list
so items, to find if a particular item exist (or does not exist) in the list.the
following sequential algorithm can be used
Sequential algorithm: -
1.start with a number I=1
2.if there are still more items in the list. Compare the 1th element in the list with
the item to be searched
else
Item is not found in the list. Stop
3.if a match is found
The item is found in the list. Stop
else
odd one 1 to I,and go back to step 1.
The important point in this algorithm is that we strat with the first item in the
list, and go up to the end of the list, or until the item to be searched is found which
ever is earlier.
The number times this algorithm is likely it be executed, depending on the
best average and worst possible case. We assume that the list contains “N”items
case Meaning Number of interation
Best The item to be searched is the first item 1
in the list
Average The item to be searched is found some N/2
were close to be middle of the list
Worst The item to be searched is the last item N
in the list it or a does not existent at all
Sravan cse 2nd batch
If we have 10,000 items in a listen an average it would required 10,000/2.i.e;
5,000 interation. Consider an array named arr, containing 1,000 integers. We can
use the following function to search for a specific item (say item) in that array.
Syntax: -
Sequentiol_search (int item)
{
Int I;
For (I=0;I<1000;I++)
{
if (arr[I]==item)
{
Printf (“item found at the position %d in the arr \n”,I+1);
return;
}
}
Printf (“item not found in the arr \n”);
}
The function is quite simple to under stands.it basically scans through all the
elements of the array one by one. Starting with the first element of the array.
Binary search: - Binary is a vast improvement over the sequential search. It
can’t always be used for binary search the item in the list must be in stored order
(Either ascending or descending) This is a pre-requisite for binary search. It the list
to be searched for a specific item is not stored, binary search fails
Binary search is called as divided-and-conquer method. Assume that a list
contains 1,000 elements. Which are stored in an ascending order of their values.
An item “x” is to be searched in the list. Then the approach used by the binary
search algorithm is as follows
Steps 1: - devoid the original list into two equals sized logical values. Let us call
then of half1 and half 2. In this case the original1, 000 elements list will be split
into two values. Each containing 500 elements
Step 2: - compare x with the last (I.e.; 500th ) elements of half1. There are 3
possibilities.
a. The value of ‘x’ matches with that of the 500 element of half1,the search is
successful their four display an appropriate message and stop processing.
b. The value of ‘x’ is greater than that of the 500 element of . note that over list
was originally sorted in the ascending order thus if the values of ‘x’ is greater
than the last elements of half1,it means that ‘x’ is definitely not one of the first
500 elements in the 10,000 elements array.
c. The values of ‘x’ is less than that of the 500 elements of half1.note that over
list was originally stored in the ascending order. Thus if the values of ‘x’ is less
than the last elements of half1,it means that if ‘x’ is in the list . it must
definitely be one of the first 500 elements in the 1,000 elements array.
Sravan cse 2nd batch
Out come Next action
a.x=last element of haif1 1.stop processing
b.x>last element of half1 2.search of x with in half2
c.x<last elements of half1 3.search of x half2
Int binary_search (int arr [], int item)
{
Int low=0,heigh=999,mid;
While (low<=high)
{
Mid = (low+high)/2;
If (item<arr [mid])
High=mid-1;
else
if(item>arr[mid])
low=mid+1;
else
Return_mid;
}
Return-1;
}
Sravan cse 2nd batch