-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinked List Implementaion.cpp
More file actions
89 lines (79 loc) · 1.63 KB
/
Linked List Implementaion.cpp
File metadata and controls
89 lines (79 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include<iostream>
using namespace std;
class Node
{
public:
int row;
int col;
int data;
Node *next;
};
void createNewNode(Node **p, int rowIndex, int colIndex, int x)
{
Node *temp = *p;
Node *r;
if (temp == NULL)
{
temp = new Node();
temp->row = rowIndex;
temp->col = colIndex;
temp->data = x;
temp->next = NULL;
*p = temp;
}
else
{
while (temp->next != NULL)
temp = temp->next;
r = new Node();
r->row = rowIndex;
r->col = colIndex;
r->data = x;
r->next = NULL;
temp->next = r;
}
}
void printList(Node *start)
{
Node *ptr = start;
cout << "row_position:";
while (ptr != NULL)
{
cout << ptr->row << " ";
ptr = ptr->next;
}
cout << endl;
cout << "column_position:";
ptr = start;
while (ptr != NULL)
{
cout << ptr->col << " ";
ptr = ptr->next;
}
cout << endl;
cout << "Value:";
ptr = start;
while (ptr != NULL)
{
cout << ptr->data << " ";
ptr = ptr->next;
}
}
int main()
{
int sparseMatrix[4][5] = { { 0 , 0 , 3 , 0 , 4 },
{ 0 , 0 , 5 , 7 , 0 },
{ 0 , 0 , 0 , 0 , 0 },
{ 0 , 2 , 6 , 0 , 0 } };
Node *first = NULL;
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 5; j++)
{
if (sparseMatrix[i][j] != 0)
createNewNode(&first, i, j, sparseMatrix[i][j]);
}
}
printList(first);
return 0;
}