0% found this document useful (0 votes)
4 views62 pages

DS Program

The document contains a series of C programs demonstrating various programming concepts, including pointer manipulation, string handling, data structures like stacks and queues, and expression conversion between infix, postfix, and prefix notations. Each program is structured with input prompts, processing logic, and output statements to illustrate the functionality. The programs cover a range of topics suitable for learning basic to intermediate C programming skills.

Uploaded by

JavedAhmad Khan
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)
4 views62 pages

DS Program

The document contains a series of C programs demonstrating various programming concepts, including pointer manipulation, string handling, data structures like stacks and queues, and expression conversion between infix, postfix, and prefix notations. Each program is structured with input prompts, processing logic, and output statements to illustrate the functionality. The programs cover a range of topics suitable for learning basic to intermediate C programming skills.

Uploaded by

JavedAhmad Khan
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/ 62

Program 1: Create, Initialize, and Access a Pointer Variable

#include<stdio.h>

#include<conio.h>

void main()

int x;

int *ptr;

printf("Enter the value of x: ");

scanf("%d",&x);

ptr=&x;

printf("The value of x using pointer is %d\n",*ptr);

getch();

}
Program 2: Calculate the Length of a String Using a Pointer

#include<stdio.h>

void main()

char str[10];

int i;

printf("Enter a string : ");

scanf("%s",str);

printf("The string is : %s\n",str);

i=0;

while(*(str+i))

i++;

printf("The length of the string is %d\n",i);

}
Program 3: Swap Numbers Using Pointer

#include<stdio.h>

#include<conio.h>

void main()

int a,b;

int *p1,*p2;

int temp;

printf("Enter 2 numbers : ");

scanf("%d%d",&a,&b);

p1=&a;

p2=&b;

printf("The numbers before swapping are : %d %d\n",*p1,*p2);

temp=*p1;

*p1=*p2;

*p2=temp;

printf("The numbers after swapping are : %d %d\n",*p1,*p2);

getch();

}
Program 4: Print All Permutations of a Given String Using
Pointers

#include<stdio.h>

#include<string.h>

void swap(char *p, char *q) {

char temp;

temp = *p;

*p = *q;

*q = temp;

void permute(char *a, int i, int n) {

int j;

if (i == n) {

printf("%s\n", a);

} else {

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

swap((a + i), (a + j));

permute(a, i + 1, n);

swap((a + i), (a + j));

int main() {

char a[20];
int n;

printf("Enter string: ");

scanf("%s", a);

n = strlen(a);

printf("Permutations:\n");

permute(a, 0, n - 1);

return 0;

}
Program 5: Store n Students' Information Using Structure

#include<stdio.h>

#include<conio.h>

struct Student

int roll,m1,m2;

char name[20];

};

void main()

struct Student s[10];

int total,n,i;

float avg;

printf("Enter the total num of students : ");

scanf("%d",&n);

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

printf("\nEnter Roll No : ");

scanf("%d",&s[i].roll);

printf("Enter the Name : ");

scanf("%s",s[i].name);
printf("Enter the marks : ");

scanf("%d%d",&s[i].m1,&s[i].m2);

printf("\n\t \tResult Sheet\n");

printf("______\n");

printf("RollNo\tName\tM1\tM2\tTotal\tAvg\n");

printf("______\n");

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

total=s[i].m1+s[i].m2;

avg=total/2;

printf("%d\t%s\t%d\t%d\t%d\t%.2f\n",

s[i].roll,s[i].name,s[i].m1,s[i].m2,total,avg);

getch();

}
Program 6: Implement Push, Pop, and Traverse Operations
on Stack

#include<stdio.h>

#include<conio.h>

int a[50],top,i,ele,stack_size;

void push();

void pop();

void display();

void main()

int ch;

top=-1;

printf("Enter the stack size : ");

scanf("%d",&stack_size);

do

printf("\n\t Stack operations\n");

printf("---\n");

printf("1:Push\n2:Pop\n3:Display\n4:Exit\n");

printf("---\n");
printf("Enter the choice : ");

scanf("%d",&ch);

switch(ch)

case 1:push();

break;

case 2:pop();

break;

case 3:display();

break;

default:printf("\nEnter correct choice\n");

}while(ch<4);

getch();

void push()

if(top==stack_size-1)

printf("\nStack is Overflow\n");

else

printf("\nEnter the element : ");


scanf("%d",&ele);

a[++top]=ele;

void pop()

if(top==-1)

printf("\nStack is Underflow\n");

else

printf("\nThe popped element is %d\n",a[top--]);

void display()

if(top==-1)

printf("\nStack is empty\n");

else

printf("\nThe stack elements are\n");

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

Program 7: Convert Infix Notation to Postfix Notation

#include <stdio.h>

#include <string.h>

int F(char symbol)

switch(symbol)

case '+':

case '-': return 1;

case '*':

case '/': return 3;

case '^':

case '$': return 6;

case ')': return 0;

case '#': return -1;

default: return 8;

}
}

int G(char symbol)

switch(symbol)

case '+':

case '-': return 2;

case '*':

case '/': return 4;

case '^':

case '$': return 5;

case '(': return 0;

case ')': return 9;

default: return 7;

void infix_postfix(char infix[], char postfix[]) {

int top;

char s[30];

int j, n;
int i;

char symbol;

top = -1;

s[++top] = '#';

j =0;

n=strlen(infix);

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

symbol = infix[i];

while (F(s[top])>G(symbol))

postfix[j] = s[top--];

j++;

if (F(s[top])!=G(symbol))

s[++top] = symbol;

else

top--;

while(s[top]!='#')

postfix[j++] = s[top--];
}

postfix[j] = '\0';

void main()

char infix[20];

char postfix[20];

printf("Enter a valid infix expression\n");

scanf("%s",infix);

infix_postfix(infix,postfix);

printf("The postfix expression is\n");

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

getch();

}
Program 8: Convert Infix Notation to Prefix Notation

#include <stdio.h>

#include <string.h>

int F(char symbol)

switch(symbol)

case '+':

case '-': return 1;

case '*':

case '/': return 3;

case '^':

case '$': return 6;

case ')': return 0;

case '#': return -1;

default: return 8;

}
int G(char symbol)

switch(symbol)

case '+':

case '-': return 2;

case '*':

case '/': return 4;

case '^':

case '$': return 5;

case '(': return 0;

case ')': return 9;

default: return 7;

void infix_prefix(char infix[], char prefix[]) {

int top;

char s[30];

int j, n;

int i;
char symbol;

top = -1;

s[++top] = '#';

j =0;

strrev(infix);

n=strlen(infix);

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

symbol = infix[i];

while (F(s[top])>G(symbol))

prefix[j] = s[top--];

j++;

if (F(s[top])!=G(symbol))

s[++top] = symbol;

else

top--;

while(s[top]!='#')
{

prefix[j++] = s[top--];

prefix[j] = '\0';

void main()

char infix[20];

char prefix[20];

printf("Enter a valid infix expression\n");

scanf("%s",infix);

infix_prefix(infix,prefix);

strrev(prefix);

printf("The prefix expression is\n");

printf("%s\n",prefix);

getch();

}
Program 9: Convert Prefix Notation to Postfix Notation

#include <stdio.h>

#include <conio.h>

#include <string.h>

void push(char item[],int *top,char s[][20])

*top=*top+1;

strcpy(s[*top],item);

char *pop(int *top,char s[][20])

char *item;

item=s[*top];

*top=*top-1;

return item;

void prefix_postfix(char prefix[],char postfix[])

int i,top;

char s[20][20],symbol,temp[2],*op1,*op2;
top=-1;

strrev(prefix);

for(i=0;i<strlen(prefix);i++)

symbol=prefix[i];

temp[0]=symbol;

temp[1]='\0';

switch(symbol)

case '+':

case '-':

case '*':

case '/':

case '^':

op1=pop(&top,s);

op2=pop(&top,s);

strcpy(postfix,op1);

strcat(postfix,op2);

strcat(postfix,temp);

push(postfix,&top,s);

break;

default:push(temp,&top,s);

}
}

void main()

char prefix[20],postfix[20];

printf("enter the valid prefix expression\n");

scanf("%s",prefix);

prefix_postfix(prefix,postfix);

printf("the equivalent postfix expression is:\n");

printf("%s",postfix);

getch();

}
Program 10: Perform Insert, Delete, and Display Operations on Queue

#include<stdio.h>

#include<conio.h>

void insert(int);

void qdelete();

void display();

int queue_size,q[10],f,r;

void main()

int choice,item;

f=0;

r=-1;

printf("Enter Queue size : ");

scanf("%d",&queue_size);

do

printf("\n Queue Operations \n");


printf("---\n");

printf("1:Insert\n2:Delete\n3:Display\n4:Exit\n");

printf("---\n");

printf("Enter your choice : ");

scanf("%d",&choice);

switch(choice)

case 1:printf("\nEnter the element to be inserted : ");

scanf("%d",&item);

insert(item);

break;

case 2:qdelete();

break;

case 3:display();

break;

case 4:printf("\nQuit the Program\n");

break;

}while(choice<4);

getch();

void insert(int item)

if(r==queue_size-1)
{

printf("\nQueue is full\n");

else

q[++r]=item;

void qdelete()

int i;

if(r==-1)

printf("\nQueue is empty\n");

else

printf("\nThe deleted element is %d\n",q[f++]);

if(f>r)

r=-1;

f=0;

}
}

void display()

int i;

if(r==-1)

printf("\nQueue is empty\n");

else

printf("\nThe Queue elements are\n");

for(i=f;i<=r;i++)

printf("%d\n",q[i]);

}
Program 11: Implement Circular Queue

#include<stdio.h>

void qinsert(int);

void qdelete();

void qdisplay();

int q_size;

int i,f,r,item,q[20],count;

void main()

int choice;

f=0;

r=-1;

count=0;

printf("\nEnter the queue size\n");

scanf("%d",&q_size);

do

{
printf("\n Queue Operations\n");

printf("\n1:Insert\n2:Delete\n3:Display\n4:Exit\n");

printf("---\n");

printf("Enter the choice : ");

scanf("%d",&choice);

switch(choice)

case 1: printf("\nEnter the item to be inserted : ");

scanf("%d",&item);

qinsert(item);

break;

case 2:qdelete();

break;

case 3:qdisplay();

break;

default:printf("Exit");

}while(choice<4);

void qinsert(int item)

if(count==q_size)

printf("\nQueue overflow\n");

}
else

r=(r+1)%q_size;

q[r]=item;

printf("\nThe inserted item is %d\n",item);

count++;

void qdelete()

if(count==0)

printf("\nQueue underflow\n");

else

printf("\nThe element deleted is %d\n",q[f]);

f=(f+1)%q_size;

count--;

void qdisplay()

int j;
if(count==0)

printf("\nQueue is Empty\n");

else

printf("\nThe Queue elements are\n");

i=f;

for(j=1;j<=count;j++)

printf("%d\n",q[i]);

i=(i+1)%q_size;

}
Program 12: Implement Double-Ended Queue

#include<stdio.h>

#include<stdlib.h>

#define QUEUE_SIZE 5

int choice,item,f,r,q[10];

void insert_rear()

if (r==QUEUE_SIZE-1)

printf("Queue overflow");

return;

r=r+1;

q[r] = item;

void delete_front()

if(f>r)
{

printf("Queue underflow\n");

return;

printf("The element deleted is %d\n",q[f++]);

if (f>r)

f=0;

r=-1;

void insert_front()

if (f==0 && r == -1)

q[++(r)] =item;

return;

if(f!=0)

q[--(f)] = item;

return;

}
printf("Front insertion not possible\n");

void delete_rear()

if(f>r)

printf("Queue underflow\n");

return;

printf("The element deleted is %d\n",q[r--]);

if (f> r)

f=0;

r=-1;

void display()

int i;

if(f>r)

{
printf("Queue is empty\n");

return;

printf("Contents of the queue\n");

for (i = f; i <= r; i++)

printf("%d\n", q[i]);

void main()

f=0;r=-1;

for (;;)

printf("\n***** Degree Operations *****\n");

printf("1:Insert_front\n2:Insert_rear\n");

printf("3:Delete_front\n4:Delete_rear\n");

printf("5:Display\n6:Exit\n");

printf("Enter the choice : ");

scanf("%d",&choice);

switch (choice)
{

case 1: printf("Enter the item to be inserted at front: ");

scanf("%d",&item);

insert_front();

break;

case 2:printf("Enter the item to be inserted at rear: ");

scanf("%d",&item);

insert_rear();

break;

case 3:delete_front();

break;

case 4: delete_rear();

break;

case 5:display();

break;

default: exit(0);

}
Program 13: Implement Priority Queue

#include<stdio.h>

void qinsert(int);

void qdelete();

void display();

int queue_size,q[10],f,r;

void main()

int choice,item;

f=0;

r=-1;

printf("Enter Queue size : ");

scanf("%d",&queue_size);

do

printf("\n Queue Operations \n");

printf("---\n");
printf("1:Insert\n2:Delete\n3:Display\n4:Exit\n");

printf("---\n");

printf("Enter your choice : ");

scanf("%d",&choice);

switch(choice)

case 1:printf("\nEnter the element to be inserted : ");

scanf("%d",&item);

qinsert(item);

break;

case 2:qdelete();

break;

case 3:display();

break;

case 4:printf("\nQuit the Program\n");

break;

}while(choice<4);

getch();

void qinsert(int item)

int j;
if(r==queue_size-1)

printf("\nQueue is full\n");

else

j=r;

while(j>=0 && item< q[j])

q[j+1]=q[j];

j--;

q[j+1]=item;

r=r+1;

void qdelete()

int i;

if(r==-1)

printf("\nQueue is empty\n");

else
{

printf("\nThe deleted element is %d\n",q[f++]);

if(f>r)

r=-1;

f=0;

void display()

int i;

if(r==-1)

printf("\nQueue is empty\n");

else

printf("the queue element are");

for(i=f;i<=r;i++)

printf("%d",q[i]);

}
}

Program 14: Search an Element Using Linear Search

#include<stdio.h>

void main()

int a[10],n,i,key;

printf("Enter the limit\n");

scanf("%d",&n);

printf("Enter the elements\n");

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

scanf("%d",&a[i]);

printf("Enter the element to be searched : ");

scanf("%d",&key);

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

if(a[i]==key)

{
printf("The element is found at location %d",(i+1));

break;

if(i==n)

printf("The element is not found!!!!");

}
Program 15: Sort Given Array Using Insertion Sort Technique

#include <stdio.h>

void insertionSort(int array[], int n)

int key, j, s;

for (s = 1; s < n; s++)

key = array[s];

j = s - 1;

while (key < array[j] && j >= 0)

array[j + 1] = array[j];

--j;

array[j + 1] = key;

void main()
{

int a[10], n, i;

printf("\nEnter the limit\n");

scanf("%d",&n);

printf("Enter array element\n");

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

scanf("%d",&a[i]);

insertionSort(a, n);

printf("Sorted array in ascending order:\n");

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

printf("%d\n", a[i]);

printf("\n");

}
Program 16: Sort Given Array Using Bubble Sort Technique

#include<stdio.h>

void main()

int a[20],i,j,n,temp;

clrscr();

printf("\nEnter the limit\n");

scanf("%d",&n);

printf("Enter array element\n");

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

scanf("%d",&a[i]);

/*SWAPING PART*/

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

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

if(a[i]>a[j])

{
temp=a[i];

a[i]=a[j];

a[j]=temp;

printf("\nSorted array elements are\n");

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

printf("%d\n",a[i]);

}
Program 17: Sort Given Array Using Quick Sort Technique

#include<stdio.h>

int partition(int a[],int low,int high)

int i,j,temp,key;

key=a[low];

i=low+1;

j=high;

while(1)

while(i<high && key>=a[i]) i++;

while(key<a[j]) j--;

if(i<j)

temp=a[i];

a[i]=a[j];

a[j]=temp;

else
{

temp=a[low];

a[low]=a[j];

a[j]=temp;

return j;

void quicksort(int a[],int low,int high)

int j;

if(low<high)

j=partition(a,low,high);

quicksort(a,low,j-1);

quicksort(a,j+1,high);

void main()

int i,n,a[20];

printf("Enter the limit : ");

scanf("%d",&n);
printf("Enter the numbers to be sorted\n");

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

scanf("%d",&a[i]);

quicksort(a,0,n-1);

printf("\nThe sorted number are\n");

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

printf("%d\n",a[i]);

}
Program 18: Sort Given Array Using Selection Sort Technique

#include <stdio.h>

void selectionSort(int a[], int n)

int pos, i, j, temp;

for (i = 0; i < n - 1; i++)

pos = i;

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

if (a[j] < a[pos])

pos = j;

temp=a[i];

a[i]=a[pos];

a[pos]=temp;

void main()
{

int a[10], n, i;

printf("\nEnter the limit\n");

scanf("%d",&n);

printf("Enter array element\n");

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

scanf("%d",&a[i]);

selectionSort(a, n);

printf("Sorted array in ascending order:\n");

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

printf("%d\n", a[i]);

printf("\n");

}
Program 19: Implement Singly Linked List

#include<stdio.h>

#include<process.h>

#include<alloc.h>

struct node

int info;

struct node *link;

};

typedef struct node* NODE;

NODE getnode()

NODE x;

x=(NODE) malloc(sizeof(struct node));

if(x==NULL)

printf("Out of Memory\n");

exit(0);

return x;
}

void freenode(NODE x)

free(x);

NODE insert_front(int item, NODE first)

NODE temp;

temp=getnode();

temp->info=item;

temp->link=first;

return temp;

void display(NODE first)

NODE temp;

if(first==NULL)

printf("The list is empty\n");


return;

printf("The contents of the singly linked list \n");

temp=first;

while(temp!=NULL)

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

temp=temp->link;

printf("\n");

void main()

NODE first=NULL;

int ch,item;

do

printf("\n\t\nlinked List Operations\n");

printf("______\n");

printf("1)Insert\n2)Display\n3)Exit\n");

printf("______\n");
printf("Enter your choice : ");

scanf("%d",&ch);

switch(ch)

case 1: printf("Enter the item to be inserted : ");

scanf("%d",&item);

first=insert_front(item,first);

break;

case 2: display(first);

break;

default : exit(0);

}while(1);

}
Program 20: Implement Double Linked List

#include<stdio.h>

#include<process.h>

#include<stdlib.h>

struct node

int info;

struct node *rlink;

struct node *llink;

};

typedef struct node* NODE;

NODE getnode()

NODE x;

x = (NODE) malloc(sizeof(struct node));

if(x==NULL)

printf("Out of memory\n");

exit(0);

}
return x;

void freenode(NODE x)

free(x);

NODE insert_front(int item, NODE head)

NODE temp, cur;

temp = getnode();

temp->info = item;

cur = head->rlink;

head->rlink = temp;

temp->llink = head;

temp->rlink = cur;

cur->llink = temp;

printf("Item inserted Successfully\n");


return head;

NODE insert_rear(int item, NODE head)

NODE temp, cur;

temp = getnode();

temp->info = item;

cur = head->llink;

head->llink = temp;

temp->rlink = head;

temp->llink = cur;

cur->rlink = temp;

printf("Item inserted Successfully\n");

return head;

}
NODE delete_front(NODE head)

NODE cur, next;

if(head->rlink == head)

printf("List is empty\n");

return head;

cur = head->rlink;

next = cur->rlink;

head->rlink = next;

next->llink = head;

printf("The node to be deleted is %d\n",cur->info);

freenode( cur);

return head;

NODE delete_rear(NODE head)

{
NODE cur,prev;

if ( head->llink == head)

printf("List is empty\n");

return head;

cur = head->llink;

prev = cur->llink;

head->llink = prev;

prev->rlink = head;

printf("The node to be deleted is %d\n",cur->info);

freenode(cur);

return head;

void display(NODE head)

NODE temp;
if ( head->rlink == head)

printf("Deque is empty\n");

return;

printf("Contents of the deque is\n");

temp = head->rlink;

while (temp != head)

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

temp = temp->rlink;

printf("\n");

void main()

NODE head;

int choice, item;

head = getnode();

head->rlink = head;
head->llink = head;

for (;;)

printf("\nOperations on Doubly Linked List\n");

printf("\n*************\n");

printf("1:Insert Front\n2:Insert Rear\n");

printf("3:Delete Front\n4:Delete Rear\n");

printf("5:Display\n6:Exit\n");

printf("\n*************\n");

printf("Enter the choice : ");

scanf("%d",&choice);

switch( choice)

case 1:printf("Enter the item to be inserted : ");

scanf("%d",&item);

head = insert_front(item,head);

break;

case 2:printf("Enter the item to be inserted : ");

scanf("%d",&item);

head = insert_rear(item,head);

break;

case 3:head = delete_front(head);

break;

case 4: head = delete_rear(head);

break;
case 5: display(head);

break;

default:printf("Exit");

exit(0);

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy