Skip to content

Commit a3cec18

Browse files
committed
initial commit
0 parents  commit a3cec18

10 files changed

+429
-0
lines changed

214854

55.9 KB
Binary file not shown.

214854.cpp

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// implement a linked list with a dummy node
2+
3+
#include <cstdlib>
4+
#include <iostream>
5+
using namespace std;
6+
7+
class Node {
8+
public:
9+
Node(int d = 0, Node *n = NULL) {
10+
data = d;
11+
next = n;
12+
}
13+
int data;
14+
Node *next;
15+
};
16+
17+
class List {
18+
public:
19+
List();
20+
~List();
21+
void insert(int d);
22+
void remove(int d);
23+
void print();
24+
25+
private:
26+
Node *head;
27+
};
28+
29+
List::List() {
30+
head = new Node;
31+
head->next = NULL;
32+
}
33+
34+
List::~List() {
35+
Node *p = head;
36+
while (p != NULL) {
37+
Node *q = p;
38+
p = p->next;
39+
delete q;
40+
}
41+
}
42+
43+
void List::insert(int d) {
44+
Node *p = head;
45+
while (p->next != NULL && p->next->data < d) p = p->next;
46+
p->next = new Node(d, p->next);
47+
}
48+
49+
void List::remove(int d) {
50+
Node *p = head;
51+
while (p->next != NULL && p->next->data < d) p = p->next;
52+
if (p->next != NULL && p->next->data == d) {
53+
Node *q = p->next;
54+
p->next = q->next;
55+
delete q;
56+
}
57+
}
58+
59+
void List::print() {
60+
Node *p = head->next;
61+
while (p != NULL) {
62+
cout << p->data << " ";
63+
p = p->next;
64+
}
65+
cout << endl;
66+
}
67+
68+
int main() {
69+
List list;
70+
int n;
71+
cin >> n;
72+
for (int i = 0; i < n; i++) {
73+
int d;
74+
cin >> d;
75+
list.insert(d);
76+
}
77+
list.print();
78+
int m;
79+
cin >> m;
80+
for (int i = 0; i < m; i++) {
81+
int d;
82+
cin >> d;
83+
list.remove(d);
84+
}
85+
list.print();
86+
return 0;
87+
}
88+
89+
// Path: 214855.cpp
90+
// Delete
91+
// restore
92+
// insert

a.out

55.9 KB
Binary file not shown.

geeks.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
class Node {
5+
public:
6+
int data;
7+
Node* next;
8+
9+
// Default constructor
10+
Node() {
11+
data = 0;
12+
next = NULL;
13+
}
14+
15+
Node(int data) {
16+
this->data = data;
17+
this->next = NULL;
18+
}
19+
};

hello

48.3 KB
Binary file not shown.

hello.c

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <stdio.h>
2+
3+
int sum(int k);
4+
5+
int main() {
6+
int boys = 4;
7+
int* ptr = &boys;
8+
// printf("There are %d boys", boys);
9+
// printf("Hello World!");
10+
// printf("%d", *ptr);
11+
int result = sum(5);
12+
printf("%d", result);
13+
return 0;
14+
}
15+
16+
int sum(int k) {
17+
if (k == 0) {
18+
return 0;
19+
} else {
20+
return k + sum(k - 1);
21+
}
22+
}

last-test

55.9 KB
Binary file not shown.

last-test.cpp

+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
// Cpp implementation of a LinkedList class with an data, key, and next pointer
2+
// Path: last-test.cpp
3+
4+
// Cpp program to implement linked list data structure as [(1, 10), (2, 20), (3,
5+
// 30)]
6+
#include <iostream>
7+
using namespace std;
8+
9+
class Node {
10+
public:
11+
int data;
12+
int key;
13+
Node* next;
14+
15+
// Default constructor
16+
Node() {
17+
data = 0;
18+
next = NULL;
19+
}
20+
21+
Node(int key, int data) {
22+
this->data = data;
23+
this->key = key;
24+
this->next = NULL;
25+
}
26+
27+
// Destructor
28+
~Node() { cout << "Node with key " << key << " is being deleted" << endl; }
29+
30+
// Print the data of the node
31+
void printNode() { cout << "(" << key << ", " << data << ")"; }
32+
};
33+
34+
class LinkedList {
35+
public:
36+
Node* head;
37+
38+
// Default constructor
39+
LinkedList() { head = NULL; }
40+
41+
// Insert a node at the index entered
42+
void insert(int index, int data) {
43+
Node* newNode = new Node(index, data);
44+
if (index == 1) {
45+
newNode->next = head;
46+
head = newNode;
47+
return;
48+
}
49+
Node* temp = head;
50+
cout << index << endl;
51+
for (int i = 1; i < index - 2; i++) {
52+
temp = temp->next;
53+
}
54+
newNode->next = temp->next;
55+
temp->next = newNode;
56+
}
57+
58+
// Delete a node at the index entered
59+
void deleteNode(int index) {
60+
Node* temp = head;
61+
if (index == 1) {
62+
head = temp->next;
63+
delete temp;
64+
return;
65+
}
66+
for (int i = 1; i < index - 2; i++) {
67+
temp = temp->next;
68+
}
69+
Node* temp1 = temp->next;
70+
temp->next = temp1->next;
71+
delete temp1;
72+
}
73+
74+
void reverse() {
75+
Node* prev = NULL;
76+
Node* current = head;
77+
Node* next = NULL;
78+
while (current != NULL) {
79+
next = current->next;
80+
current->next = prev;
81+
prev = current;
82+
current = next;
83+
}
84+
head = prev;
85+
}
86+
87+
// Print the data of the linked list
88+
void printList() {
89+
Node* temp = head;
90+
cout << "[";
91+
while (temp != NULL) {
92+
temp->printNode();
93+
temp = temp->next;
94+
}
95+
cout << "]" << endl;
96+
}
97+
98+
// sort the linked list
99+
100+
// Destructor
101+
~LinkedList() { cout << "List is being deleted" << endl; }
102+
};
103+
104+
int main() {
105+
LinkedList list;
106+
list.insert(1, 10);
107+
list.insert(3, 30);
108+
list.insert(2, 20);
109+
// list.insert(4, 40);
110+
// list.insert(5, 50);
111+
// list.insert(6, 60);
112+
cout << "Original List";
113+
list.printList();
114+
115+
list.reverse();
116+
list.printList();
117+
// list.deleteNode(1);
118+
// list.printList();
119+
// list.deleteNode(3);
120+
// list.printList();
121+
// list.deleteNode(5);
122+
// list.printList();
123+
return 0;
124+
}

test

56.3 KB
Binary file not shown.

0 commit comments

Comments
 (0)