Lab
Bhai tension mat le 😎
Main tujhe Lab-3 ka pura ready-to-write material de raha hoon —
tu bas copy karega, samjhega aur practical me likh dega.
Main sab programs simple C, array-based stack/queue, singly linked list me de raha hoon.
---
🔥 1️⃣ Evaluate Postfix Expression Using Stack
#include <stdio.h>
#include <ctype.h>
int stack[100], top = -1;
void push(int x) {
stack[++top] = x;
}
int pop() {
return stack[top--];
}
int main() {
char exp[100];
int i, a, b;
printf("Enter postfix expression: ");
scanf("%s", exp);
for(i = 0; exp[i] != '\0'; i++) {
if(isdigit(exp[i])) {
push(exp[i] - '0');
}
else {
b = pop();
a = pop();
switch(exp[i]) {
case '+': push(a + b); break;
case '-': push(a - b); break;
case '*': push(a * b); break;
case '/': push(a / b); break;
}
}
}
printf("Result = %d", pop());
return 0;
}
---
🔥 2️⃣ Infix to Postfix
(Short lab version)
#include <stdio.h>
#include <ctype.h>
char stack[100];
int top = -1;
void push(char x) {
stack[++top] = x;
}
char pop() {
return stack[top--];
}
int priority(char x) {
if(x == '+' || x == '-') return 1;
if(x == '*' || x == '/') return 2;
return 0;
}
int main() {
char exp[100], postfix[100];
int i, j = 0;
printf("Enter infix: ");
scanf("%s", exp);
for(i = 0; exp[i] != '\0'; i++) {
if(isalnum(exp[i]))
postfix[j++] = exp[i];
else if(exp[i] == '(')
push(exp[i]);
else if(exp[i] == ')') {
while(stack[top] != '(')
postfix[j++] = pop();
pop();
}
else {
while(top != -1 && priority(stack[top]) >= priority(exp[i]))
postfix[j++] = pop();
push(exp[i]);
}
}
while(top != -1)
postfix[j++] = pop();
postfix[j] = '\0';
printf("Postfix = %s", postfix);
return 0;
}
---
🔥 3️⃣ Balanced Parentheses
#include <stdio.h>
char stack[100];
int top = -1;
void push(char x) {
stack[++top] = x;
}
char pop() {
return stack[top--];
}
int main() {
char exp[100];
int i;
printf("Enter expression: ");
scanf("%s", exp);
for(i = 0; exp[i] != '\0'; i++) {
if(exp[i] == '(')
push(exp[i]);
else if(exp[i] == ')') {
if(top == -1) {
printf("Not Balanced");
return 0;
}
pop();
}
}
if(top == -1)
printf("Balanced");
else
printf("Not Balanced");
return 0;
}
---
🔥 4️⃣ Average of Queue
#include <stdio.h>
int main() {
int n, i, arr[100];
float sum = 0;
printf("Enter number of elements: ");
scanf("%d", &n);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
sum += arr[i];
}
printf("Average = %.2f", sum/n);
return 0;
}
---
🔥 5️⃣ Max & Min in Singly Linked List
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *head = NULL;
void create(int n) {
struct node *newnode, *temp;
for(int i=0;i<n;i++) {
newnode = malloc(sizeof(struct node));
scanf("%d",&newnode->data);
newnode->next = NULL;
if(head == NULL)
head = newnode;
else {
temp = head;
while(temp->next != NULL)
temp = temp->next;
temp->next = newnode;
}
}
}
int main() {
int n, max, min;
struct node *temp;
printf("Enter number of nodes: ");
scanf("%d",&n);
create(n);
temp = head;
max = min = temp->data;
while(temp != NULL) {
if(temp->data > max) max = temp->data;
if(temp->data < min) min = temp->data;
temp = temp->next;
}
printf("Maximum = %d\nMinimum = %d", max, min);
return 0;
}
---
🔥 6️⃣ Reverse Singly Linked List
void reverse() {
struct node *prev = NULL, *current = head, *next;
while(current != NULL) {
next = current->next;
current->next = prev;
prev = current;
current = next;
}
head = prev;
}
---
🔥 7️⃣ Search in Singly Linked List
void search(int key) {
struct node *temp = head;
int pos = 1;
while(temp != NULL) {
if(temp->data == key) {
printf("Found at position %d", pos);
return;
}
temp = temp->next;
pos++;
}
printf("Not Found");
}
---
💀 Important
Lab exam me:
Code clean likhna
Indentation maintain
Output format likhna
---
Agar chahe to main: ✅ Har question ka dry run
✅ Viva questions
✅ PDF-style lab record format
✅ Handwritten style format
bana du.
Bol bhai 👊
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *head = NULL;
struct node *tail = NULL;
/* Create using tail (NO traversal) */
void create(int n) {
struct node *newnode;
for(int i = 0; i < n; i++) {
newnode = malloc(sizeof(struct node));
scanf("%d", &newnode->data);
newnode->next = NULL;
if(head == NULL) {
head = tail = newnode;
}
else {
tail->next = newnode;
tail = newnode;
}
}
}
int main() {
int n, max, min;
struct node *temp;
printf("Enter number of nodes: ");
scanf("%d", &n);
create(n);
if(head == NULL) {
printf("List empty");
return 0;
}
temp = head;
max = min = temp->data;
while(temp != NULL) {
if(temp->data > max)
max = temp->data;
if(temp->data < min)
min = temp->data;
temp = temp->next;
}
printf("Maximum = %d\n", max);
printf("Minimum = %d\n", min);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *head = NULL;
struct node *tail = NULL;
/* Create using tail */
void create(int n) {
struct node *newnode;
for(int i = 0; i < n; i++) {
newnode = malloc(sizeof(struct node));
scanf("%d", &newnode->data);
newnode->next = NULL;
if(head == NULL)
head = tail = newnode;
else {
tail->next = newnode;
tail = newnode;
}
}
}
/* Search function */
void search(int key) {
struct node *temp = head;
int pos = 1;
while(temp != NULL) {
if(temp->data == key) {
printf("Element found at position %d\n", pos);
return;
}
temp = temp->next;
pos++;
}
printf("Element not found\n");
}
/* Display */
void display() {
struct node *temp = head;
while(temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
int main() {
int n, key;
printf("Enter number of nodes: ");
scanf("%d", &n);
create(n);
printf("Enter element to s#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *head = NULL;
struct node *tail = NULL;
/* Create using tail */
void create(int n) {
struct node *newnode;
for(int i = 0; i < n; i++) {
newnode = malloc(sizeof(struct node));
scanf("%d", &newnode->data);
newnode->next = NULL;
if(head == NULL)
head = tail = newnode;
else {
tail->next = newnode;
tail = newnode;
}
}
}
/* Reverse function */
void reverse() {
struct node *prev = NULL;
struct node *current = head;
struct node *next;
tail = head; // old head becomes new tail
while(current != NULL) {
next = current->next;
current->next = prev;
prev = current;
current = next;
}
head = prev;
}
/* Display */
void display() {
struct node *temp = head;
while(temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
int main() {
int n;
printf("Enter number of nodes: ");
scanf("%d", &n);
create(n);
printf("Original List:\n");
display();
reverse();
printf("Reversed List:\n");
display();
return 0;
}earch: ");
scanf("%d", &key);
search(key);
return 0;
}
Comments
Post a Comment