Vous êtes sur la page 1sur 8

1.WAP for create a sparse matrix from a non sparse matrix.

Use yales method


for sparse matrix representation.
//Sparse Matrix Implementation using yales method
#include<cstdlib>
#include<iostream>
#include"iomanip"
#include"time.h"
using namespace std;

int main(int argc, char** argv) {

int a[5][5];
int nnz = 0;
cout <<"Non Sparse Matrix Array is ->\n";
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
int val = rand() % 10;
a[i][j] = (val % 2 == 0) ? val * 10 : 0;
if (a[i][j] != 0)
nnz++;
cout << a[i][j] <<"";
}
}

cout << endl;


cout <<"non zero elements are:"<< nnz << endl;
cout <<"Sparse Matrix using Yales method->\n";
int sm[13][3];
int rindex = 0, cindex = 0;
sm[0][0] = 5;
sm[0][1] = 5;
sm[0][2] = nnz;
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (a[i][j] != 0) {
sm[rindex][0] = i;
sm[rindex][1] = j;
sm[rindex][2] = a[i][j];
cout << sm[rindex][0] <<""<< sm[rindex][1] <<""<< sm[rindex][2] << endl;
rindex++;
}
}
}

return 0;
}

2. WAP for implementing stack using array and pointers.

A)Stack Implementation using Array

//Stack Implementation using Array


#include<cstdlib>
#include<iostream>
#define MAX 10
using namespace std;

class Stack {
int top;
int A[MAX];
public:

Stack() {
top = -1;
}
bool IsEmpty();
bool IsFull();
void Push(int);
int Pop();
};

bool Stack::IsEmpty() {
if (top<= -1)
return true;
return false;
}

bool Stack::IsFull() {
if (top>= MAX - 1)
return true;
return false;
}

void Stack::Push(int val) {


if (!IsFull()) {
A[++top] = val;
} else
cout <<"Stack is Overflow.\n";
}

int Stack::Pop() {
if (!IsEmpty()) {
return A[top--];
}
}

int main(int argc, char** argv) {


Stack s1;
int num[100];
int n;
cout <<"Enter how many elements to insert->";
cin>>n;
cout <<"Push elements->";
for (int i = 0; i < n; i++) {
cin >> num[i];
}
for (int i = 0; i < n; i++) {
s1.Push(num[i]);
}
cout <<"Displaying the pushed elements->\n";
for (int i = 0; i < n; i++) {
cout << s1.Pop() <<"";
}
return 0;
}

B) Stack Implementation by using of Pointers


//SLL.h
//linked Header for Stack
#ifndef SLL_H
#define SLL_H

struct node {
int info;
node *next;
};

class SLL {
public:
SLL();
SLL(const SLL& orig);
virtual ~SLL();
bool IsEmpty();
void Create(int val);
void Insert_beg(int val);
void Insert_end(int val);
void Insert_pos(int val);
int Delete_beg();
int Delete_end();
void Destory();
void Traverse();
private:
node *list;
};

class Cstack : public SLL {


public:
void Push(int val);
int Pop();
void Display();
};
#endif /* SLL_H */

#include"SLL.h"
#include<iostream>
using namespace std;

SLL::SLL() {
list = NULL;
}

SLL::SLL(const SLL& orig) {


}

SLL::~SLL() {
Destory();
}

bool SLL::IsEmpty() {
if (list == NULL) {
return true;
return false;
}
}

void SLL::Create(int val) {


node *p;
p = new node;
p->info = val;
p->next = NULL;
list = p;
}

void SLL::Insert_beg(int val) {


if (!IsEmpty()) {
node *p;
p = new node;
p->info = val;
p->next = list;
list = p;
}
}

void SLL::Insert_end(int val) {


if (IsEmpty()) {
Create(val);
} else {
node *p;
p = new node;
p->info = val;
p->next = NULL;
node *q = list;
while (q->next != NULL)
q = q->next;
q->next = p;
}
}

int SLL::Delete_beg() {
int val = -1;
if (!IsEmpty()) {
node *q = list;
list = list->next;
val = q->info;
delete q;
}
return val;
}

int SLL::Delete_end() {
int val = -1;
if (!IsEmpty()) {
if (list->next != NULL)
val = Delete_beg();
else {
node *q = list;
while (q->next != NULL)
q = q->next;
node *p = q->next;
q->next = NULL;
val = p->info;
delete p;

}
}
return val;
}

void SLL::Traverse() {
if (!IsEmpty()) {
node *q = list;
while (q != NULL) {
cout << q->info<<"->";
q = q->next;
}
cout <<"NULL\n";
}
}

void SLL::Destory() {
if (!IsEmpty()) {
Delete_beg();
}
}

Vous aimerez peut-être aussi