Week - 6
Week - 6
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node* next;
};
int isEmpty()
{
return top == NULL;
}
int main()
{
push(1);
push(2);
push(3);
peek();
pop();
pop();
pop();
pop();
peek();
return 0;
}
In-Lab – 2(page No: 121)
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node* next;
};
char pop()
{
if(top>=0)
{
char ele = a[top];
top--;
return ele;
}
else
{
printf("Stack is empty. Cannot pop.\n");
return '-1';
}
}
int isEmpty()
{
return top==-1;
}
int isFull()
{
return top>=MAX_SIZE;
}
int main()
{
char originalString[] = "Hello, World!";
int len = strlen(originalString);
#include <stdio.h>
int sum(int arr[], int size)
{
int total = 0;
for (int i = 0; i < size; i++)
{
total += arr[i];
}
return total;
}
int equalStacks(int h1[], int n1, int h2[], int n2, int h3[], int n3)
{
int sum1 = sum(h1, n1);
int sum2 = sum(h2, n2);
int sum3 = sum(h3, n3);
int i = 0, j = 0, k = 0;
while (i < n1 && j < n2 && k < n3)
{
if (sum1 == sum2 && sum2 == sum3)
{
return sum1;
}
if (sum1 >= sum2 && sum1 >= sum3)
{
sum1 -= h1[i];
i++;
}
else if (sum2 >= sum1 && sum2 >= sum3)
{
sum2 -= h2[j];
j++;
}
else if (sum3 >= sum1 && sum3 >= sum2)
{
sum3 -= h3[k];
k++;
}
}
return 0;
}
int main()
{
int n1, n2, n3;
scanf("%d %d %d", &n1, &n2, &n3);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Node
{
int data;
struct Node* next;
};
void push(int x)
{
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL)
{
printf("Stack Overflow. Cannot push: %d\n", x);
return;
}
newNode->data = x;
newNode->next = top;
top = newNode;
}
void pop()
{
if (top == NULL)
{
printf("Stack is empty! Deletion not possible.\n");
return;
}
struct Node* temp = top;
top = top->next;
free(temp);
}
int getMax()
{
struct Node *temp;
temp=top;
if(temp ==NULL) return -1;
int max=top->data;
while(temp!=NULL)
{
if(temp->data>min)
max=temp->data;
temp=temp->next;
}
return max;
}
int main()
{
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
char operation[10];
scanf(" %s", operation);
if (strcmp(operation, "1") == 0)
{
int value;
scanf(" %d", &value);
push(value);
}
else if (strcmp(operation, "2") == 0)
{
pop();
}
else if (strcmp(operation, "3") == 0)
{
printf("%d\n", getMax());
}
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node* next;
};
void push(int x)
{
struct node* newnode=(struct node*)malloc(sizeof(struct node));
if (newnode == NULL)
{
printf("Stack Overflow. Cannot push: %d\n", x);
return;
}
newnode->data = x;
newnode->next = top;
top = newnode;
}
int main()
{
push(5);
push(2);
push(1);
push(6);
push(8);
printf("Current stack elements:\n");
display();
printf("Stack elements in insertion order: ");
printInOrder(top);
return 0;
}
Skill Lab-2(page No:130)
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node* next;
};
void push(int x)
{
struct Node* newNode=(struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL)
{
printf("Stack Overflow. Cannot push: %d\n", x);
return;
}
newNode->data = x;
newNode->next = top;
top = newNode;
}
float calculateAverage()
{
if (top == NULL)
{
printf("Stack is empty!\n");
return 0;
}
int sum = 0;
int count = 0;
struct Node* temp = top;
while (temp != NULL)
{
sum += temp->data;
count++;
temp = temp->next;
}
return (float)sum / count;
}
void displayStack()
{
if (top == NULL)
{
printf("Stack is empty!\n");
return;
}
struct Node* temp = top;
printf("Elements of the stack: ");
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
int main()
{
push(6);
push(4);
push(2);
push(5);
push(3);
push(1);
displayStack();
float average = calculateAverage();
printf("Average of the said stack values: %.2f\n", average);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node* next;
};
int isEmpty()
{
return top == NULL;
}
int getTop()
{
if (isEmpty())
{
printf("Stack is empty.\n");
return -1;
}
return top->data;
}
int getKthElement(int k)
{
struct Node* temp = top;
int count = 1;
if (temp == NULL)
{
printf("There are less than %d elements in the stack.\n", k);
return -1;
}
return temp->data;
}
void display()
{
struct Node* temp = top;
printf("Elements of the stack:\n");
while (temp != NULL)
{
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
int main()
{
int n, value, k;
printf("Enter the number of elements to push: ");
scanf("%d", &n);
printf("Enter the elements:\n");
for (int i = 0; i < n; i++)
{
scanf("%d", &value);
push(value);
}
displayStack();
printf("Top element: %d\n", getTop());
printf("Enter k value to find the kth element from top: ");
scanf("%d", &k);
int kthElement = getKthElement(k);
if (kthElement != -1)
{
printf("%dth element from top: %d\n", k, kthElement);
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node* next;
};
int main()
{
int decimal;
printf("Input a decimal number: ");
scanf("%d", &decimal);
decimalToBinary(decimal);
return 0;
}