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

Transpose of A Sparse Matrix

The document discusses several Java programs related to data structures and algorithms: 1. The first section discusses a program to find the transpose of a sparse matrix represented in sparse format. It takes in a sparse matrix, finds its transpose, and displays the transpose. 2. The second section discusses a program to add two polynomials represented using linked lists. It defines a linked list node class Link and linked list class LinkList to store coefficients and exponents. It then defines methods to add two polynomials and display the result. 3. The remaining sections discuss programs for stack implementation using arrays, infix to postfix conversion, parsing postfix expression, and a shopping list application using arrays.

Uploaded by

jaseem
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Transpose of A Sparse Matrix

The document discusses several Java programs related to data structures and algorithms: 1. The first section discusses a program to find the transpose of a sparse matrix represented in sparse format. It takes in a sparse matrix, finds its transpose, and displays the transpose. 2. The second section discusses a program to add two polynomials represented using linked lists. It defines a linked list node class Link and linked list class LinkList to store coefficients and exponents. It then defines methods to add two polynomials and display the result. 3. The remaining sections discuss programs for stack implementation using arrays, infix to postfix conversion, parsing postfix expression, and a shopping list application using arrays.

Uploaded by

jaseem
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

TRANSPOSE OF A SPARSE MATRIX

import java.io.*;
class Tsparse
{
public static void main(String args[])throws IOException
{
int m,n,i,j,t=0,k,l;
InputStreamReader obj=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(obj);
System.out.println("ENTER THE ORDER OF THE ARRAY :");
i=Integer.parseInt(br.readLine());
j=Integer.parseInt(br.readLine());
int a[][]=new int [10][20];
int b[][]=new int [10][20];
System.out.println("ENTER THE SPARSE REPRESENTATION OF THE MATRIX :");
for(k=0;k<i;k++)
{
for(l=0;l<j;l++)
{
a[k][l]=Integer.parseInt(br.readLine());
}
}
m=a[0][0]; n=a[0][1]; t=a[0][2];
b[0][0]=n; b[0][1]=m; b[0][2]=t;
System.out.println(" THE SPARSE REPRESENTATION OF THE MATRIX :");
for(k=0;k<i;k++)
{
for(l=0;l<j;l++)
{
System.out.print(" "+a[k][l]);
}
System.out.println();
}
if(t>0)
{
System.out.println("TRANSPOSE OF MATRIX : ");
int q=1;
for(int col=1;col<=n;col++)
{
for(int 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=q+1;
}
}
}
for(k=0;k<i;k++)
{

for(l=0;l<j;l++)
{
System.out.print(" "+b[k][l]);
}
System.out.println();
}
}
}
}

POLYNOMIAL ADDITION
import java.io.*;
class Link
{
int coef;
int exp;
Link next;
public Link(int c,int e)
{
coef=c;
exp=e;
}
public void displayLink()
{
System.out.print(coef+"x^"+exp);
}
}
class LinkList
{
public Link first;
public Link last;
public LinkList()
{
first=null;
last=null;
}
public void displaypoly()
{
Link current=first;
while(current!=null)
{
current.displayLink();
current=current.next;
if(current!=null)
System.out.print("+");
}
}

public boolean isEmpty()


{
return(first==null);
}
public void insertLast(int c,int e)
{
Link nl=new Link(c,e);
if(isEmpty())
first=nl;
else
last.next=nl;
last=nl;
}
public void polyaddn(LinkList a,LinkList b)
{
Link p=a.first;
Link q=b.first;
int x;
while((p!=null)&&(q!=null))
{
if(p.exp==q.exp)
{
x=p.coef+q.coef;
if(x!=0)
insertLast(x,p.exp);
p=p.next;
q=q.next;
}
else if(p.exp<q.exp)
{
insertLast(q.coef,q.exp);
q=q.next;
}
else if(p.exp>q.exp)
{
insertLast(p.coef,p.exp);
p=p.next;
}
}
while(p!=null)
{
insertLast(p.coef,p.exp);
p=p.next;
}
while(q!=null)
{
insertLast(q.coef,q.exp);
q=q.next;
}
}
}
class PolyAdd
{

public static void main(String args[])throws IOException


{
LinkList a=new LinkList();
LinkList b=new LinkList();
LinkList c=new LinkList();
InputStreamReader obj=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(obj);
System.out.println("ENTER THE NO. OF TERMS IN THE 1ST POLYNOMIAL :");
int n1=Integer.parseInt(br.readLine());
for(int i=1;i<=n1;i++)
{
System.out.println("ENTER COEFFICIENT OF TERMS :"+i);
int co1=Integer.parseInt(br.readLine());
System.out.println("ENTER EXPONENTS OF TERMS :"+i);
int ex1=Integer.parseInt(br.readLine());
a.insertLast(co1,ex1);
}
System.out.println("1st polynomial");
a.displaypoly();
System.out.println("\nEnter the no.of termsin 2nd polynomial");
int n2=Integer.parseInt(br.readLine());
for(int i=1;i<=n2;i++)
{
System.out.println("ENTER COEFFICIENT OF TERMS :"+i);
int co2=Integer.parseInt(br.readLine());
System.out.println("ENTER EXPONENTS OF TERMS :"+i);
int ex2=Integer.parseInt(br.readLine());
b.insertLast(co2,ex2);
}
System.out.println("2nd polynomial");
b.displaypoly();
System.out.println("\nafter addition....");
c.polyaddn(a,b);
Link current=c.first;
while(current!=null)
{
current.displayLink();
System.out.print("+");
current=current.next;}
}
}
STACK USING ARRAY
import java.io.*;
class StackX
{
private int maxSize;
private double[] stackArray;
private int top;
public StackX(int s)
{
maxSize=s;

stackArray= new double[maxSize];


top=-1;
}
public void push(double j)
{ stackArray[++top]=j; }
public double pop()
{ return stackArray[top--]; }
public double peek()
{ return stackArray[top];}
public void showStack()
{
for(int i=top;i>=0;i--)
System.out.println((stackArray[i]));
}
public boolean isEmpty()
{
if(top==-1)
return true;
else
return false;
}
}
class StackApp
{
public static void main(String[] args)throws IOException
{
char c;
StackX a=new StackX(10);
do
{
InputStreamReader obj=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(obj);
System.out.println("STACK OPERATIONS : \n1.PUSH\n2.POP\n3.DISPLAY\nENTER UR
CHOICE");
int ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1:
System.out.println("HOW MANY ELEMENTS :");
int n=Integer.parseInt(br.readLine());
System.out.println("THE ELEMENTS ARE:");
for(int i=0;i<n;i++)
{
int d=Integer.parseInt(br.readLine());
a.push(d);
}
System.out.println("CURRENT STACK TOP:-"+a.peek());
break;
case 2:
System.out.println("HOW MANY ELEMENTS :");
int m=Integer.parseInt(br.readLine());
for(int i=0;i<m;i++)
{
a.pop();

}
System.out.println("CURRENT STACK TOP:-"+a.peek());
break;
case 3:
System.out.println("THE ELEMENTS ARE :");
a.showStack();
break;
default:
System.out.println("WRONG CHOICE!!!!!");
}
System.out.println("DO YOU WANNA CONTINUE ??? (Y/N)");
c=(char)br.read();
}while(c=='y'||c=='Y');
}
}

INFIX TO POSTFIX CONVERSION AND EVALUATION


import java.io.*;
class StackX
{
private int maxSize;
private char[] stackArray;
private int top;
public StackX(int s)
{
maxSize = s;
stackArray = new char[maxSize];
top = -1;
}
public void push(char j)
{ stackArray[++top] = j; }
public char pop()
{ return stackArray[top--]; }
public char peek()
{ return stackArray[top]; }
public boolean isEmpty()
{ return (top == -1); }
public int size()
{ return top+1; }
public char peekN(int n)
{ return stackArray[n]; }
public void displayStack(String s)
{
System.out.print(s);
System.out.print("Stack (bottom-->top): ");
for(int j=0; j<size(); j++)
{

System.out.print( peekN(j) );
System.out.print(' ');
}
System.out.println("");
}
}
class InToPost
{
private StackX theStack;
private String input;
private String output = "";
public InToPost(String in)
{
input = in;
int stackSize = input.length();
theStack = new StackX(stackSize);
}
public String doTrans()
{
for(int j=0; j<input.length(); j++)
{
char ch = input.charAt(j);
theStack.displayStack("For "+ch+" ");
switch(ch)
{
case '+':
case '-':
gotOper(ch, 1);
break;
case '*':
case '/':
gotOper(ch, 2);
break;
case '(':
theStack.push(ch);
break;
case ')':
gotParen(ch);
break;
default:
output = output + ch;
break;
}
}
while( !theStack.isEmpty() )
{
theStack.displayStack("While ");
output = output + theStack.pop();
}
theStack.displayStack("End ");
return output;
}
public void gotOper(char opThis, int prec1)
{

while( !theStack.isEmpty() )
{
char opTop = theStack.pop();
if( opTop == '(' )
{
theStack.push(opTop);
break;
}
else
{
int prec2;
if(opTop=='+' || opTop=='-')
prec2 = 1;
else
prec2 = 2;
if(prec2 < prec1)
{
theStack.push(opTop);
break;
}
else
output = output + opTop;
}
}
theStack.push(opThis);
}
public void gotParen(char ch)
{
while( !theStack.isEmpty() )
{
char chx = theStack.pop();
if( chx == '(' )
break;
else
output = output + chx;
}
}
}
class StackXX
{
private int maxSize;
private int[] stackArray;
private int top;
public StackXX(int size)
{
maxSize = size;
stackArray = new int[maxSize];
top = -1;
}
public void push(int j)
{ stackArray[++top] = j; }
public int pop()
{ return stackArray[top--]; }
public int peek()

{ return stackArray[top]; }
public boolean isEmpty()
{ return (top == -1); }
public boolean isFull()
{ return (top == maxSize-1); }
public int size()
{ return top+1; }
public int peekN(int n)
{ return stackArray[n]; }
public void displayStack(String s)
{
System.out.print(s);
System.out.print("Stack (bottom-->top): ");
for(int j=0; j<size(); j++)
{
System.out.print( peekN(j) );
System.out.print(' ');
}
System.out.println("");
}
}
class ParsePost
{
private StackXX theStack;
private String input;
public ParsePost(String s)
{ input = s; }
public int doParse()
{
theStack = new StackXX(20);
char ch;
int j;
int num1, num2, interAns;
for(j=0; j<input.length(); j++)
{
ch = input.charAt(j);
theStack.displayStack(""+ch+" ");
if(ch >= '0' && ch <= '9')
theStack.push( (int)(ch-'0') );
else
{
num2 = theStack.pop();
num1 = theStack.pop();
switch(ch)
{
case '+':
interAns = num1 + num2;
break;
case '-':
interAns = num1 - num2;
break;
case '*':
interAns = num1 * num2;
break;

case '/':
interAns = num1 / num2;
break;
default:
interAns = 0;
}
theStack.push(interAns);
}
}
interAns = theStack.pop();
return interAns;
}
}
class InpostApp
{
public static void main(String[] args) throws IOException
{
String input1, output1;
int output2;
while(true)
{
System.out.print("Enter infix: ");
System.out.flush();
input1 = getString();
if( input1.equals("") )
break;
InToPost theTrans = new InToPost(input1);
output1 = theTrans.doTrans();
System.out.println("Postfix is " + output1 + '\n');
ParsePost aParser = new ParsePost(output1);
output2 = aParser.doParse();
System.out.println("Evaluates to " + output2);
if( output1.equals("") )
break;
}
}
public static String getString() throws IOException
{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String s = br.readLine();
return s;
}
}

SHOPPING LIST OF ITEMS


import java.io.*;
class Shopping
{

private String a[];


private int nElems;
public Shopping(int max)
{
a=new String[max];
nElems=0;
}
public void insert(String str)
{
a[nElems]=str;
nElems++;
}
public void findpos(String item,int pos)
{
for(int i=nElems-1;i>=pos;i--)
a[i+1]=a[i];
a[pos]=item;
nElems++;
}
public void delete(String item)
{
for(int i=0;i<nElems;i++)
{
if(a[i].compareTo(item)==0)
{
for(int j=i;j<nElems;j++)
{
a[j]=a[j+1];
}
nElems--;
break;
}
}
}
public void display()
{
System.out.println("CONTENT OF THE ARRAY IS : ");
for(int i=0;i<nElems;i++)
System.out.println(a[i]);
}
}
class ShoppingList
{
public static void main(String args[])throws IOException
{
int maxsize=100;
char c;
Shopping S;
S=new Shopping(maxsize);
String t;
do
{

InputStreamReader obj=new InputStreamReader(System.in);


BufferedReader br=new BufferedReader(obj);
System.out.println("ENTER UR CHOICE:\n1.INSERT\n2.INSERT AT SPECIFIED
LOCATION\n3.DELETE\n4.DISPLAY \n ");
int ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1:
System.out.println("HOW MANY ELEMENTS : ");
int n=Integer.parseInt(br.readLine());
String[] item=new String[n];
System.out.println("ENTER ITEMS : ");
for(int i=0;i<n;i++)
{
item[i]=br.readLine();
S.insert(item[i]);
}
break;
case 2:
System.out.println("ENTER THE ELEMENT TO BE INSERTED AND ITS POSITION ");
t=br.readLine();
int pos=Integer.parseInt(br.readLine());
S.findpos(t,pos);
break;
case 3:
System.out.println("ENTER THE ELEMENT TO BE DELETED : ");
t=br.readLine();
S.delete(t);
break;
case 4:
S.display();
break;
default:
System.out.println("WRONG CHOICE!!!");
}
System.out.println("DO YOU WANNA CONTINUE ??? (Y/N)");
c=(char)br.read();
}while(c=='y'||c=='Y');
}
}

You might also like