Program
function[]=back(X,Y,x0)
n=length(X)
h=X(2)-X(1);
for i=1:n
Z(i)=Y(i);
d(i,1)=Y(i);
end
for i=1:n-1
for j=1:n-i
d(i,j)=Z(j+1)-Z(j);
D(j,i+1)=d(i,j);
end
for k=1:n-i
Z(k)=d(i,k);
end
d(i,n)=Z(k);
end
printf('difference table is')
disp(D)
y_x=Y(n);
p=(x0-X(n))/h;
for i=1:n-1
pp=1;
for j=1:i
pp=pp*(p+(j-1));
end
y_x=y_x+((pp*d(i,n))/factorial(i))
end
printf('value of function at %.4f is %.4f',x0,y_x)
endfunction
Output:
Warning : redefining function: back . Use funcprot(0) to avoid this message
exec('C:\Users\acer\back.sce', -1)
x=(100:50:300);
y=[958 917 865 799 712];
back(x,y,275)
difference table is
0. -41. -11. -3. -4.
0. -52. -14. -7. 0.
0. -66. -21. 0. 0.
0. -87. 0. 0. 0.
value of function at 275.0000 is 758.7188
function[]=back(X,Y,x0)
n=length(X)
h=X(2)-X(1);
for i=1:n
Z(i)=Y(i);
d(i,1)=Y(i);
end
for i=1:n-1
for j=1:n-i
d(i,j)=Z(j+1)-Z(j);
D(j,i+1)=d(i,j);
end
for k=1:n-i
Z(k)=d(i,k);
end
d(i,n)=Z(k);
end
printf('difference table is')
disp(D)
y_x=Y(n);
p=(x0-X(n))/h;
for i=1:n-1
pp=1;
for j=1:i
pp=pp*(p+(j-1));
end
y_x=y_x+((pp*d(i,n))/factorial(i))
end
printf('value of function at %.4f is %.4f',x0,y_x)
endfunction
Output:
Warning : redefining function: back . Use funcprot(0) to avoid this message
exec('C:\Users\acer\back.sce', -1)
x=(100:50:300);
y=[958 917 865 799 712];
back(x,y,275)
difference table is
0. -41. -11. -3. -4.
0. -52. -14. -7. 0.
0. -66. -21. 0. 0.
0. -87. 0. 0. 0.
value of function at 275.0000 is 758.7188


8 Comments
// Practical 6: Implement singly linked with the following operations - insert at beginning, end and at a given position.
ReplyDelete#include iostream.h
#include conio.h
#include stdlib.h
struct Node
{
int data;
struct Node *next;
};
void insertAtBeginning(struct Node *&head, int value)
{
struct Node *newNode;
newNode = (struct Node *)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = head;
head = newNode;
}
void insertAtEnd(struct Node *&head, int value)
{
struct Node *newNode, *temp;
newNode = (struct Node *)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;
if (head == NULL)
{
head = newNode;
return;
}
temp = head;
while (temp->next != NULL)
temp = temp->next;
temp->next = newNode;
}
void insertAtPosition(struct Node *&head, int value, int position)
{
int i;
struct Node *newNode, *temp;
if (position <= 1)
{
insertAtBeginning(head, value);
return;
}
newNode = (struct Node *)malloc(sizeof(struct Node));
newNode->data = value;
temp = head;
for (i = 1; i < position - 1 && temp != NULL; i++)
temp = temp->next;
if (temp == NULL)
{
cout << "Position out of bounds. Inserting at end." << endl;
insertAtEnd(head, value);
free(newNode);
return;
}
newNode->next = temp->next;
temp->next = newNode;
}
void display(struct Node *head)
{
struct Node *temp;
if (head == NULL)
{
cout << "List is empty." << endl;
return;
}
temp = head;
while (temp != NULL)
{
cout << temp->data << " -> ";
temp = temp->next;
}
cout << "NULL" << endl;
}
void main()
{
struct Node *head = NULL;
int choice, value, pos;
clrscr();
while (1)
{
cout << "\nMenu:\n";
cout << "1. Insert at Beginning\n";
cout << "2. Insert at End\n";
cout << "3. Insert at Position\n";
cout << "4. Display\n";
cout << "5. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice)
{
case 1:
cout << "Enter value to insert at beginning: ";
cin >> value;
insertAtBeginning(head, value);
break;
case 2:
cout << "Enter value to insert at end: ";
cin >> value;
insertAtEnd(head, value);
break;
case 3:
cout << "Enter value to insert: ";
cin >> value;
cout << "Enter position: ";
cin >> pos;
insertAtPosition(head, value, pos);
break;
case 4:
display(head);
break;
case 5:
cout << "Exiting...";
getch();
return;
default:
cout << "Invalid choice!";
}
}
}
// Practical 7: Implement doubly linked list with insertion, deletion and traversal operations in both directions.
ReplyDelete#include iostream.h
#include conio.h
#include stdlib.h
struct Node
{
int data;
struct Node *prev;
struct Node *next;
};
struct Node *head = NULL;
void insertEnd(int value)
{
struct Node *newNode, *temp;
newNode = (struct Node *)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;
newNode->prev = NULL;
if (head == NULL)
{
head = newNode;
return;
}
temp = head;
while (temp->next != NULL)
temp = temp->next;
temp->next = newNode;
newNode->prev = temp;
}
void deleteNode(int value)
{
struct Node *temp;
temp = head;
while (temp != NULL && temp->data != value)
temp = temp->next;
if (temp == NULL)
return;
if (temp->prev != NULL)
temp->prev->next = temp->next;
else
head = temp->next;
if (temp->next != NULL)
temp->next->prev = temp->prev;
free(temp);
}
void traverseForward()
{
struct Node *temp = head;
while (temp != NULL)
{
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
void traverseBackward()
{
struct Node *temp = head;
if (temp == NULL)
return;
while (temp->next != NULL)
temp = temp->next;
while (temp != NULL)
{
cout << temp->data << " ";
temp = temp->prev;
}
cout << endl;
}
void main()
{
clrscr();
insertEnd(10);
insertEnd(20);
insertEnd(30);
cout << "Forward: ";
traverseForward();
cout << "Backward: ";
traverseBackward();
deleteNode(20);
cout << "After deletion (forward): ";
traverseForward();
cout << "After deletion (backward): ";
traverseBackward();
getch();
}
// Practical 8: Implement circular linked list with insertion, deletion operations.
ReplyDelete#include iostream.h
#include conio.h
#include stdlib.h
struct Node
{
int data;
struct Node *next;
};
struct Node* createNode(int data)
{
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
void insertAtEnd(struct Node* &head, int data)
{
struct Node *newNode, *temp;
newNode = createNode(data);
if (head == NULL)
{
head = newNode;
newNode->next = head;
}
else
{
temp = head;
while (temp->next != head)
temp = temp->next;
temp->next = newNode;
newNode->next = head;
}
}
void deleteNode(struct Node* &head, int key)
{
struct Node *temp, *prev;
if (head == NULL)
{
cout << "List is empty." << endl;
return;
}
temp = head;
prev = NULL;
do
{
if (temp->data == key)
{
if (temp == head && temp->next == head)
{
free(head);
head = NULL;
}
else if (temp == head)
{
while (temp->next != head)
temp = temp->next;
temp->next = head->next;
prev = head;
head = head->next;
free(prev);
}
else
{
prev->next = temp->next;
free(temp);
}
return;
}
prev = temp;
temp = temp->next;
} while (temp != head);
cout << "Key not found." << endl;
}
void displayList(struct Node* head)
{
struct Node *temp;
if (head == NULL)
{
cout << "List is empty." << endl;
return;
}
temp = head;
do
{
cout << temp->data << " ";
temp = temp->next;
} while (temp != head);
cout << endl;
}
void main()
{
struct Node *head = NULL;
clrscr();
insertAtEnd(head, 10);
insertAtEnd(head, 20);
insertAtEnd(head, 30);
cout << "Circular Linked List: ";
displayList(head);
deleteNode(head, 20);
cout << "After deleting 20: ";
displayList(head);
getch();
}
/* Bubble Sort */
ReplyDelete#include iostream.h
#include conio.h
void main()
{
int n, i, j, temp;
int a[100];
clrscr();
cout << "Enter number of elements: ";
cin >> n;
cout << "Enter " << n << " elements:\n";
for (i = 0; i < n; i++)
{
cin >> a[i];
}
for (i = 0; i < n - 1; i++)
{
for (j = 0; j < n - i - 1; j++)
{
if (a[j] > a[j + 1])
{
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
cout << "Sorted array: ";
for (i = 0; i < n; i++)
{
cout << a[i] << " ";
}
getch();
}
/* Selection Sort */
ReplyDelete#include iostream.h
#include conio.h
void main()
{
int n, i, j, minIndex, temp;
int a[100];
clrscr();
cout << "Enter number of elements: ";
cin >> n;
cout << "Enter " << n << " elements:\n";
for (i = 0; i < n; i++)
{
cin >> a[i];
}
for (i = 0; i < n - 1; i++)
{
minIndex = i;
for (j = i + 1; j < n; j++)
{
if (a[j] < a[minIndex])
{
minIndex = j;
}
}
temp = a[i];
a[i] = a[minIndex];
a[minIndex] = temp;
}
cout << "Sorted array: ";
for (i = 0; i < n; i++)
{
cout << a[i] << " ";
}
getch();
}
/* Insertion Sort */
ReplyDelete#include iostream.h
#include conio.h
void main()
{
int n, i, j, key;
int a[100];
clrscr();
cout << "Enter number of elements: ";
cin >> n;
cout << "Enter " << n << " elements:\n";
for (i = 0; i < n; i++)
{
cin >> a[i];
}
for (i = 1; i < n; i++)
{
key = a[i];
j = i - 1;
while (j >= 0 && a[j] > key)
{
a[j + 1] = a[j];
j--;
}
a[j + 1] = key;
}
cout << "Sorted array: ";
for (i = 0; i < n; i++)
{
cout << a[i] << " ";
}
getch();
}
// Merge
ReplyDelete#include iostream.h
#include conio.h
#define MAX 100
void merge(int arr[], int left, int mid, int right)
{
int i, j, k;
int L[MAX], R[MAX];
int n1 = mid - left + 1;
int n2 = right - mid;
for (i = 0; i < n1; i++)
L[i] = arr[left + i];
for (j = 0; j < n2; j++)
R[j] = arr[mid + 1 + j];
i = 0;
j = 0;
k = left;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
arr[k++] = L[i++];
else
arr[k++] = R[j++];
}
while (i < n1)
arr[k++] = L[i++];
while (j < n2)
arr[k++] = R[j++];
}
void mergeSort(int arr[], int left, int right)
{
int mid;
if (left < right)
{
mid = (left + right) / 2;
mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);
merge(arr, left, mid, right);
}
}
void main()
{
int arr[7] = {64, 34, 25, 12, 22, 11, 90};
int n = 7;
int i;
clrscr();
mergeSort(arr, 0, n - 1);
cout << "Sorted array: ";
for (i = 0; i < n; i++)
cout << arr[i] << " ";
getch();
}
// Quick
ReplyDelete#include iostream.h
#include conio.h
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
int partition(int arr[], int low, int high)
{
int pivot, i, j;
pivot = arr[high];
i = low - 1;
for (j = low; j < high; j++)
{
if (arr[j] < pivot)
{
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return i + 1;
}
void quickSort(int arr[], int low, int high)
{
int pi;
if (low < high)
{
pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
void main()
{
int arr[7] = {64, 34, 25, 12, 22, 11, 90};
int n = 7;
int i;
clrscr();
quickSort(arr, 0, n - 1);
cout << "Sorted array: ";
for (i = 0; i < n; i++)
cout << arr[i] << " ";
getch();
}