Professional Documents
Culture Documents
DSFILE
DSFILE
DEFINITION:-
ARRAY
An array can be defined as a finite collection of homogeneous element.
This means that an array can store either all integers, all floating point number, all
characters or anyother complex datatype but all of same type.
The individual elements within the array can be designated by an index. An index is also
called asubscript.
Array Elements
For Example:-
Array
Array Indexes 0 1 2 3 5 6
INSERTION
An element can be inserted in an array at a particular
location.
ALGORITHM
1. Start
2. Create an array of a desire datatype and size
3. Initialize a variable i as 0
4. Enter the element at ith index of the array
5. Increment i by 1
6. Repeat steps 4 and 5 until the end of the array
7. Stop
CODING
include<stdio.h>
int main()
int student[40],pos,i,size,value;
for(i=0;i<size;i++)
scanf("%d",&student[i]);
printf("enter the position where you want to insert the element:");
scanf("%d",&pos);
printf("enter the value into that poition:");
scanf("%d",&value);
for(i=size-1;i>=pos-1;i--)
student[i+1]=student[i];
student[pos-1]= value;
printf("final array after inserting the value is ");
for(i=0;i<=size;i++)
printf("%d",student[i]);
return 0;
}
OUTPUT
DEFINITION :-
ARRAY
An array can be defined as an finite collection of homogeneous element.
This means that an array can store either all integers, all floating point number, all
characters orany other complex datatype but all of same type.
The individual elements within the array can be designated by an index. An index is also
called asubscript.
For Example: -
DELETION
It is the operation that removes an element from a given location of a list.
ALGORITHM
LA is a linear array with an elements and K is a positive integer such
that K<=N.We have to delete an array available at the kth position of
LA.
1. Start
2. Set J=K
3. Repeat steps 4 and 5 while J<N
4. Set LA [J] = LA[J+1]
5. Set J = J+1
6. Set N = N-1
7. Stop
CODING
#include <stdio.h>
#include <conio.h>
int main()
{
int arr[50];
int pos, i, num;
printf (" \n Enter the number of elements in an array: \n ");
scanf (" %d", &num);
printf (" \n Enter %d elements in array: \n ", num);
for (i = 0; i < num; i++ )
{ printf (" arr[%d] = ", i);
scanf (" %d", &arr[i]);
}
printf( " Define the position of the array element where you want to delete: \n ");
scanf (" %d", &pos);
if (pos >= num+1)
{ printf (" \n Deletion is not possible in the array.");
} else
{
for (i = pos - 1; i < num -1; i++)
arr[i] = arr[i+1];
}
printf (" \n The resultant array is: \n");
OUTPUT
DEFINITION
Binary Search is a searching algorithm for finding an element's position in a sorted array. In
this approach, the element is always searched in the middle of a portion of an array.
Binary search can be implemented only on a sorted list of items. If the elements are not sorted
already, we need to sort them first.
Algorithm:-
Step 1: set beg = lower_bound, end = upper_bound, pos = - 1
Step 2: repeat steps 3 and 4 while beg <=end
Step 3: set mid = (beg + end)/2
Step 4: if a[mid] = val
set pos = mid
print pos
go to step 6
else if a[mid] > val
set end = mid - 1
else
set beg = mid + 1
[end of if]
[end of loop]
Step 5: if pos = -1
CODING
#include <stdio.h>
int main()
{
int i, low, high, mid, n, key, array[100];
printf("Enter number of elementsn");
scanf("%d",&n);
printf("Enter %d integersn", n);
for(i = 0; i < n; i++)
scanf("%d",&array[i]);
printf("Enter value to findn");
scanf("%d", &key);
low = 0;
high = n - 1;
mid = (low+high)/2;
while (low <= high)
{
if(array[mid] < key) low = mid + 1;
else if (array[mid] == key)
{ printf("%d found at location %d.n", key, mid+1); break;
}
else high = mid - 1;
mid = (low + high)/2;
}
Output
LINKED LIST
Link structures are a completely different way to represent a list of items. Each elements of
the listis made to point to the next element in the list.
An element in the list is called a
node. The Node have two part’s :
Data and Next
Data parts contain the information about the element.
Next part contains a pointer to the next elements or a node in the list.
A list consisting four names i.e. A, B, C and D is represented using link structures
such a list iscalled linked list or a linear linked list.
ALGORITHM
1. Create a class node which has two attributes data and next. Next is a pointer to
the nextnode in the linked list.
2. Create another class which has two attributes head and tail.
3. Add node function will add a new node to the list.
a) Create a new node.
b) It first checks, whether the head = NULL
which means the list is empty.
c) If the list is empty, both head and tail will
point to the newly added node.
d) List ≠ empty the new node will be added to
end of the list such that tails next will
point to a newly added node. This new node will become the new tail of the list.
4. Repeat step 2 and 3 till whole of list is constructed.
5. Point tail = NULL
6. Stop
CODING
#include<stdio.h>
#include<conio.h>
#define TRUE 1
#define SIZE 10
struct link
{ int info;
int next; };
struct link node[SIZE];
int Getnode();
void Createlist();
void Freenode(int);
void Display();
void Insert(int,int);
void Delete(int);
int p, avail=0;
void main()
{ int
.ch=1,i,n,x;
clrscr();
/*Creation of available list*/
for(i=0;i<SIZE-1;i++)
node[i].next=i+1;
node[SIZE-1].next=-1;
printf("\n Create a List:");
Createlist();
while(ch!=4)
{
printf("\n1-DISPLAY");
printf("\n2-INSERT");
printf("\n3-DELETE");
printf("\n4-QUIT");
printf("\n Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1 :
Display();
break;
case 2:
printf("\n Node insertion:after which node:");
scanf("%d",&n);
p=n;
printf("\n Enter the item for insertion:");
scanf("%d",&x);
Insert(p,x);
break;
case 3:
printf("\n Enter the node after which the node will be deleted:");
scanf("%d",&n);
p=n;
Delete(p);
break;
case 4:
break;
default:
printf("\n Wrong choice! Try again:");
}
}}
int Getnode()
{
if (avail==-1)
{
printf("\n Overflow:");
exit(0);
}
p=avail;
avail=node[avail].next;
return p;
}
void Freenode(int q)
{
node[q].next=avail;
avail=q;
return;
}
void Createlist()
{ int
x;
char c;
p=Getnode();
printf("\n Enter an item to be inserted:");
scanf("%d", &x);
node[p].info=x ;
node[p].next=-1;
while(TRUE)
{
printf("\n Enter the choice(y/n):");
fflush(stdin);
c=getchar();
if(c=='y'||c=='Y')
{
printf("\n Enter an item to be inserted:");
scanf("%d",&x);
Insert(p,x);
node[p].next= -1;
}
else
return;
}}
void Display()
{
p=0;
while(node[p].next!=-1)
{ printf("\n%d\t%d\t%d:",p,node[p].info,node[p].next);
p=node[p].next;
}
printf("\n%d\t%d\t%d:",p,node[p].info,node[p].next);
}
void Insert(int r,int x)
{ int q;
if(r==-1)
{
printf("\n void insertion:");
return;
}
q=Getnode();
node[q].info=x;
node[q].next=node[r].next;
node[r].next=q;
return;
}
void Delete(int r)
{
int q;
if(r==-1||node[r].next==-1)
{
printf("\n void deletion:");
return;
}
q=node[r].next;
node[r].next=node[q].next;
Freenode(q);
return;
}
OUTPUT
4. AIM: Write a program in C to insert a node at the beginning of Singly Linked List.
LINKED LIST
Link structures are a completely different way to represent a list of items. Each element of
the listis made to point to the next element in the list.
An element in the list is called a
node. The Node have two part’s :
Data and Next
Data parts contain the information about the element.
Next part contains a pointer to the next elements or a node in the list.
A list consisting four names i.e. A, B, C and D is represented using link structures
such a list iscalled linked list or a linear linked list.
CODING
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node*next;
}*head;
void createlist(int n);
void insertNodeAtBeginning(int data);
void displayList();
int main()
{
int n,data;
scanf("%d",&n);
createlist(n);
displayList();
scanf("%d",&data);
insertNodeAtBeginning(data);
return 0;
}
void createlist(int n)
{
struct node*newNode,*temp;
int data,i;
head=(struct node*)malloc(sizeof(struct node));
if(head==NULL)
{
printf("unable to allocate memory");
}
else
{
printf("enter the data of node 1:");
scanf("%d",&data);
head->data=data;
head->next=NULL;
temp=head;
for(i=2;i<=n;i++)
{
newNode=(struct node*)malloc(sizeof(struct node));
if(newNode==NULL)
{
printf("unable to allocate memory");
break;
}
else
{
printf("enter the data of node%d:",i);
scanf("%d",&data);
newNode->data=data;
newNode->next=NULL;
temp->next=newNode;
temp=temp->next;
}
}
OUTPUT
5. AIM: Write a program in C to delete a first node from singly linked list.
LINKED LIST
Link structures are a completely different way to represent a list of items. Each element of
the listis made to point to the next element in the list.
An element in the list is called a
node. The Node have two part’s :
Data and Next
Data parts contain the information about the element.
Next part contains a pointer to the next elements or a node in the list.
A list consisting four names i.e. A, B, C and D is represented using link structures
such a list iscalled linked list or a linear linked list.
ALGORITHM DELNODE ()
{
CODING
#include <stdio.h>
#include <stdlib.h>
struct node
{ int data;
struct node *next;
} *head;
void createList(int n);
void deleteFirstNode();
void displayList();
int main()
{ int n, choice;
printf("Enter the total number of nodes: ");
scanf("%d", &n);
createList(n);
printf("\n Data in the list \n");
displaylist();
printf("\nPress 1 to delete first node: ");
scanf("%d", &choice);
if(choice == 1)
deleteFirstNode();
printf("\nData in the list \n");
displayList();
return 0;
} void createList(int n)
{
struct node *newNode, *temp;
int data, i;
head = (struct node *)malloc(sizeof(struct node));
if (head == NULL)
{ printf("Unable to allocate memory.");
}
else
{
printf("Enter the data of node 1: ");
scanf("%d", &data); head->data = data; head->next = NULL;
temp = head;
for(i=2; i<=n; i++)
{ newNode = (struct node *)malloc(sizeof(struct node)
if(newNode == NULL)
{
printf("Unable to allocate memory.");
break; }
else
{
printf("Enter the data of node %d: ", i);
scanf("%d", &data);
newNode->data = data;
newNode->next = NULL;
temp->next = newNode;
temp = temp->next;
}}
printf("SINGLY LINKED LIST CREATED SUCCESSFULLY\n");
}}
void deleteFirstNode()
{
DEFINITION :-
Stack is a LIFO (last in first out) structure. It is an ordered list of the same type of elements.
A stack is a linear list where all insertions and deletions are permitted only at one end of the
list. When elements are added to the stack it grows at one end. Similarly, when elements are
deleted from a stack, it shrinks at the same end.
Algorithm push() :
{
Step1. if (Top>=N) then
{ print “stack full”;
exist:
}
Step 2. Top=Top+1;
Step3. Stack[Top]=item;
Step4. Stop;
}
Algorithm pop() :
{
Step1. if (Top<0) then
Step 2. Item=Stack[Top];
Top=Top-1;
Step3. Stop;
}
CODING
#include<stdio.h>
int stack[100], choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
int main()
{
clrscr();
top=-1;
printf("\n Enter the size of STACK[MAX=100]:");
scanf("%d",&n);
printf("\n\t STACK OPERATIONS USING ARRAY");
printf("\n\t ");
printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT");
do
{ printf("\n Enter the Choice:");
scanf("%d",&choice);
switch(choice)
{ case 1:
{ push();
break; }
case 2:
{ pop();
break;}
case 3:
{display();
break;}
case 4:
{ printf("\n\t EXIT POINT ");
break;}
default:
{ printf ("\n\t Please Enter a Valid Choice(1/2/3/4)");
}}}
while(choice!=4);
return 0;
} void push()
{ if(top>=n-1)
{printf("\n\tSTACK is over flow");
}else
{ printf(" Enter a value to be pushed:");
scanf("%d",&x);
top++;
stack[top]=x;
}} void pop()
{ if(top<=-1)
{ printf("\n\t Stack is under flow");
} else
{ printf("\n\t The popped elements is %d",stack[top]);
top--;
}}
void display()
{ if(top>=0)
{ printf("\n The elements in STACK \n");
for(i=top; i>=0; i--)
printf("\n%d",stack[i]);
printf("\n Press Next Choice");
} else
}
printf("\n The STACK is empty");
}}
OUTPUT
DEFINITION:-
A queue is a linear data structure that follows the First-In-First-Out (FIFO) principle. This
means that the first element added to the queue will be the first one to be removed.
Algorithms enqueue() :
void enqueue(int element)
{
if (isFull()) {
printf("Error: Queue is full\n");
return;
}
rear = (rear + 1) % MAX_SIZE;
queue[rear] = element;
count++;
}
Algorithms dequeue() :
int dequeue()
{
if (isEmpty())
{
printf("Error: Queue is empty\n");
return -1;
}
int element = queue[front];
front = (front + 1) % MAX_SIZE;
count--;
return element;
}
CODING
#include <stdio.h>
#include<stdlib.h>
#define MAX 50
Void insert();
void delete();
void display();
int queue_array[MAX];
int rear = - 1;
int front = - 1;
int main()
{ int choice;
while (1)
{ printf("1.Insert element to queue n");
printf("2.Delete element from queue n");
printf("3.Display all elements of queue n");
printf("4.Quit n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch(choice)
{ case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("Wrong choice n");
} } } void insert() {
int item; if(rear == MAX - 1)
printf("Queue Overflow n");
else{
if(front== - 1)
front = 0;
printf("Inset the element in queue : ");
rear = rear + 1;
queue_array[rear] = item;
}}
void delete()
{
if(front == -
1 || front >
rear)
{ printf("Queue Underflow n");
return;
} else
{
printf("Element deleted from queue is : %dn", queue_array[front]);
front = front + 1;
} } void
display()
{ int i;
if(front == 1)
printf("Queue
is empty n");
else
{ printf("Queue is : n");
for(i = front; i <= rear; i++)
printf("%d ", queue_array[i]);
printf("n");
}
}
OUTPUT