0% found this document useful (0 votes)
9 views12 pages

C Pds Lab Experiments

Uploaded by

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

C Pds Lab Experiments

Uploaded by

nithu3591
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

C PROGRAMMING AND DATA STRUCTURES LAB EXPERIMENTS.

LIST OF EXPERIMENTS

1. Practice of C programming using statements, expressions, decision making and iterative statements

2. Practice of C programming using Functions and Arrays

3. Implement C programs using Pointers and Structures

4. Implement C programs using Files

5. Development of real time C applications

6. Array implementation of List ADT

7. Array implementation of Stack and Queue ADTs

8. Linked list implementation of List, Stack and Queue ADTs

9. Applications of List, Stack and Queue ADTs

10.Implementation of Binary Trees and operations of Binary Trees

11. Implementation of Binary Search Trees

12. Implementation of searching techniques

13. Implementation of Sorting algorithms : Insertion Sort, Quick Sort, Merge Sort

14. Implementation of Hashing – any two collision techniques

Practice of C programming using statements, expressions, decision making and iterative statements

#include <stdio.h>

int main()

int n, i, fact = 1;
printf("Enter a number: ");

scanf("%d", &n);

for(i=1; i<=n; i++) {

fact *= i;

printf("Factorial of %d = %d\n", n, fact);

return 0;

2. Functions and Arrays

#include <stdio.h>

int sumArray(int arr[], int n) {

int sum = 0;

for(int i=0;i<n;i++) sum += arr[i];

return sum;

int main() {

int arr[5] = {10, 20, 30, 40, 50};

printf("Sum = %d\n", sumArray(arr, 5));

return 0;

3. Pointers and Structures

#include <stdio.h>

struct Student {

char name[20];
int age;

};

int main() {

struct Student s = {"Arun", 20};

struct Student *ptr = &s;

printf("Name: %s, Age: %d\n", ptr->name, ptr->age);

return 0;

4. Files

#include <stdio.h>

int main() {

FILE *fp = fopen("data.txt", "w");

fprintf(fp, "Hello, File Handling in C!\n");

fclose(fp);

fp = fopen("data.txt", "r");

char ch;

while((ch=fgetc(fp))!=EOF) printf("%c", ch);

fclose(fp);

return 0;

5. Real-time Application (Simple Calculator)

#include <stdio.h>

int main() {

char op; double a,b;


printf("Enter expression (a + b): ");

scanf("%lf %c %lf",&a,&op,&b);

switch(op) {

case '+': printf("Result = %.2lf\n", a+b); break;

case '-': printf("Result = %.2lf\n", a-b); break;

case '*': printf("Result = %.2lf\n", a*b); break;

case '/': printf("Result = %.2lf\n", a/b); break;

default: printf("Invalid operator\n");

return 0;

6. Array Implementation of List

#include <stdio.h>

int main() {

int arr[10] = {1,2,3,4}, n=4, i;

printf("List: ");

for(i=0;i<n;i++) printf("%d ", arr[i]);

printf("\nInsert 99 at position 2\n");

for(i=n;i>2;i--) arr[i] = arr[i-1];

arr[2] = 99; n++;

for(i=0;i<n;i++) printf("%d ", arr[i]);

return 0;

}
7. Array Implementation of Stack

#include <stdio.h>

#define MAX 5

int stack[MAX], top=-1;

void push(int x){ if(top<MAX-1) stack[++top]=x; }

void pop(){ if(top>=0) printf("Popped: %d\n",stack[top--]); }

int main() {

push(10); push(20); push(30);

pop(); pop();

return 0;

8. Linked List (Insert & Display)

#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node *next;

};

int main() {

struct Node *head=NULL, *temp, *newNode;

for(int i=1;i<=3;i++){

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

newNode->data = i*10;

newNode->next = NULL;

if(head==NULL) head=newNode;
else temp->next=newNode;

temp=newNode;

temp=head;

while(temp){ printf("%d ", temp->data); temp=temp->next; }

return 0;

9. Application of Stack (Infix → Postfix)

(simplified version – only single digits & +,-,*,/ operators)

#include <stdio.h>

#include <ctype.h>

#define MAX 100

char stack[MAX]; int top=-1;

void push(char c){ stack[++top]=c; }

char pop(){ return stack[top--]; }

int precedence(char c){ if(c=='*'||c=='/') return 2; if(c=='+'||c=='-') return 1; return 0; }

int main(){

char infix[]="A+B*C", postfix[MAX]; int k=0;

for(int i=0; infix[i]; i++){

if(isalnum(infix[i])) postfix[k++]=infix[i];

else{

while(top!=-1 && precedence(stack[top])>=precedence(infix[i]))

postfix[k++]=pop();

push(infix[i]);

}
}

while(top!=-1) postfix[k++]=pop();

postfix[k]='\0';

printf("Postfix: %s\n", postfix);

return 0;

10. Binary Tree Traversal

#include <stdio.h>

#include <stdlib.h>

struct Node{

int data; struct Node *left,*right;

};

struct Node* newNode(int val){

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

node->data=val; node->left=node->right=NULL;

return node;

void inorder(struct Node* root){

if(root){ inorder(root->left); printf("%d ",root->data); inorder(root->right);}

int main(){

struct Node* root=newNode(1);

root->left=newNode(2);

root->right=newNode(3);

root->left->left=newNode(4);
printf("Inorder: "); inorder(root);

return 0;

11. Binary Search Tree

#include <stdio.h>

#include <stdlib.h>

struct Node{ int data; struct Node *left,*right; };

struct Node* newNode(int v){

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

n->data=v; n->left=n->right=NULL; return n;

struct Node* insert(struct Node* root,int v){

if(root==NULL) return newNode(v);

if(v<root->data) root->left=insert(root->left,v);

else root->right=insert(root->right,v);

return root;

void inorder(struct Node* r){ if(r){ inorder(r->left); printf("%d ",r->data); inorder(r->right);} }

int main(){

struct Node* root=NULL;

root=insert(root,50); insert(root,30); insert(root,70);

insert(root,20); insert(root,40);

printf("BST Inorder: "); inorder(root);

return 0;

}
12. Searching Techniques

#include <stdio.h>

int linearSearch(int arr[], int n, int x){

for(int i=0;i<n;i++) if(arr[i]==x) return i;

return -1;

int binarySearch(int arr[], int n, int x){

int l=0,r=n-1,mid;

while(l<=r){

mid=(l+r)/2;

if(arr[mid]==x) return mid;

else if(arr[mid]<x) l=mid+1;

else r=mid-1;

return -1;

int main(){

int arr[]={10,20,30,40,50};

printf("Linear: %d\n", linearSearch(arr,5,30));

printf("Binary: %d\n", binarySearch(arr,5,30));

return 0;

}
13. Sorting

#include <stdio.h>

// Insertion Sort

void insertion(int arr[], int n){

for(int i=1;i<n;i++){

int key=arr[i], j=i-1;

while(j>=0 && arr[j]>key){ arr[j+1]=arr[j]; j--; }

arr[j+1]=key;

// Quick Sort

int partition(int arr[],int low,int high){

int pivot=arr[high], i=low-1;

for(int j=low;j<high;j++)

if(arr[j]<pivot){ i++; int t=arr[i]; arr[i]=arr[j]; arr[j]=t; }

int t=arr[i+1]; arr[i+1]=arr[high]; arr[high]=t;

return i+1;

void quick(int arr[],int low,int high){

if(low<high){

int pi=partition(arr,low,high);

quick(arr,low,pi-1); quick(arr,pi+1,high);

}
}

// Merge Sort

void merge(int arr[],int l,int m,int r){

int n1=m-l+1,n2=r-m,L[n1],R[n2];

for(int i=0;i<n1;i++) L[i]=arr[l+i];

for(int j=0;j<n2;j++) R[j]=arr[m+1+j];

int i=0,j=0,k=l;

while(i<n1 && j<n2) arr[k++]=(L[i]<=R[j])?L[i++]:R[j++];

while(i<n1) arr[k++]=L[i++];

while(j<n2) arr[k++]=R[j++];

void mergeSort(int arr[],int l,int r){

if(l<r){ int m=(l+r)/2; mergeSort(arr,l,m); mergeSort(arr,m+1,r); merge(arr,l,m,r); }

int main(){

int arr[]={50,20,10,40,30}, n=5;

insertion(arr,n);

printf("Insertion Sort: "); for(int i=0;i<n;i++) printf("%d ",arr[i]); printf("\n");

int arr2[]={50,20,10,40,30}; quick(arr2,0,n-1);

printf("Quick Sort: "); for(int i=0;i<n;i++) printf("%d ",arr2[i]); printf("\n");

int arr3[]={50,20,10,40,30}; mergeSort(arr3,0,n-1);

printf("Merge Sort: "); for(int i=0;i<n;i++) printf("%d ",arr3[i]); printf("\n");

return 0;
}

14. Hashing (Chaining + Linear Probing)

#include <stdio.h>

#define SIZE 10

// Linear Probing

void linearProbing(int arr[], int keys[], int n){

for(int i=0;i<SIZE;i++) arr[i]=-1;

for(int i=0;i<n;i++){

int idx=keys[i]%SIZE;

while(arr[idx]!=-1) idx=(idx+1)%SIZE;

arr[idx]=keys[i];

printf("Linear Probing Table:\n");

for(int i=0;i<SIZE;i++) printf("%d: %d\n",i,arr[i]);

int main(){

int arr[SIZE], keys[]={23,43,13,27}, n=4;

linearProbing(arr,keys,n);

return 0;

You might also like