linkkist
linkkist
linkkist
h>
int stack[100],choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
int main()
{
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");
}
}
#include <stdio.h>
#include <stdlib.h>
newNode->data = data;
newNode->next = NULL;
return newNode;
}
if (!newNode)
return -1;
if (*head == NULL) {
*head = newNode;
return 0;
}
newNode->next = *head;
*head = newNode;
return 0;
}
if (insertBeforeHead(stack, data)) {
printf("Stack Overflow!\n");
}
}
deleteHead(stack);
}
if (!isEmpty(stack))
return (*stack)->data;
else
return -1;
}
int main()
{
push(&stack, 10);
push(&stack, 20);
push(&stack, 30);
push(&stack, 40);
push(&stack, 50);
printf("Stack: ");
printStack(&stack);
pop(&stack);
pop(&stack);
printf("\nStack: ");
printStack(&stack);
return 0;
}
#include <stdio.h>
Int fibonacci(int n) {
If (n <= 1)
Return n;
Else
Int main() {
Int n_terms, I;
Scanf(“%d”, &n_terms);
If (n_terms <= 0) {
} else {
Printf(“Fibonacci sequence:\n”);
Printf(“%d “, fibonacci(i));
Printf(“\n”);
Return 0;
}
#include <stdio.h>
Int factorial(int n) {
If (n == 0 || n == 1) {
} else {
Int main() {
Int num;
Scanf(“%d”, &num);
If (num < 0) {
} else {
Return 0;
}
#include <stdio.h>
If (n == 1) {
Return;
}
Int main() {
Int n;
Scanf(“%d”, &n);
return 0;
}
#include <stdio.h>
Struct Queue {
Int queue[MAX_SIZE];
Int front;
Int rear;
};
q->front = -1;
q->rear = -1;
If (isFull(q)) {
Printf(“Queue is full\n”);
Return;
If (isEmpty(q)) {
q->front = 0;
q->rear++;
q->queue[q->rear] = data;
If (isEmpty(q)) {
Printf(“Queue is empty\n”);
Return -1;
If (q->front == q->rear) {
q->front = -1;
q->rear = -1;
} else {
q->front++;
Return data;
If (isEmpty(q)) {
Printf(“Queue is empty\n”);
Return;
Printf(“%d “, q->queue[i]);
Printf(“\n”);
Int main() {
Struct Queue q;
initializeQueue(&q);
enqueue(&q, 1);
enqueue(&q, 2);
enqueue(&q, 3);
display(&q);
dequeue(&q);
display(&q);
return 0;
}
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
Int data;
} Node;
New_node->data = new_data;
New_node->next = NULL;
Return new_node;
} Queue;
Queue* createQueue()
Queue* q = (Queue*)malloc(sizeof(Queue));
return q;
}
Int isEmpty(Queue* q)
Return 1;
Return 0;
If (q->rear == NULL) {
return;
q->rear->next = new_node;
q->rear = new_node;
Void dequeue(Queue* q)
If (isEmpty(q)) {
Printf(“Queue Underflow\n”);
Return;
}
Node* temp = q->front;
q->front = q->front->next;
If (q->front == NULL)
q->rear = NULL;
Free(temp);
Int getFront(Queue* q)
If (isEmpty(q)) {
Printf(“Queue is empty\n”);
Return INT_MIN;
Return q->front->data;
Int getRear(Queue* q)
If (isEmpty(q)) {
Printf(“Queue is empty\n”);
Return INT_MIN;
Return q->rear->data;
Int main()
{
Queue* q = createQueue();
Enqueue(q, 10);
Enqueue(q, 20);
Dequeue(q);
Dequeue(q);
Enqueue(q, 30);
Enqueue(q, 40);
Enqueue(q, 50);
Dequeue(q);
Return 0;
}
Linear Search
#include<stdio.h>
#include<stdbool.h>
int main(){
int arr[100];
int n ;
printf("Size of Array:");
scanf("%d",&n);
for(int i=0; i<n; i++){
scanf("%d",&arr[i]);
}
for(int i=0; i<n; i++){
printf("%d ",arr[i]);
}
int x=7;
bool flag = false;
for(int i=0; i<n; i++){
if(arr[i]==x)
flag = true;
}
if(flag==true) printf("\n%d Exists",x);
else printf("\n%d Does not Exist",x);
return 0;
}
Output:
Size of Array:5
47382
47382
7 Exists
Binary search
#include<stdio.h>
#include<stdbool.h>
int binarysearch(int arr[] , int n , int keyword){
int start = 0;
int end = n-1;
while(start <= end){
int mid = (start+end)/2;
if(arr[mid]==keyword){
return 1;
}
if(keyword>mid){
start = mid +1;
}
if(keyword<mid){
end = mid;
}
}
return 0;
}
int main(){
int keyword = 5;
int arr[] = {1,2,3,4,5,6};
int index = binarysearch(arr,6,keyword);
if(index){
printf("Keyword is Present");
}
else{
printf("Keyword is Absent");
}
}
Output:
Keyword is Present
Insertion sort
#include<stdio.h>
int main(){
int arr[7] = {7,4,5,9,8,2,1};
int n = 7;
for(int i=1; i<=n-1; i++){
int j=i;
while(j>=1 && arr[j]<arr[j-1]){
int temp = arr[j];
arr[j] = arr[j-1];
arr[j-1] = temp;
j--;
}
}
for(int i=0; i<n; i++){
printf("%d\n",arr[i]);
}
return 0;
}
Output:
1
2
4
5
7
8
9
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
new_node->data = new_data;
new_node->next = *head_ref;
*head_ref = new_node;
new_node->data = new_data;
new_node->next = NULL;
if (*head_ref == NULL) {
*head_ref = new_node;
return;
last = last->next;
last->next = new_node;
*head_ref = temp->next;
free(temp);
return;
prev = temp;
temp = temp->next;
prev->next = temp->next;
free(temp);
node = node->next;
printf("NULL\n");
int main() {
insertAtEnd(&head, 1);
insertAtEnd(&head, 2);
insertAtEnd(&head, 3);
insertAtBeginning(&head, 0);
traverse(head);
deleteNode(&head, 2);
traverse(head);
return 0;
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
newNode->data = data;
newNode->next = NULL;
newNode->prev = NULL;
return newNode;
if (head == NULL) {
head = newNode;
return;
newNode->next = head;
head->prev = newNode;
head = newNode;
if (head == NULL) {
head = newNode;
return;
temp = temp->next;
temp->next = newNode;
newNode->prev = temp;
head = temp->next;
if (head != NULL)
head->prev = NULL;
free(temp);
return;
temp = temp->next;
if (temp == NULL)
return;
if (temp->next != NULL)
temp->next->prev = temp->prev;
if (temp->prev != NULL)
temp->prev->next = temp->next;
free(temp);
void traverse() {
temp = temp->next;
printf("\n");
int main() {
insertAtBeginning(10);
insertAtEnd(20);
insertAtBeginning(5);
traverse();
deleteNode(10);
traverse();
deleteNode(5);
traverse();
return 0;