Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 335cdc7520 | |||
| 26eccbe41f | |||
| 0c44ab0767 | |||
| 6cefe4c8d0 | |||
| cd929624f3 | |||
| c655dd624f | |||
| 6925ae0dca | |||
| 5e37c1d12b | |||
| 9efbd42b29 |
23
Array.c
Normal file
23
Array.c
Normal file
@@ -0,0 +1,23 @@
|
||||
// WAP to print out an array of dynamic values.
|
||||
|
||||
void main() {
|
||||
int r,n,i,a[10];
|
||||
|
||||
// To set range of array:
|
||||
printf("\n Enter the range of array: \n");
|
||||
scanf("%d", &n);
|
||||
|
||||
// To get values of array:
|
||||
printf("\n Enter the elements of the array: \n");
|
||||
r = 0;
|
||||
for (i=0 ; i<=n; i++) {
|
||||
scanf("%d", &a[i]);
|
||||
}
|
||||
|
||||
// To print the elements of the array:
|
||||
printf("\n The elements of the array are: \n");
|
||||
for (i=0 ; i<=n; i++) {
|
||||
r++;
|
||||
printf("\n a[%d] = %d \n", r, a[i]);
|
||||
}
|
||||
}
|
||||
24
ArrayReverse.c
Normal file
24
ArrayReverse.c
Normal file
@@ -0,0 +1,24 @@
|
||||
// WAP to print out an array of dynamic values in rverse order.
|
||||
|
||||
void main() {
|
||||
int i, r, a[10], n;
|
||||
|
||||
// To set range of array:
|
||||
printf("\n Enter the range of array: \n");
|
||||
scanf("%d", &n);
|
||||
|
||||
// To get values of array:
|
||||
printf("\n Enter the elements of the array: \n");
|
||||
n-1;
|
||||
r = n;
|
||||
for (i=0 ; i<=n; i++) {
|
||||
scanf("%d", &a[i]);
|
||||
}
|
||||
|
||||
// To print the elements of the array:
|
||||
printf("\n The elements of the array are: \n");
|
||||
for (i=n ; i>=0; i--) {
|
||||
r--;
|
||||
printf("\n a[%d] = %d \n", r, a[i]);
|
||||
}
|
||||
}
|
||||
BIN
ArrayReverse.exe
Normal file
BIN
ArrayReverse.exe
Normal file
Binary file not shown.
37
BinarySearch.c
Normal file
37
BinarySearch.c
Normal file
@@ -0,0 +1,37 @@
|
||||
// WAP to search an element in an array using Binary Search.
|
||||
|
||||
void main() {
|
||||
int a[10], n, item, beg, mid, end, i;
|
||||
|
||||
// To set range of array:
|
||||
printf("\n Enter the range of array: \n");
|
||||
scanf("%d", &n);
|
||||
|
||||
// To get values of array:
|
||||
printf("\n Enter the elements of the array: \n");
|
||||
for (i=0 ; i<=n; i++) {
|
||||
scanf("%d", &a[i]);
|
||||
}
|
||||
|
||||
// Binary Search Algorithm:
|
||||
printf("\n Enter item to search: \n");
|
||||
scanf("%d", &item);
|
||||
beg = 0;
|
||||
end = n-1;
|
||||
mid = (beg + end)/2;
|
||||
while ((beg<=end)&&(a[mid]!=item)) {
|
||||
if (item<a[mid]) {
|
||||
end = mid - 1;
|
||||
}
|
||||
else {
|
||||
beg = mid + 1;
|
||||
}
|
||||
mid = (beg + end)/2;
|
||||
}
|
||||
if (a[mid]==item) {
|
||||
printf("\n Item found at location: %d", mid + 1);
|
||||
}
|
||||
else {
|
||||
printf("\n Item doesn't exist.");
|
||||
}
|
||||
}
|
||||
BIN
BinarySearch.exe
Normal file
BIN
BinarySearch.exe
Normal file
Binary file not shown.
38
BubbleSort.c
Normal file
38
BubbleSort.c
Normal file
@@ -0,0 +1,38 @@
|
||||
// WAP to sort an array using bubble sort.
|
||||
|
||||
void main() {
|
||||
int array[10], n, c, d, swap;
|
||||
|
||||
// To set range of array:
|
||||
printf("\n Enter the range of array: \n");
|
||||
scanf("%d", &n);
|
||||
|
||||
// To get values of array:
|
||||
printf("\n Enter the elements of the array: \n");
|
||||
for (c=0 ; c<=n; c++) {
|
||||
scanf("%d \n", &array[c]);
|
||||
}
|
||||
|
||||
// To print the elements of the insorted array:
|
||||
printf("\n The elements of the unsorted array are: \n");
|
||||
for (c=0 ; c<=n; c++) {
|
||||
printf("%d \n", array[c]);
|
||||
}
|
||||
|
||||
// Bubble Ssort algorithm:
|
||||
for (c=0 ; c<=n-1; c++) {
|
||||
for (d=0; d<n-c-1; d++) {
|
||||
if (array[d] > array[d+1]) {
|
||||
swap = array[d+1];
|
||||
array[d] = array[d+1];
|
||||
array[d+1] = swap;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// To print the elements of the sorted array:
|
||||
printf("\n The elements of the sorted array are: \n");
|
||||
for (c=0 ; c<=n; c++) {
|
||||
printf("\n %d \n", array[c]);
|
||||
}
|
||||
}
|
||||
BIN
BubbleSort.exe
Normal file
BIN
BubbleSort.exe
Normal file
Binary file not shown.
28
LinearSearch.c
Normal file
28
LinearSearch.c
Normal file
@@ -0,0 +1,28 @@
|
||||
// WAP to search an element in an array using Linear Search.
|
||||
|
||||
void main() {
|
||||
int a[10], i, item, n;
|
||||
|
||||
// To set range of array:
|
||||
printf("\n Enter the range of array: \n");
|
||||
scanf("%d", &n);
|
||||
|
||||
// To get values of array:
|
||||
printf("\n Enter the elements of the array: \n");
|
||||
for (i=0 ; i<=n; i++) {
|
||||
scanf("%d", &a[i]);
|
||||
}
|
||||
|
||||
// Liner Search Algorithm:
|
||||
printf("\n Enter item to search: \n");
|
||||
scanf("%d", &item);
|
||||
for (i=0; i<=n; i++) {
|
||||
if (item==a[i]) {
|
||||
printf("\n Item found at location %d", i+1);
|
||||
break;
|
||||
}
|
||||
if (i>n) {
|
||||
printf("\n Item does not exist.");
|
||||
}
|
||||
}
|
||||
}
|
||||
BIN
LinearSearch.exe
Normal file
BIN
LinearSearch.exe
Normal file
Binary file not shown.
@@ -24,3 +24,12 @@
|
||||
|22 | Calculator.c | Calculator.exe | Write a menu driven program for calculator |
|
||||
|23 | MultiplicationTable.c | MultiplicationTable.exe | WAP to print the multiplication table for a given number |
|
||||
|24 | Divisible.c | Divisible.exe | WAP to find an addition of numbers which are >30, <100 & divisible by 7 |
|
||||
|25 | Array.c | Array.exe | WAP to print out an array of dynamic values |
|
||||
|26 | ArrayReverse.c | ArrayReverse.exe | WAP to print out an array of dynamic values in reverse order |
|
||||
|27 | BubbleSort.c | BubbleSort.exe | WAP to sort an array using bubble sort |
|
||||
|28 | LinearSearch.c | LinearSearch.exe | WAP to search an element in an array using Linear Search |
|
||||
|29 | BinarySearch.c | BinarySearch.exe | WAP to search an element in an array using Binary Search |
|
||||
|30 | SelectionSort.c | SelectionSort.exe | WAP to sort an array using selection sort |
|
||||
|31 | StackUsingLinkedList.c | StackUsingLinkedList.exe | WAP to Implement a Stack using Linked List |
|
||||
|32 | SinglyLinkedList.c | SinglyLinkedList.exe | WAP to implement singly linked lists |
|
||||
|33 | QueueUsingLinkedList.c | QueueUsingLinkedList.exe | WAP to implement a Queue using Linked List |
|
||||
106
QueueUsingLinkedList.c
Normal file
106
QueueUsingLinkedList.c
Normal file
@@ -0,0 +1,106 @@
|
||||
// WAP to implement Queue using linked list.
|
||||
|
||||
#include<stdio.h>
|
||||
#include<stdlib.h>
|
||||
|
||||
struct node
|
||||
{
|
||||
int data;
|
||||
struct node *next;
|
||||
};
|
||||
struct node *front;
|
||||
struct node *rear;
|
||||
void insert();
|
||||
void delete();
|
||||
void display();
|
||||
void main ()
|
||||
{
|
||||
int choice;
|
||||
while(choice != 4)
|
||||
{
|
||||
printf("\n*************************Main Menu*****************************\n");
|
||||
printf("\n=================================================================\n");
|
||||
printf("\n1.insert an element\n2.Delete an element\n3.Display the queue\n4.Exit\n");
|
||||
printf("\nEnter your choice ?");
|
||||
scanf("%d",& choice);
|
||||
switch(choice)
|
||||
{
|
||||
case 1:
|
||||
insert();
|
||||
break;
|
||||
case 2:
|
||||
delete();
|
||||
break;
|
||||
case 3:
|
||||
display();
|
||||
break;
|
||||
case 4:
|
||||
exit(0);
|
||||
break;
|
||||
default:
|
||||
printf("\nEnter valid choice??\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
void insert()
|
||||
{
|
||||
struct node *ptr;
|
||||
int item;
|
||||
ptr = (struct node *) malloc (sizeof(struct node));
|
||||
if(ptr == NULL)
|
||||
{
|
||||
printf("\nOVERFLOW\n");
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("\nEnter value?\n");
|
||||
scanf("%d",&item);
|
||||
ptr -> data = item;
|
||||
if(front == NULL)
|
||||
{
|
||||
front = ptr;
|
||||
rear = ptr;
|
||||
front -> next = NULL;
|
||||
rear -> next = NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
rear -> next = ptr;
|
||||
rear = ptr;
|
||||
rear->next = NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
void delete ()
|
||||
{
|
||||
struct node *ptr;
|
||||
if(front == NULL)
|
||||
{
|
||||
printf("\nUNDERFLOW\n");
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
ptr = front;
|
||||
front = front -> next;
|
||||
free(ptr);
|
||||
}
|
||||
}
|
||||
void display()
|
||||
{
|
||||
struct node *ptr;
|
||||
ptr = front;
|
||||
if(front == NULL)
|
||||
{
|
||||
printf("\nEmpty queue\n");
|
||||
}
|
||||
else
|
||||
{ printf("\nprinting values .....\n");
|
||||
while(ptr != NULL)
|
||||
{
|
||||
printf("\n%d\n",ptr -> data);
|
||||
ptr = ptr -> next;
|
||||
}
|
||||
}
|
||||
}
|
||||
BIN
QueueUsingLinkedList.exe
Normal file
BIN
QueueUsingLinkedList.exe
Normal file
Binary file not shown.
@@ -1,6 +1,6 @@
|
||||
# C Programs For Everybody To Try!
|
||||
|
||||
_`Last Updated: April 25' 2021`_
|
||||
_`Last Updated: June 11' 2021`_
|
||||
|
||||
## Introduction:
|
||||
|
||||
|
||||
38
SelectionSort.c
Normal file
38
SelectionSort.c
Normal file
@@ -0,0 +1,38 @@
|
||||
// WAP to sort an array using selection sort.
|
||||
|
||||
void swap(int *xp, int *yp) {
|
||||
int temp = *xp;
|
||||
*xp = *yp;
|
||||
*yp = temp;
|
||||
}
|
||||
|
||||
void main() {
|
||||
int a[10], n, min, i, j;
|
||||
|
||||
// To set range of array:
|
||||
printf("\n Enter the range of array: \n");
|
||||
scanf("%d", &n);
|
||||
|
||||
// To get values of array:
|
||||
printf("\n Enter the elements of the array: \n");
|
||||
for (i=0 ; i<=n; i++) {
|
||||
scanf("%d", &a[i]);
|
||||
}
|
||||
|
||||
// Selection Sort Algorithm:
|
||||
for (i=0; i<n; i++) {
|
||||
min = i;
|
||||
for (j=i+1; j<n; j++) {
|
||||
min = j;
|
||||
}
|
||||
if (min != i) {
|
||||
swap(&a[i], &a[min]);
|
||||
}
|
||||
}
|
||||
|
||||
// Sorted array print:
|
||||
printf("\nThe sorted array is: \n");
|
||||
for (i=0 ; i<=n; i++) {
|
||||
printf("%d \n", &a[i]);
|
||||
}
|
||||
}
|
||||
BIN
SelectionSort.exe
Normal file
BIN
SelectionSort.exe
Normal file
Binary file not shown.
54
SinglyLinkedList.c
Normal file
54
SinglyLinkedList.c
Normal file
@@ -0,0 +1,54 @@
|
||||
// WAP to implement singly linked lists
|
||||
|
||||
#include<stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
struct node {
|
||||
int data;
|
||||
struct node *next;
|
||||
};
|
||||
|
||||
struct node *head = NULL;
|
||||
struct node *current = NULL;
|
||||
|
||||
//display the list
|
||||
void printList() {
|
||||
|
||||
struct node *ptr = head;
|
||||
|
||||
printf("\n[head] =>");
|
||||
//start from the beginning
|
||||
while(ptr != NULL) {
|
||||
printf(" %d =>",ptr->data);
|
||||
ptr = ptr->next;
|
||||
}
|
||||
|
||||
printf(" [null]\n");
|
||||
}
|
||||
|
||||
//insert link at the first location
|
||||
void insert(int data) {
|
||||
//create a link
|
||||
struct node *link = (struct node*) malloc(sizeof(struct node));
|
||||
|
||||
//link->key = key;
|
||||
link->data = data;
|
||||
|
||||
//point it to old first node
|
||||
link->next = head;
|
||||
|
||||
//point first to new first node
|
||||
head = link;
|
||||
}
|
||||
|
||||
int main() {
|
||||
insert(10);
|
||||
insert(20);
|
||||
insert(30);
|
||||
insert(1);
|
||||
insert(40);
|
||||
insert(56);
|
||||
|
||||
printList();
|
||||
return 0;
|
||||
}
|
||||
BIN
SinglyLinkedList.exe
Normal file
BIN
SinglyLinkedList.exe
Normal file
Binary file not shown.
182
StackUsingLinkedList.c
Normal file
182
StackUsingLinkedList.c
Normal file
@@ -0,0 +1,182 @@
|
||||
// WAP to Implement a Stack using Linked List
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
struct node
|
||||
{
|
||||
int info;
|
||||
struct node *ptr;
|
||||
}*top,*top1,*temp;
|
||||
|
||||
int topelement();
|
||||
void push(int data);
|
||||
void pop();
|
||||
void empty();
|
||||
void display();
|
||||
void destroy();
|
||||
void stack_count();
|
||||
void create();
|
||||
|
||||
int count = 0;
|
||||
|
||||
void main()
|
||||
{
|
||||
int no, ch, e;
|
||||
|
||||
printf("\n 1 - Push");
|
||||
printf("\n 2 - Pop");
|
||||
printf("\n 3 - Top");
|
||||
printf("\n 4 - Empty");
|
||||
printf("\n 5 - Exit");
|
||||
printf("\n 6 - Dipslay");
|
||||
printf("\n 7 - Stack Count");
|
||||
printf("\n 8 - Destroy stack");
|
||||
|
||||
create();
|
||||
|
||||
while (1)
|
||||
{
|
||||
printf("\n Enter choice : ");
|
||||
scanf("%d", &ch);
|
||||
|
||||
switch (ch)
|
||||
{
|
||||
case 1:
|
||||
printf("Enter data : ");
|
||||
scanf("%d", &no);
|
||||
push(no);
|
||||
break;
|
||||
case 2:
|
||||
pop();
|
||||
break;
|
||||
case 3:
|
||||
if (top == NULL)
|
||||
printf("No elements in stack");
|
||||
else
|
||||
{
|
||||
e = topelement();
|
||||
printf("\n Top element : %d", e);
|
||||
}
|
||||
break;
|
||||
case 4:
|
||||
empty();
|
||||
break;
|
||||
case 5:
|
||||
exit(0);
|
||||
case 6:
|
||||
display();
|
||||
break;
|
||||
case 7:
|
||||
stack_count();
|
||||
break;
|
||||
case 8:
|
||||
destroy();
|
||||
break;
|
||||
default :
|
||||
printf(" Wrong choice, Please enter correct choice ");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Create empty stack */
|
||||
void create()
|
||||
{
|
||||
top = NULL;
|
||||
}
|
||||
|
||||
/* Count stack elements */
|
||||
void stack_count()
|
||||
{
|
||||
printf("\n No. of elements in stack : %d", count);
|
||||
}
|
||||
|
||||
/* Push data into stack */
|
||||
void push(int data)
|
||||
{
|
||||
if (top == NULL)
|
||||
{
|
||||
top =(struct node *)malloc(1*sizeof(struct node));
|
||||
top->ptr = NULL;
|
||||
top->info = data;
|
||||
}
|
||||
else
|
||||
{
|
||||
temp =(struct node *)malloc(1*sizeof(struct node));
|
||||
temp->ptr = top;
|
||||
temp->info = data;
|
||||
top = temp;
|
||||
}
|
||||
count++;
|
||||
}
|
||||
|
||||
/* Display stack elements */
|
||||
void display()
|
||||
{
|
||||
top1 = top;
|
||||
|
||||
if (top1 == NULL)
|
||||
{
|
||||
printf("Stack is empty");
|
||||
return;
|
||||
}
|
||||
|
||||
while (top1 != NULL)
|
||||
{
|
||||
printf("%d ", top1->info);
|
||||
top1 = top1->ptr;
|
||||
}
|
||||
}
|
||||
|
||||
/* Pop Operation on stack */
|
||||
void pop()
|
||||
{
|
||||
top1 = top;
|
||||
|
||||
if (top1 == NULL)
|
||||
{
|
||||
printf("\n Error : Trying to pop from empty stack");
|
||||
return;
|
||||
}
|
||||
else
|
||||
top1 = top1->ptr;
|
||||
printf("\n Popped value : %d", top->info);
|
||||
free(top);
|
||||
top = top1;
|
||||
count--;
|
||||
}
|
||||
|
||||
/* Return top element */
|
||||
int topelement()
|
||||
{
|
||||
return(top->info);
|
||||
}
|
||||
|
||||
/* Check if stack is empty or not */
|
||||
void empty()
|
||||
{
|
||||
if (top == NULL)
|
||||
printf("\n Stack is empty");
|
||||
else
|
||||
printf("\n Stack is not empty with %d elements", count);
|
||||
}
|
||||
|
||||
/* Destroy entire stack */
|
||||
void destroy()
|
||||
{
|
||||
top1 = top;
|
||||
|
||||
while (top1 != NULL)
|
||||
{
|
||||
top1 = top->ptr;
|
||||
free(top);
|
||||
top = top1;
|
||||
top1 = top1->ptr;
|
||||
}
|
||||
free(top1);
|
||||
top = NULL;
|
||||
|
||||
printf("\n All stack elements destroyed");
|
||||
count = 0;
|
||||
}
|
||||
BIN
StackUsingLinkedList.exe
Normal file
BIN
StackUsingLinkedList.exe
Normal file
Binary file not shown.
Reference in New Issue
Block a user