0% found this document useful (0 votes)
33 views29 pages

Linkkist

Uploaded by

csaiml23094
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views29 pages

Linkkist

Uploaded by

csaiml23094
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

#include<stdio.

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 [Link]\n\t [Link]\n\t [Link]\n\t [Link]");
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>

typedef struct Node {


int data;
struct Node* next;
} node;

node* createNode(int data)


{

node* newNode = (node*)malloc(sizeof(node));


if (newNode == NULL)
return NULL;

newNode->data = data;
newNode->next = NULL;
return newNode;
}

int insertBeforeHead(node** head, int data)


{

node* newNode = createNode(data);

if (!newNode)
return -1;

if (*head == NULL) {
*head = newNode;
return 0;
}

newNode->next = *head;
*head = newNode;
return 0;
}

int deleteHead(node** head)


{

node* temp = *head;


*head = (*head)->next;
free(temp);
return 0;
}

int isEmpty(node** stack) { return *stack == NULL; }


void push(node** stack, int data)
{

if (insertBeforeHead(stack, data)) {
printf("Stack Overflow!\n");
}
}

int pop(node** stack)


{
if (isEmpty(stack)) {
printf("Stack Underflow\n");
return -1;
}

deleteHead(stack);
}

int peek(node** stack)


{

if (!isEmpty(stack))
return (*stack)->data;
else
return -1;
}

void printStack(node** stack)


{
node* temp = *stack;
while (temp != NULL) {
printf("%d-> ", temp->data);
temp = temp->next;
}
printf("\n");
}

int main()
{

node* stack = NULL;

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

Return (fibonacci(n – 1) + fibonacci(n – 2));

Int main() {

Int n_terms, I;

Printf(“Enter the number of terms: “);

Scanf(“%d”, &n_terms);

If (n_terms <= 0) {

Printf(“Please enter a positive integer\n”);

} else {

Printf(“Fibonacci sequence:\n”);

For (I = 0; I < n_terms; i++) {

Printf(“%d “, fibonacci(i));

Printf(“\n”);

Return 0;

}
#include <stdio.h>

Int factorial(int n) {

If (n == 0 || n == 1) {

Return 1; // Base case: factorial of 0 or 1 is 1

} else {

Return n * factorial(n – 1); // Recursive call

Int main() {

Int num;

Printf(“Enter a number: “);

Scanf(“%d”, &num);

If (num < 0) {

Printf(“Factorial is not defined for negative numbers.\n”);

} else {

Printf(“Factorial of %d is %d\n”, num, factorial(num));

Return 0;
}

#include <stdio.h>

Void towerOfHanoi(int n, char source, char destination, char auxiliary) {

If (n == 1) {

Printf(“Move disk 1 from %c to %c\n”, source, destination);

Return;

towerOfHanoi(n – 1, source, auxiliary, destination);

Printf(“Move disk %d from %c to %c\n”, n, source, destination);

towerOfHanoi(n – 1, auxiliary, destination, source);

}
Int main() {

Int n;

Printf(“Enter the number of disks: “);

Scanf(“%d”, &n);

towerOfHanoi(n, ‘A’, ‘C’, ‘B’);

return 0;

}
#include <stdio.h>

#define MAX_SIZE 100

Struct Queue {

Int queue[MAX_SIZE];

Int front;

Int rear;

};

Void initializeQueue(struct Queue *q) {

q->front = -1;

q->rear = -1;

Int isEmpty(struct Queue *q) {

Return (q->front == -1);

Int isFull(struct Queue *q) {

Return (q->rear == MAX_SIZE – 1);

Void enqueue(struct Queue *q, int data) {

If (isFull(q)) {

Printf(“Queue is full\n”);

Return;

If (isEmpty(q)) {

q->front = 0;

q->rear++;

q->queue[q->rear] = data;

printf(“Enqueued %d in queue\n”, data);


}

Int dequeue(struct Queue *q) {

If (isEmpty(q)) {

Printf(“Queue is empty\n”);

Return -1;

Int data = q->queue[q->front];

// If the queue is empty reset the pointers

If (q->front == q->rear) {

q->front = -1;

q->rear = -1;

} else {

q->front++;

Printf(“Deleted element: %d\n”, data);

Return data;

Void display(struct Queue *q) {

If (isEmpty(q)) {

Printf(“Queue is empty\n”);

Return;

For (int I = q->front; I <= q->rear; i++) {

Printf(“%d “, q->queue[i]);

Printf(“\n”);

Int main() {
Struct Queue q;

initializeQueue(&q);

enqueue(&q, 1);

enqueue(&q, 2);

enqueue(&q, 3);

printf(“Elements in the queue after enqueue operation: “);

display(&q);

dequeue(&q);

printf(“Elements in the queue after dequeue operation: “);

display(&q);

return 0;

}
#include <limits.h>

#include <stdio.h>

#include <stdlib.h>

Typedef struct Node {

Int data;

Struct Node* next;

} Node;

Node* createNode(int new_data)

Node* new_node = (Node*)malloc(sizeof(Node));

New_node->data = new_data;

New_node->next = NULL;

Return new_node;

Typedef struct Queue {

Node *front, *rear;

} Queue;

Queue* createQueue()

Queue* q = (Queue*)malloc(sizeof(Queue));

q->front = q->rear = NULL;

return q;

}
Int isEmpty(Queue* q)

If (q->front == NULL && q->rear == NULL) {

Return 1;

Return 0;

Void enqueue(Queue* q, int new_data)

Node* new_node = createNode(new_data);

If (q->rear == NULL) {

q->front = q->rear = new_node;

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);

Printf(“Queue Front: %d\n”, getFront(q));

Printf(“Queue Rear: %d\n”, getRear(q));

Dequeue(q);

Dequeue(q);

Enqueue(q, 30);

Enqueue(q, 40);

Enqueue(q, 50);

Dequeue(q);

Printf(“Queue Front: %d\n”, getFront(q));

Printf(“Queue Rear: %d\n”, getRear(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;

struct Node* next;

};

void insertAtBeginning(struct Node** head_ref, int new_data) {

struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));

new_node->data = new_data;

new_node->next = *head_ref;

*head_ref = new_node;

void insertAtEnd(struct Node** head_ref, int new_data) {

struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));

struct Node* last = *head_ref;

new_node->data = new_data;

new_node->next = NULL;

if (*head_ref == NULL) {

*head_ref = new_node;

return;

while (last->next != NULL)

last = last->next;

last->next = new_node;

void deleteNode(struct Node** head_ref, int key) {

struct Node* temp = *head_ref;


struct Node* prev = NULL;

if (temp != NULL && temp->data == key) {

*head_ref = temp->next;

free(temp);

return;

while (temp != NULL && temp->data != key) {

prev = temp;

temp = temp->next;

if (temp == NULL) return;

prev->next = temp->next;

free(temp);

void traverse(struct Node* node) {

while (node != NULL) {

printf("%d -> ", node->data);

node = node->next;

printf("NULL\n");

int main() {

struct Node* head = NULL;

insertAtEnd(&head, 1);

insertAtEnd(&head, 2);
insertAtEnd(&head, 3);

insertAtBeginning(&head, 0);

printf("Linked list after insertions: ");

traverse(head);

deleteNode(&head, 2);

printf("Linked list after deleting 2: ");

traverse(head);

return 0;

#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node* next;

struct Node* prev;

};

struct Node* head = NULL;

struct Node* createNode(int data) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = data;
newNode->next = NULL;

newNode->prev = NULL;

return newNode;

void insertAtBeginning(int data) {

struct Node* newNode = createNode(data);

if (head == NULL) {

head = newNode;

return;

newNode->next = head;

head->prev = newNode;

head = newNode;

void insertAtEnd(int data) {

struct Node* newNode = createNode(data);

if (head == NULL) {

head = newNode;

return;

struct Node* temp = head;

while (temp->next != NULL)

temp = temp->next;

temp->next = newNode;

newNode->prev = temp;

void deleteNode(int data) {

struct Node* temp = head;


if (temp != NULL && temp->data == data) {

head = temp->next;

if (head != NULL)

head->prev = NULL;

free(temp);

return;

while (temp != NULL && temp->data != data)

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() {

struct Node* temp = head;

while (temp != NULL) {

printf("%d ", temp->data);

temp = temp->next;

printf("\n");

int main() {

insertAtBeginning(10);

insertAtEnd(20);

insertAtBeginning(5);
traverse();

deleteNode(10);

traverse();

deleteNode(5);

traverse();

return 0;

You might also like