Skip to content

Commit 33a896a

Browse files
author
Ashutosh Trivedi
committed
done
1 parent aea568b commit 33a896a

27 files changed

+492
-2
lines changed

lec06/program33-m.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,18 @@ using namespace std;
44

55
int main(int argc, char* argv[]) {
66
int x = 7;
7+
78
//int& y; // error
89
//int& y = 7; // error
910
int& y = x; // once and for all bound to x
1011

1112
cout << "x = " << x << " Add of x:" << &x << endl;
12-
cout << "y = " << x << " Add of y: " << &y << endl;
13+
cout << "y = " << y << " Add of y: " << &y << endl;
1314

1415
y++;
1516

1617
cout << "x = " << x << " Add of x:" << &x << endl;
17-
cout << "y = " << x << " Add of y: " << &y << endl;
18+
cout << "y = " << y << " Add of y: " << &y << endl;
1819

1920
return 0;
2021
}

lec08/a.out

19.2 KB
Binary file not shown.

lec08/fancy

19.2 KB
Binary file not shown.

lec08/first.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include<iostream>
2+
#include "second.hpp"
3+
4+
using namespace std;
5+
6+
7+
int main() {
8+
9+
cout << " Inside main" << endl;
10+
foo();
11+
12+
return 0;
13+
14+
}

lec08/first.o

13.2 KB
Binary file not shown.

lec08/second.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include<iostream>
2+
using namespace std;
3+
4+
void foo() {
5+
cout << "Inside function foo()" << endl;
6+
7+
}

lec08/second.hpp

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef __SECOND_HPP__
2+
#define __SECOND_HPP__
3+
4+
// declaring function foo
5+
void foo();
6+
static int x = 0;
7+
8+
9+
10+
11+
12+
#endif // __SECOND_HPP__

lec08/second.o

13.2 KB
Binary file not shown.

lec09/LinkedList.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#include<iostream>
2+
#include "LinkedList.hpp"
3+

lec09/LinkedList.hpp

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#ifndef __LINKEDLIST_HPP__
2+
#define __LINKEDLIST_HPP__
3+
4+
// Define data-type of a single node
5+
struct Node {
6+
int data; // value of the current node
7+
Node* next; // address of the next node
8+
};
9+
10+
class LinkedList {
11+
private:
12+
Node* head;
13+
Node* tail;
14+
public:
15+
LinkedList();
16+
~LinkedList();
17+
void traverse();
18+
19+
}
20+
21+
#endif /* __LINKEDLIST_HPP__*/

lec09/a.out

18.7 KB
Binary file not shown.

lec09/main.cpp

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include<iostream>
2+
using namespace std;
3+
4+
struct Node {
5+
int value;
6+
Node* next;
7+
};
8+
int main() {
9+
10+
Node* tmp = new Node();
11+
(*tmp).value = 20;
12+
(*tmp).next=0;
13+
14+
Node* tmp2 = new Node();
15+
tmp2->value = 30;
16+
tmp2->next= tmp;
17+
18+
Node* tmp3 = new Node();
19+
tmp3->value = 40;
20+
tmp3->next= tmp2;
21+
22+
Node* head = tmp3;
23+
while (head != 0) {
24+
cout << head->value << " ";
25+
head = head->next;
26+
}
27+
cout << endl;
28+
29+
30+
return 0;
31+
32+
}

lec10/LinkedList.cpp

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include<iostream>
2+
#include "LinkedList.hpp"
3+
using namespace std;
4+
5+
LinkedList::LinkedList() {
6+
head = 0;
7+
tail = 0;
8+
}
9+
10+
LinkedList::~LinkedList() {
11+
12+
for (Node* curr = head; curr !=0;) {
13+
Node* tmp = curr->next;
14+
delete curr;
15+
curr = tmp;
16+
}
17+
18+
head = 0;
19+
tail = 0;
20+
21+
}
22+
23+
void LinkedList::traverse() {
24+
cout << "[ ";
25+
for (Node* tmp = head; tmp != 0; tmp = tmp->next) {
26+
cout << tmp->data << " ";
27+
}
28+
cout << "]" << endl;
29+
}
30+
31+
Node* LinkedList::search(int item) {
32+
33+
for (Node* tmp = head; tmp != 0; tmp = tmp->next) {
34+
if (tmp->data == item) return tmp;
35+
}
36+
37+
return 0;
38+
}
39+
40+
void LinkedList::insert(Node* left, int item) {
41+
Node* tmp = new Node(item, 0);
42+
43+
if (left == 0) {
44+
tmp->next = head;
45+
head = tmp;
46+
if (tail == 0) tail = head;
47+
}
48+
else if (left == tail) {
49+
tail->next = tmp;
50+
tail = tmp;
51+
}
52+
else {
53+
54+
}
55+
56+
57+
return;
58+
}
59+
60+
void LinkedList::remove(Node* pos) {
61+
62+
63+
return;
64+
}

lec10/LinkedList.hpp

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#ifndef __LINKEDLIST_HPP__
2+
#define __LINKEDLIST_HPP__
3+
4+
// Define data-type of a single node
5+
struct Node {
6+
int data; // value of the current node
7+
Node* next; // address of the next node
8+
9+
Node() {next = 0;}
10+
Node(int d, Node* n) {data = d; next = n;}
11+
};
12+
13+
class LinkedList {
14+
private:
15+
Node* head;
16+
Node* tail;
17+
18+
public:
19+
LinkedList();
20+
~LinkedList();
21+
void traverse();
22+
Node* search(int item);
23+
24+
void insert(Node* left, int item); // precondition: left is in the list
25+
26+
void remove(Node* pos); // precondition: pos is in the list
27+
28+
};
29+
30+
#endif /* __LINKEDLIST_HPP__*/

lec10/LinkedList.o

15.4 KB
Binary file not shown.

lec10/Makefile

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
all: main.o LinkedList.o
2+
g++ -o lists main.o LinkedList.o
3+
main.o: main.cpp LinkedList.hpp
4+
g++ -c main.cpp
5+
LinkedList.o: LinkedList.cpp LinkedList.hpp
6+
g++ -c LinkedList.cpp
7+
clean:
8+
rm -rf lists main.o LinkedList.o

lec10/a.out

19.1 KB
Binary file not shown.

lec10/array.cpp

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#include<iostream>
2+
using namespace std;
3+
// Fun with Array Doubling
4+
5+
//passing array normally
6+
void double1(int* C, int size);
7+
8+
//passing array by Reference
9+
void double2(int* &A, int size);
10+
11+
//passing array by pointer
12+
void double3(int* *A, int size);
13+
14+
int main() {
15+
int* A = new int[2];
16+
A[0] = 22;
17+
A[1] = 33;
18+
19+
cout << "Address of A (before):" << A << endl;
20+
double1(A, 2);
21+
// double2(A, 2);
22+
// double3(&A, 2);
23+
cout << "Address of A (after):" << A << endl;
24+
25+
for (int i=0; i<4; i++) cout << A[i] << " ";
26+
cout << endl;
27+
28+
return 0;
29+
}
30+
31+
void double1(int* A, int size) {
32+
int* B = new int[2*size];
33+
cout << "Address of B:" << B << endl;
34+
A[1] = 55;
35+
for (int i=0; i < size; i++) B[i] = A[i];
36+
37+
delete[] A;
38+
A = B;
39+
40+
return;
41+
}
42+
43+
void double2(int* &A, int size) {
44+
int* B = new int[2*size];
45+
cout << "Address of B:" << B << endl;
46+
47+
for (int i=0; i < size; i++) B[i] = A[i];
48+
49+
delete[] A;
50+
A = B;
51+
52+
return;
53+
}
54+
55+
void double3(int* *A, int size) {
56+
int* B = new int[2*size];
57+
cout << "Address of B:" << B << endl;
58+
59+
for (int i=0; i < size; i++) B[i] = (*A)[i];
60+
61+
delete[] *A;
62+
*A = B;
63+
64+
return;
65+
}

lec10/complete/LinkedList.cpp

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#include<iostream>
2+
#include "LinkedList.hpp"
3+
using namespace std;
4+
5+
LinkedList::LinkedList() {
6+
head = 0;
7+
tail = 0;
8+
}
9+
10+
LinkedList::~LinkedList() {
11+
for (Node* tmp = head; tmp != 0; tmp = tmp->next) {
12+
delete tmp;
13+
}
14+
head = 0;
15+
tail = 0;
16+
}
17+
18+
void LinkedList::traverse() {
19+
cout << "[ ";
20+
for (Node* tmp = head; tmp != 0; tmp = tmp->next) {
21+
cout << tmp->data << " ";
22+
}
23+
cout << "]" << endl;
24+
}
25+
26+
Node* LinkedList::search(int item) {
27+
28+
for (Node* tmp = head; tmp != 0; tmp = tmp->next) {
29+
if (tmp->data == item) return tmp;
30+
}
31+
32+
return 0;
33+
}
34+
35+
void LinkedList::insert(Node* left, int item) {
36+
Node* tmp = new Node(item, 0);
37+
38+
if (left == 0) { //insert at the head
39+
tmp->next = head;
40+
head = tmp;
41+
}
42+
else if (left == tail) { //insert at the tail
43+
left->next = tmp;
44+
tail = tmp;
45+
}
46+
else { //insert in the middle
47+
tmp->next = left->next;
48+
left->next = tmp;
49+
}
50+
51+
return;
52+
}
53+
54+
void LinkedList::remove(Node* pos) {
55+
Node* prev = 0;
56+
57+
for (Node* curr = head; curr != 0; prev = curr, curr = curr->next) {
58+
if (curr == pos) {
59+
if (prev == 0) { // delete the head node
60+
if (head->next != 0) head = head->next;
61+
else {
62+
head = 0;
63+
tail = 0;
64+
}
65+
delete pos;
66+
}
67+
else { // delete middle or last node
68+
prev->next = curr->next;
69+
delete pos;
70+
if (prev->next == 0) {
71+
tail = prev;
72+
}
73+
}
74+
}
75+
}
76+
return;
77+
}

0 commit comments

Comments
 (0)