Assignment
Assignment
USES
A structure is a collection of variables of same or different datatypes. It is
useful in storing or using informations or databases.
Example: An employee’s record must show its salary, position, experience, etc. It
all can be stored in one single variable using structures.
struct Employee
{
char ename[20];
int id;
float salary,experience;
};
Structure written inside another structure is called as nesting of two structures.
Nested Structures are allowed in C Programming Language. We can write one Structure
inside another structure as member of another structure.
Example:
struct date
{
int date;
int month;
int year;
};
struct Employee
{
char ename[20];
int ssn;
float salary;
struct date doj;
};
Here, struct date is passed as a member of struct employee. Hence, nested.
4. pointer
C Pointers
The pointer in C language is a variable which stores the address of another
variable. This variable can be of type int, char, array, function, or any other
pointer. The size of the pointer depends on the architecture. However, in 32-bit
architecture the size of a pointer is 2 byte.
Consider the following example to define a pointer which stores the address of an
integer.
int n = 10;
int* p = &n; // Variable p of type pointer is pointing to the address of the
variable n of type integer.
Declaring a pointer
The pointer in c language can be declared using * (asterisk symbol). It is also
known as indirection pointer used to dereference a pointer.
pointer example
As you can see in the above figure, pointer variable stores the address of number
variable, i.e., fff4. The value of number variable is 50. But the address of
pointer variable p is aaa3.
By the help of * (indirection operator), we can print the value of pointer variable
p.
Let's see the pointer example as explained for the above figure.
#include<stdio.h>
int main(){
int number=50;
int *p;
p=&number;//stores the address of number variable
printf("Address of p variable is %x \n",p); // p contains the address of the number
therefore printing p gives the address of number.
printf("Value of p variable is %d \n",*p); // As we know that * is used to
dereference a pointer therefore if we print *p, we will get the value stored at the
address contained by p.
return 0;
}
Output
ADVERTISEMENT
2) We can return multiple values from a function using the pointer.
3) It makes you able to access any memory location in the computer's memory.
Usage of pointer
There are many applications of pointers in c language.
#include<stdio.h>
int main(){
int number=50;
printf("value of number is %d, address of number is %u",number,&number);
return 0;
}
Output
int *p=NULL;
In the most libraries, the value of the pointer is 0 (zero).
part 2
11.List Applications Of Stacks. Using Stack Write An Algorithm To Determine If A
Given String Is Palindrome And Print Suitable Message As Output.
-> application
Following are the applications of stack:
1. Expression Evaluation
2. Expression Conversion
i. Infix to Postfix
ii. Infix to Prefix
iii. Postfix to Infix
iv. Prefix to Infix
3. Backtracking
4. Memory Management
A string is palindrome, if string remains same after reversing the sequence of it's
character. For example, "asdfdsa" is a palindrome string whereas string "mango" is
not a palindrome string.
A stack is LAST IN FIRST OUT (LIFO) data structure. The element which is inserted
last, is accessed first. Insertion and deletion of elements happens only at top of
the Stack. The sequence of exit of elements from a stack is reverse of the sequence
of their entry in stack.
Sequence of Entry.
A --> B --> C -- > D --> E
Sequence of Exit.
E --> D --> C --> B --> A
Algorithm to check palindrome string using stack
Find the length of the input string using strlen function and store it in a integer
variable "length".
Using a for loop, traverse input string from index 0 to length-1 and push all
characters in stack.
Remove (Pop) characters from stack one by one using a for loop and compare it with
corresponding character of input string from beginning(traverse from index 0 to
length-1). If we found a mismatch the input string is not a palindrome string
otherwise palindrome string.
C program to check a string is palindrome or not using a stack
#include <stdio.h>
#include <string.h>
/*
Initializes the top index to -1
*/
void initialize() {
st.top = -1;
}
/*
Checks if Stack is Full or not
*/
int isFull() {
if(st.top >= MAXSIZE-1)
return TRUE;
else
return FALSE;
}
/*
Checks if Stack is Empty or not
*/
int isEmpty() {
if(st.top == -1)
return TRUE;
else
return FALSE;
}
/*
Adds an element to stack and then increment top index
*/
void push(int num) {
if (isFull())
printf("Stack is Full...\n");
else {
st.array[st.top + 1] = num;
st.top++;
}
}
/*
Removes top element from stack and decrement top index
*/
int pop() {
if (isEmpty())
printf("Stack is Empty...\n");
else {
st.top = st.top - 1;
return st.array[st.top+1];
}
}
int main() {
char inputString[100], c;
int i, length;
initialize();
printf("Enter a string\n");
gets(inputString);
length = strlen(inputString);
/* Push all characters of input String to Stack */
for(i = 0; i < length; i++){
push(inputString[i]);
}
/* Poping characters from stack returs the characters of input string
in reverse order. We will then compare it with corresponding
characters of input string. If we found a mismatch the input
string is not a palindrome string */
for(i = 0; i < length; i++){
if(pop() != inputString[i]) {
printf("Not a Palindrome String\n");
return 0;
}
}
printf("Palindrome String\n");
return 0;
}
</string.h></stdio.h>
Output
Enter a string
ASDFGFDSA
Palindrome String
Enter a string
TECHCRASHCOURSE
Not a Palindrome String
Infix to Postfix
Postfix:
AB-CD*/E+
Even though a linear queue is simple to implement, it has some drawbacks. Removing
items from the queue can create more space. However, it can still be difficult to
enter new elements as this can cause an underflow condition. A circular queue helps
to overcome this issue.
As a circular queue connects the two ends, the first item comes after the last
item. There is no overflow condition in a circular queue until the queue is
actually full. Therefore, entering a new element is easy.
Furthermore, the circular queue works according to the below two conditions. The
maxSize indicates the maximum number of items the queue can consist of.
part 4
6. What Is A Single Linked List? Explain With An Example How A Single Linked List
Can Be Used For
Sorting A Set Of N Numbers.
A singly linked list is a type of linked list that is unidirectional, that is, it
can be traversed in only one direction from head to the last node (tail).
Each element in a linked list is called a node. A single node contains data and a
pointer to the next node which helps in maintaining the structure of the list.
The first node is called the head; it points to the first node of the list and
helps us access every other element in the list. The last node, also sometimes
called the tail, points to NULL which helps us in determining when the list ends.
svg viewer
An example of a singly Linked List.
#include <stdio.h>
if(head == NULL) {
return;
}
else {
while(current != NULL) {
//Node index will point to node next to current
index = current->next;
while(index != NULL) {
//If current node's data is greater than index's node data,
swap the data between them
if(current->data > index->data) {
temp = current->data;
current->data = index->data;
index->data = temp;
}
index = index->next;
}
current = current->next;
}
}
}
int main()
{
//Adds data to the list
addNode(9);
addNode(7);
addNode(2);
addNode(5);
addNode(4);
//Sorting list
sortList();
return 0;
}
Output:
Original list:
9 7 2 5 4
Sorted list:
2 4 5 7 9
11. write advantages of doubly linked list over singly linked list. write c
function that will insert a given integer value into an ordered doubly linked list.
Following are advantages/disadvantages of doubly linked list over singly linked
list.
Advantages over singly linked list
1) A DLL can be traversed in both forward and backward direction.
2) The delete operation in DLL is more efficient if pointer to the node to be
deleted is given.
3) We can quickly insert a new node before a given node.
In singly linked list, to delete a node, pointer to the previous node is needed. To
get this previous node, sometimes the list is traversed. In DLL, we can get the
previous node using previous pointer.
// if list is empty
if (*head_ref == NULL)
*head_ref = newNode;
else {
current = *head_ref;
current->next = newNode;
newNode->prev = current;
}
}
part 5
1. What Is An Algorithm? Write General Characteristics Of Design Analysis And
Algorithm.
Algorithm is a step-by-step procedure, which defines a set of instructions to be
executed in a certain order to get the desired output. Algorithms are generally
created independent of underlying languages, i.e. an algorithm can be implemented
in more than one programming language.
From the data structure point of view, following are some important categories of
algorithms −
Search − Algorithm to search an item in a data structure.
Characteristics of an Algorithm
Not all procedures can be called an algorithm. An algorithm should have the
following characteristics −
Unambiguous − Algorithm should be clear and unambiguous. Each of its steps (or
phases), and their inputs/outputs should be clear and must lead to only one
meaning.
Output − An algorithm should have 1 or more well-defined outputs, and should match
the desired output.
In divide and conquer approach, the problem in hand, is divided into smaller sub-
problems and then each problem is solved independently. When we keep on dividing
the subproblems into even smaller sub-problems, we may eventually reach a stage
where no more division is possible. Those "atomic" smallest possible sub-problem
(fractions) are solved. The solution of all sub-problems is finally merged in order
to obtain the solution of an original problem.
Divide/Break
This step involves breaking the problem into smaller sub-problems. Sub-problems
should represent a part of the original problem. This step generally takes a
recursive approach to divide the problem until no sub-problem is further divisible.
At this stage, sub-problems become atomic in nature but still represent some part
of the actual problem.
Conquer/Solve
This step receives a lot of smaller sub-problems to be solved. Generally, at this
level, the problems are considered 'solved' on their own.
Merge/Combine
When the smaller sub-problems are solved, this stage recursively combines them
until they formulate a solution of the original problem. This algorithmic approach
works recursively and conquer & merge steps works so close that they appear as one.
Examples
The following computer algorithms are based on divide-and-conquer programming
approach −
Merge Sort
Quick Sort
Binary Search
Strassen's Matrix Multiplication
Closest pair (points)
There are various ways available to solve any computer problem, but the mentioned
are a good example of divide and conquer approach.