#include<stdio.h>
#include<conio.h>
struct Node{
int data;
Node *next;
Node * prev;
Node(int a){
data=a;
next=NULL;
prev=NULL;
}
};
struct llist{
Node *front;
Node *rear;
llist(){
front=NULL;
rear=NULL;
}
void add(int x){
Node *temp=new Node(x);
if(front==NULL){
front=temp;
rear=temp;
}
else{
rear->next=temp;
temp->prev=rear;
rear=temp;
}
}
void display(){
Node *f=front;
if(f==NULL){
printf("\nNothing is added to list...\n");
return;
}
while(f!=NULL){
printf("Value of the Node is: %d\n",f->data);
f=f->next;
}
}
void rev_display(){
Node *f=rear;
if(f==NULL){
printf("\nNothing is added to list...\n");
return;
}
while(f!=NULL){
printf("Value of the Node is: %d\n",f->data);
f=f->prev;
}
}
void average(){
Node *f=front;
if(f==NULL){
printf("\nNothing is added to list...\n");
return;
}
int total=0;
int count=0;
while(f!=NULL){
total+=f->data;
count++;
printf("Value of the Node is: %d\n",f->data);
f=f->next;
}
printf("\nAverage salary is: %d\n",total/count);
}
void ndelete(int x){
Node *f=front,*g;
if(f==NULL){
printf("\nNothing is added to list...\n");
return;
}
if(front->data==x){
front=front->next;
front->prev=NULL;
delete f;
}
else{
while(f->data!=x && f!=NULL){
g=f;
f=f->next;
}
if(f->data==x){
g->next=f->next;
if(f->next !=NULL)
(f->next)->prev=g;
if(rear->data==x)
rear=rear->prev;
/*f->prev=NULL;
f->next=NULL;*/
delete f;
}
}
}
void nndelete(){
Node*f=front;
if(f==NULL){
printf("\nNothing is added to list...\n");
return;
}
while(front!=NULL){
f=front;
front=front->next;
//front->prev=NULL;
delete f;
}
}
void search(int x){
Node *f=front;
if(f==NULL){
printf("\nNothing is added to list...\n");
return;
}
while(f!=NULL){
if(f->data==x){
printf("\nNode found\n");
break;
}
f=f->next;
}
if(f==NULL)
printf("\nNode not found...\n");
}
};
int main(){
llist ls;
int m;
for(int i=0;i<10;i++){
printf("Enter the value you want to add: ");
scanf("%d",&m);
ls.add(m);
}
ls.rev_display();
printf("\nDeleting middle node . . . \n");
ls.ndelete(50);
ls.display();
printf("\nDeleting first node . . . \n");
ls.ndelete(10);
ls.display();
printf("\nDeleting last node . . . \n");
ls.ndelete(100);
ls.display();
printf("Enter the value you want to search: ");
scanf("%d",&m);
ls.search(m);
ls.average();
ls.nndelete();
ls.display();
getch();
}
Thursday, 30 April 2015
DOUBLE LINKED LIST
DOUBLE LINKED LIST:
Sunday, 26 April 2015
DATA STRUCTURE AND ALGORITHM: SHORT QUESTIONS
DATA STRUCTURE AND ALGORITHM: SHORT QUESTIONS
Q1
[Why to study this course]
Ans: We have studied basic of
programming language in Programming fundamental course and structure
programming also in object oriented programming. Now there is some problem we
face in our daily life and their data is huge and we cannot store data in computer’s
ram. So the study of this course is due to manipulate data and store in
commuter.
Suppose computers were infinitely fast
and had free (of cost) memory. Would you have any reason to study Data Structures and Algorithms?
Ans: yes there a lot of other things
that we have to study in data structures and algorithms like how to tackle huge
data and how to perform some action on particular data with an efficient way.
Q2
[Building blocks of data structures]
What is the difference between data type and abstract data type?
Ans: A data type tends to mean a primitive
data type. Primitive data are built-in data types, such as integers, characters
and Booleans. Abstract data types are generally
constructed by the user or by a higher level language.
Q3
[Different data structures suit different problems]
Insertion
and Deletion are costlier in arrays
as compared to linked lists. Justify this statement.
Array has static memory. It cannot be
deleted just we -1 the index and think that deleted the memory. While on the
other hand in link list in actual memory is free from the ram.
•On the other hand it’s easier to
store data of different sizes in a linked list. An array assumes every element
is exactly the same size.
•Shuffling a linked list is just a
matter of changing what points to what. Shuffling an array is more complicated
and takes more memory.
Q4
[Application]
Let’s assume several people are giving
print requests to a single shared printer. Which of the data structure is
likely to be used at printer?
Queue data structure because this rule
it is based on first in first out. In which first element is inserted from one
end and deletion of existing element takes place from the other end. So this is
appropriate for this problem.
Q5
[Inspiration]
An operating system (like Windows) is
a program or an algorithm?
Program. Because it is just interface
and provide some service to computer user.
(algorithm to evaluate sum, Recursion,linked list,pointers,searching an integer)
Assignment # 2 SHORT QUESTIONS
(algorithm to evaluate sum, Recursion,linked list,pointers,searching an integer)
Q1 [Remembering the C language]
Write a function/algorithm to evaluate the sum of {1, 4, 9,
16, . . . 100}.
Ans:
#include<stdio.h>
int
square(int x){
return x*x;
}
int
main(){
int sum;
for(int i=0;i<=10;i++){
sum=sum+square(i);
}
Printf(“Sum
is %d”,sum);
}
Q2 [Recursion: A useful function, we will
interact with, often in this course]
What does the following function return when it is called
with 5 as its argument i.e. when called in main as xyz(5);
if(x<=2)
return 1;
else
return xyz(x-1)+ xyz(x-2)+ xyz(x-3);
}
Q3 [Linked List, a sample program: Explain the
output of the following program]
#include<stdio.h>
#include<conio.h>
struct
Node{
int data;
Node *next;
Node(int a){
data=a;
next=NULL;
}
};
struct
llist{
Node *start;
llist(){
start=NULL;
}
void add(int x){
Node
*temp=new Node(x);
if(start==NULL){
start=temp;
}
else{
temp->next=start;
start=temp;
}
}
void display(){
Node
*f=start;
while(f!=NULL){
printf("Value
of the Node is: %d\n",f->data);
f=f->next;
}
}
void average(){
Node
*f=start;
int total=0;
int count=0;
while(f!=NULL){
total+=f->data;
count++;
f=f->next;
}
printf("\nAverage salary is:
%d\n",total/count);
}
};
int
main(){
llist ls;
int m;
for(int i=0;i<5;i++){
printf("Enter
the value you want to add: ");
scanf("%d",&m);
ls.add(m);
}
ls.display();
ls.average();
getch();
}
Ans:
This program consists of two functions other than the main function, one is
display which displays every value in the link list and the other is average
which uses a loop to calculate the sum and no of values in a link list and then
display the average.
After
execution, the program prompts the user to enter the value in the link list,
once the user entered all the 5 values in link list, the ls.display and
ls.average function will be called and the program displays sum and average.
Q4 [EE210 rocks as it’s
too interesting: Refer to the following diagram and answer the questions]
Give the values
of the following expressions
- ptr1->data Ans: 22
- ptr2->next->data Ans:75
- head->next->next->data Ans: 34
Are the following
expressions true or false
- head->next = = ptr1 True
(
- ptr1->next->data= = 46 False
- ptr2->next = = NULL False
Decide
whether the syntax of each of the following statements is valid or invalid. If
it is valid, mark it as such; if it is invalid, explain what is wrong.
- head->next = ptr1->next; Valid
- ptr2 = ptr1->next->data; Pointers
should contain address information
Write
one statement to do each of the following:
- Make head point to the
node containing 34. head->next->next->data;
- Make ptr2 point to the
last node in the list. Ptr->next
- Make head point to an
empty list. head->next= NULL;
- Set the data member of the
node containing 34 to 45. Ptr1->next->data=45;
Q5 [Searching in a linked list: Is a given
element member of the given linked list?]
(Hint: Q3 might be helpful)
Write a function/algorithm to find a value in a singly
linked list of integers. Function arguments are two, the pointer to the head
node and the integer to be searched.
void
search(int start, int find){
Node
*f=start;
while(f!=NULL){
if
(f==find)
printf("The
Number Has been Found”);
else f=f->next;
}
Thursday, 23 April 2015
IMPORTANT QUESTIONS
IMPORTANT QUESTIONS
Write a function/algorithm to evaluate the sum of {1, 4, 9,
16, . . . 100}.
Ans:
#include<stdio.h>
int
square(int x){
return x*x;
}
int
main(){
int sum;
for(int i=0;i<=10;i++){
sum=sum+square(i);
}
Printf(“Sum
is %d”,sum);
}
Q2 [Recursion: A useful function, we will
interact with, often in this course]
What does the following function return when it is called
with 5 as its argument i.e. when called in main as xyz(5);
if(x<=2)
return 1;
else
return xyz(x-1)+ xyz(x-2)+ xyz(x-3);
}
Q3 [Linked List, a sample program: Explain the
output of the following program]
#include<stdio.h>
#include<conio.h>
struct
Node{
int data;
Node *next;
Node(int a){
data=a;
next=NULL;
}
};
struct
llist{
Node *start;
llist(){
start=NULL;
}
void add(int x){
Node
*temp=new Node(x);
if(start==NULL){
start=temp;
}
else{
temp->next=start;
start=temp;
}
}
void display(){
Node
*f=start;
while(f!=NULL){
printf("Value
of the Node is: %d\n",f->data);
f=f->next;
}
}
void average(){
Node
*f=start;
int total=0;
int count=0;
while(f!=NULL){
total+=f->data;
count++;
f=f->next;
}
printf("\nAverage salary is:
%d\n",total/count);
}
};
int
main(){
llist ls;
int m;
for(int i=0;i<5;i++){
printf("Enter
the value you want to add: ");
scanf("%d",&m);
ls.add(m);
}
ls.display();
ls.average();
getch();
}
Ans:
This program consists of two functions other than the main function, one is
display which displays every value in the link list and the other is average
which uses a loop to calculate the sum and no of values in a link list and then
display the average.
After
execution, the program prompts the user to enter the value in the link list,
once the user entered all the 5 values in link list, the ls.display and
ls.average function will be called and the program displays sum and average.
Q4 [EE210 rocks as it’s
too interesting: Refer to the following diagram and answer the questions]
Give the values
of the following expressions
- ptr1->data Ans: 22
- ptr2->next->data Ans:75
- head->next->next->data Ans: 34
Are the following
expressions true or false
- head->next = = ptr1 True
(
- ptr1->next->data= = 46 False
- ptr2->next = = NULL False
Decide
whether the syntax of each of the following statements is valid or invalid. If
it is valid, mark it as such; if it is invalid, explain what is wrong.
- head->next = ptr1->next; Valid
- ptr2 = ptr1->next->data; Pointers
should contain address information
Write
one statement to do each of the following:
- Make head point to the
node containing 34. head->next->next->data;
- Make ptr2 point to the
last node in the list. Ptr->next
- Make head point to an
empty list. head->next= NULL;
- Set the data member of the
node containing 34 to 45. Ptr1->next->data=45;
Q5 [Searching in a linked list: Is a given
element member of the given linked list?]
(Hint: Q3 might be helpful)
Write a function/algorithm to find a value in a singly
linked list of integers. Function arguments are two, the pointer to the head
node and the integer to be searched.
void
search(int start, int find){
Node
*f=start;
while(f!=NULL){
if
(f==find)
printf("The
Number Has been Found”);
else f=f->next;
}
Saturday, 18 April 2015
LINKED LIST
LINKED LIST:
"it is a collection of objects linked together by refrence from one object to another.these objects are called node.
#include<stdio.h>
#include<conio.h>
struct
Node{
int data;
Node *next;
Node(int a){
data=a;
next=NULL;
}
};
struct
llist{
Node *start;
llist(){
start=NULL;
}
void add(int x){
Node
*temp=new Node(x);
if(start==NULL){
start=temp;
}
else{
temp->next=start;
start=temp;
}
}
void display(){
Node
*f=start;
while(f!=NULL){
printf("Value
of the Node is: %d\n",f->data);
f=f->next;
}
}
void average(){
Node
*f=start;
int total=0;
int
count=0;
while(f!=NULL){
total+=f->data;
count++;
f=f->next;
}
printf("\nAverage salary is:
%d\n",total/count);
}
};
int
main(){
llist ls;
int m;
for(int i=0;i<5;i++){
printf("Enter
the value you want to add: ");
scanf("%d",&m);
ls.add(m);
}
ls.display();
ls.average();
getch();
}
OUTPUT:
Subscribe to:
Posts (Atom)
