Data Structures C
Data Structures C
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]);
}
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;
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();
}
#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 . ");
}
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);
#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);
}
}
#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();
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 ... ");
}
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);
}
#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
#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;
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);
}
}