Articles by "Data Structure"
Showing posts with label Data Structure. Show all posts
In computer science, a linked list is a linear collection of data elements, in which linear order is not given by their physical placement in memory. Each pointing to the next node by means of a pointer. It is a data structure consisting of a group of nodes which together represent a sequence.
In computer science, a linked list is a linear collection of data elements, in which linear order is not given by their physical placement in memory. Each pointing to the next node by means of a pointer. It is a data structure consisting of a group of nodes which together represent a sequence.
In computer science, a linked list is a linear collection of data elements, in which linear order is not given by their physical placement in memory. Each pointing to the next node by means of a pointer. It is a data structure consisting of a group of nodes which together represent a sequence.
Write an Algorithm to implement insertion operations ( insert at front, Insert at End, Insert at Middle) on a linked list.
Algorithm to insert node at front:
Insert at front()
{
Allocate a memory for new node(new1)
if (new1== NULL)
then
display memory is full (insertion is not possible)
end if
else
read element ele;
new1->data=ele;/*copy the data into new1 node */
new1->next=header->next;
header->next=new1;
end else
}
Algorithm to insert node at End:
Insert at End()
{
Allocate a memory for new node(new1)
if(new1== NULL)
then
display memory is full (insertion is not possible)
end if
else
ptr=header
while(ptr->next!=NULL )/*ptr moves end of list */
then
ptr=ptr->next
end
new1->next=NULL
new1->data=ele
ptr->next=new1
end else
}
Algorithm to insert node at Middle:
Insert at Middle()
{
Allocate a memory for new node(new1)
if (new1== NULL)
then
display memory is full (insertion is not possible)
exit
else
read ele,pos
ptr=header
count=0
while(ptr->next!=NULL)
{
ptr=ptr->next;
count ++;
}
if(count<pos-1)
then
display position is not of range
end
ptr=header
count=0
while(count<pos-1)
then
ptr=ptr->next
count++
end
new1->data=ele
new1->next=ptr->next
ptr->next=new1
end else
}
Write program algorithms using arrays, pointers & structures.
a) Implementation of basic arithmetic operations using switch – case statements and functions.
Functions: Function is a portion of code within a larger program, which performs a specific task and can be relatively independent of the remaining code. The syntax of many programming languages includes support for creating self contained subroutines, and for calling and returning from them.
Program Algorithm:
i. Get two numbers and the arithmetic operation to be performed on them as input.
ii. Use a switch case statement
iii. According to the selected option the desired function is called from inside a switch case function.
iv. The result is printed.
v. The steps are repeated until the user wishes to quit.
b) Creating an employee database using structure.
Structures: A structure provides a means of grouping variables under a single name for easier handling and identification. A structure is declared by using the keyword struct followed by an optional structure tag followed by the body of the structure. The variables or members of the structure are declared within the body.
Program Algorithm:
i. Create a structure using the struct variable for each employee containing the necessary attributes of that employee.
ii. Create an array of such structures for n number of employees.
iii. Get the input of each attribute for each employee from the user.
iv. Calculate the gross salary by the following formulas.
hra (10 %), da(50%), ta(5%), pf(11%), tax(6%) from basic.
Gross = basic + hra + da + ta
Deduction = pf + tax
Netsalary = gross – deduction
v. Display the results.
c) Swapping of two numbers using call by reference.
Pointers: Pointers are variables that hold addresses in C. A pointer is a special type of variable that contains a memory address rather than a data value. Just as data is modified when a normal variable is used, the value of the address stored in a pointer is modified as a pointer variable is manipulated. A pointer to the variable is passed to the function. The pointer can then be manipulated to change the value of the variable in the calling routine. It is interesting to note that the pointer itself is passed by value. The function cannot change the pointer itself since it gets a local copy of the pointer. However, the function can change the contents of memory, the variable, to which the pointer refers. The advantages of passing by pointer are that any changes to variables will be passed back to the calling routine and that multiple variables can be changed.
Program Algorithm:
i. Get the numbers two be swapped
ii. Pass the elements using their address to the swap function
iii. Receive the parameters for the swap function using pointers
iv. Perform swapping inside function
v. Display the result in the main function
Data Structure and Algorithms Introduction:
A data structure is an arrangement of data in a computer's memory or even disk storage. An example of several common data structures are arrays, linked lists, queues, stacks, binary trees, and hash tables. Algorithms, on the other hand, are used to manipulate the data contained in these data structures as in searching and sorting.
C provides us with just one built in data structure, the array. Although efficient and easily used and understood, arrays often don't provide us with the level of functionality we need from a data structure. If the quantity of data we need to store is not well known at compile time, then using arrays could waste memory if too large, or waste time in resizing at runtime if too small. Several, more abstract, data structures can be constructed to better serve our needs.
Many algorithms apply directly to a specific data structures. When working with certain data structures you need to know how to insert new data, search for a specified item, and deleting a specific item.
Commonly used algorithms include are useful for:
• Searching for a particular data item (or record).
• Sorting the data. There are many ways to sort data. Simple sorting, Advanced sorting
• Iterating through all the items in a data structure. (Visiting each item in turn so as to display it or perform some other action on these items)
Recursive Functions:
Functions are used to encapsulate a set of operations and return information to the main program or calling routine. The use of functions provides several benefits. First, it makes programs significantly easier to understand and maintain. The main program can consist of a series of function calls rather than countless lines of code. A second benefit is that well written functions may be reused in multiple programs. The C standard library is an example of the reuse of functions. A third benefit of using functions is that different programmers working on one large project can divide the workload by writing different functions. A recursive function is a function that calls itself, thus understanding recursion to understand recursion seems like a call to itself.
a) Algorithm to find Fibonacci Series:
i Get the input number n, i.e. the number of terms to be generated in the series.
ii Print the first two numbers of the series as 0 and 1, assign these two values to
variables a and b.
iii Compute the next element in the series as the sum of the previous two elements.
iv Keep on sliding the variables a and b to b and c to compute the series.
b) Algorithm to find the factorial of a given number using recursion:
i. Get the number N
ii. call the function for N
iii. Inside the function compute fact = N * fact (N-1)
iv. Step ((3) is repeated till N becomes 1.
v. Return the fact value to the main function and display the result.
c) Algorithm to sort a set of numbers using arrays:
i Initialize an array of required size.
ii Get the input set of numbers and store them in the array.
iii Take each number of an array and compare that number with the subsequent
numbers in the array.
iv If the former is greater than the latter, swap the two numbers.
v Repeat the procedure for all the numbers in the array.
vi At the end of the procedure, the array would be sorted and is printed.
Subscribe to:
Posts (Atom)

