0% found this document useful (0 votes)
245 views

Data Structures C

The document contains programs demonstrating different data structures: 1) The first program shows how to implement a matrix transpose in sparse form using arrays. 2) The second program demonstrates stack operations like push and pop using arrays. 3) The third program implements a stack using linked lists with functions for push, pop, and display. 4) The fourth program shows queue implementation using arrays with functions for insert and delete. 5) The fifth program implements a circular queue with array-based storage and functions for insert and delete. 6) The sixth program demonstrates a queue using a linked list structure and functions for addition and deletion.

Uploaded by

srinath45
Copyright
© Attribution Non-Commercial (BY-NC)
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)
245 views

Data Structures C

The document contains programs demonstrating different data structures: 1) The first program shows how to implement a matrix transpose in sparse form using arrays. 2) The second program demonstrates stack operations like push and pop using arrays. 3) The third program implements a stack using linked lists with functions for push, pop, and display. 4) The fourth program shows queue implementation using arrays with functions for insert and delete. 5) The fifth program implements a circular queue with array-based storage and functions for insert and delete. 6) The sixth program demonstrates a queue using a linked list structure and functions for addition and deletion.

Uploaded by

srinath45
Copyright
© Attribution Non-Commercial (BY-NC)
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
You are on page 1/ 10

PROGRAMS ON DATA STRUCTURE

Program : Program for finding the transpose of a martix in sparse form.


/* Program for finding the transpose of a martix in sparse form*/
#include
#include
int a[100][100],b[100][100];

void main()
{
int i,m,n,p,q,col,t;
clrscr();
printf("Enter the no. of rows");
scanf("%d", &a[0][0]);
printf("\nEnter the no. of cols");
scanf("%d", &a[0][1]);
printf("\nEnter the number of non zero terms");
scanf("%d", &a[0][2]);

for(i=1;i<=a[0][2];i++)
{
printf("\nEnter the value (that is non zero)");
scanf("%d",&a[i][2]);
printf("\nEnter the row for %d : ",a[i][2]);
scanf("%d",&a[i][0]);
printf("\nEnter the col for %d : ",a[i][2]);
scanf("%d",&a[i][1]);
}

/* Printing for testing the sparse input */


printf("\n *****************************\n The martix you entered is \n
************************\n\n Row \t Col \t Value \t");
for ( i = 0;i <= a[0][2];i++)
{
printf("\n %d \t %d \t %d \t " , a[i][0],a[i][1],a[i][2]);
}

/* Calling function for evaluation of transpose */

m = a[0][0];
n = a[0][1];
t = a[0][2];

b[0][0] = n;
b[0][1] = m;
b[0][2] = t;

q=1;

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


{
for(p = 1; p<=t;p++)
{
if(a[p][1] == col)
{
b[q][0] = a[p][1];
b[q][1] =a[p][0];
b[q][2] = a[p][2];
q++;
}
}
} //end of outer for loop

/* Printing the transposed matrix */

getch();
printf("\nThe Transpose of the above matrix is ");
for ( i = 0;i <= a[0][2];i++)
{
printf("\n %d \t %d \t %d \t " , b[i][0],b[i][1],b[i][2]);
}
getch();
}

Program : Stack operations through array.


///////////////////////////////////////////////////////////////////
// STACK IMPLEMENTATION THROUGH ARRAY //
// PUSH . POP //
//////////////////////////////////////////////////////////////////

#include
#include
# define MAXSIZE 200

int stack[MAXSIZE];
int top; //index pointing to the top of stack
void main()
{
void push(int);
int pop();
int will=1,i,num;
clrscr();
printf("\n\t\tProgram for stack demonstration through array by Amit
Mathur and Aditya Tiwari.\n\n\n");
while(will ==1)
{
printf("\n\t\tMAIN MENU: \n\t1.Add element to stack\n\t2.Delete element
from the stack\n");
scanf("%d",&will);

switch(will)
{
case 1:
printf("\nEnter the data... ");
scanf("%d",&num);
push(num);
break;
case 2: i=pop();
printf("\nValue returned from pop function is %d ",i);
break;
default: printf("Invalid Choice . ");
}

printf(" \n\n\t\t\tDo you want to do more operations on Stack ( 1 for


yes, any other key to exit) ");
scanf("%d" , &will);
} //end of outer while
} //end of main

void push(int y)
{

if(top>MAXSIZE)
{
printf("\n\n\t\tSTACK FULL\n\n");
return;
}
else
{
top++;
stack[top]=y;
}
}

int pop()
{
int a;
if(top<=0)
{
printf("\n\n\t\tSTACK EMPTY\n\n\t\t");
return 0;
}
else
{
a=stack[top];
top--;
}
return(a);

Program : Stack implementation through linked list.


///////////////////////////////////////////////////////////////////////
////
// STACK AS LINKED LIST
//
// PUSH . POP . DISPLAY
//
///////////////////////////////////////////////////////////////////////
////

#include
#include
# include "malloc.h"
struct node
{
int data;
struct node *link;
} ;
struct node *top;

void main()
{
void push(int);
void display();
int wish, num,will,a;
wish = 1;
top = NULL;
clrscr();
printf("Program for Stack as Linked List demo..\n");
while(wish == 1)
{
printf("\n\t\t\tMain Menu\n1.Enter data in stack \n2.Delete from
stack\n");
scanf("%d",&will);
switch(will)
{
case 1:
printf("\nEnter the data");
scanf("%d",&num);
push(num);
display();
break;
case 2:
a=pop();
printf("\nValue returned from top of the stack is
%d",a);
break;
default:
printf("\nInvalid choice");
}
printf("\nDo you want to continue, press 1");
scanf("%d",&wish);
}
}

void push(int y)
{
struct node *x;
x=malloc(sizeof(struct node));
printf("\n Address of newly created node x is %d",x);
x->data = y;
x->link = top;
top = x;
}
void display()
{
int i =0;
struct node * temp;
temp = top;

while(temp!=NULL)
{
printf("\nItem No. %d : Data %d Link %d ",i++,temp-
>data,temp->link);
temp=temp->link;
}
}

/// THIS FUNCTION REMOVES TOP NODE FROM THE STACK AND RETURNS ITS
VALUE///

int pop()
{
int a;
if(top==NULL)
{printf("\n\t\tSTACK EMPTY...\n\n"); return 0;}
else
{
a=top->data;
printf("The value returned is %d ",a);
free(top);
top=top->link; return (a);
}
}

Program : Queue implementation through array.


///////////////////////////////////////////////////////////////////
// QUEUE IMPLEMENTATION THROUGH ARRAY //
// INSERT . DELETE //
/////////////////////////////////////////////////////////////////

#include
#include
# define MAXSIZE 200

int q[MAXSIZE];
int front, rear;
void main()
{
void add(int);
int del();
int will=1,i,num;
front =0;
rear = 0;

clrscr();

printf("\n\t\tProgram for queue demonstration through array\n\n\n");

while(will ==1)
{
printf("\n\t\tMAIN MENU: \n\t1.Add element to queue\n\t2.Delete element
from the queue\n");
scanf("%d",&will);

switch(will)
{
case 1:
printf("\nEnter the data... ");
scanf("%d",&num);
add(num);
break;
case 2: i=del();
printf("\nValue returned from delete function is %d ",i);
break;
default: printf("Invalid Choice ... ");
}

printf(" \n\n\t\t\tDo you want to do more operations on Queue ( 1 for


yes, any other key to exit) ");
scanf("%d" , &will);
} //end of outer while
} //end of main

void add(int a)
{

if(rear>MAXSIZE)
{
printf("\n\n\t\tQUEUE FULL\n\n");
return;
}
else
{
q[rear]=a;
rear++;
printf("\n Value of rear = %d and the value of front is
%d",rear,front);
}
}

int del()
{
int a;
if(front == rear)
{
printf("\n\n\t\tQUEUE EMPTY\n\n");
return(0);
}
else
{
a=q[front];
front++;
}
return(a);
}

Program : Circular Queue implementation through array.


///////////////////////////////////////////////////////////////////////
////
// CIRCULAR QUEUE IMPLEMENTATION THROUGH ARRAY //
// INSERT . DELETE
//
////////////////////////////////////////////////////////////////////

#include
#include
# define MAXSIZE 200

int cq[MAXSIZE];
int front,rear;

void main()
{
void add(int,int [],int,int,int);
int del(int [],int ,int ,int );
int will=1,i,num;
front = 1;
rear = 1;

clrscr();
printf("\n\t\tProgram for Circular Queue demonstration through
array\n\n\n");
while(will ==1)
{
printf("\n\t\tMAIN MENU: \n\t1.Add element to Circular
Queue\n\t2.Delete element from the Circular Queue\n");
scanf("%d",&will);

switch(will)
{
case 1:
printf("\nEnter the data... ");
scanf("%d",&num);
add(num,cq,MAXSIZE,front,rear);
break;
case 2: i=del(cq,MAXSIZE,front,rear);
printf("\nValue returned from delete function is %d ",i);
break;
default: printf("\n\nInvalid Choice . ");
}
printf(" \n\n\t\t\tDo you want to do more operations on Circular Queue
( 1 for yes, any other key to exit) ");
scanf("%d" , &will);
} //end of outer while
} //end of main

void add(int item,int q[],int MAX,int front,int rear)


{
rear++;
rear= (rear%MAX);
if(front ==rear)
{
printf("\n\n\t\tCIRCULAR QUEUE FULL\n\n");
return;
}
else
{
cq[rear]=item;
printf("\nRear = %d Front = %d ",rear,front);
}
}
int del(int q[],int MAX,int front,int rear)
{
int a;
if(front == rear)
{
printf("\n\n\t\tCIRCULAR STACK EMPTY\n\n");
return (0);
}
else
{
front++;
front = front%MAX;
a=cq[front];
return(a);
printf("\nRear = %d Front = %d ",rear,front);
}
}

Program : Queue implementation through linked list.


///////////////////////////////////////////////////////////////////
// QUEUE AS LINKED LIST //
// ADDITION . DELETION //
//////////////////////////////////////////////////////////////////

#include
#include
struct node
{
int data;
struct node *link;
} ;
struct node *front, *rear;
void main()
{

int wish,will,a,num;
void add(int);

wish=1;
clrscr();
front=rear=NULL;

printf("Program for Queue as Linked List demo..\n");


while(wish == 1)
{
printf("\n\t\t\tMain Menu\n1.Enter data in queue \n2.Delete from
queue\n");
scanf("%d",&will);
switch(will)
{
case 1:
printf("\nEnter the data");
scanf("%d",&num);
add(num);
//display();
break;
case 2:
a=del();
printf("\nValue returned from front of the queue
is %d",a);
break;
default:
printf("\nInvalid choice");
}
printf("\nDo you want to continue, press 1");
scanf("%d",&wish);
}
getch();
}

void add(int y)
{
struct node *ptr;
ptr=malloc(sizeof(struct node));
ptr->data=y;
ptr->link=NULL;
if(front ==NULL)
{
front = rear= ptr;
}
else
{
rear->link=ptr;
rear=ptr;
}
}
int del()
{
int num;
if(front==NULL)
{
printf("\n\n\t\tQUEUE EMPTY\n\n");
return(0);
}
else
{
num=front->data;
front = front->link;
printf("\n Value returned by delete function is %d \n",num);
return(num);
}
}

You might also like