Skip to content

Instantly share code, notes, and snippets.

@alirezaarzehgar
Last active June 22, 2024 22:45
Show Gist options
  • Save alirezaarzehgar/4bd7c59771e931b7dab9fde15ad92cb4 to your computer and use it in GitHub Desktop.
Save alirezaarzehgar/4bd7c59771e931b7dab9fde15ad92cb4 to your computer and use it in GitHub Desktop.
cpp Linked Lists
#include <iostream>
using namespace std;
struct Node {
int date;
Node *link;
};
int main()
{
Node *first, *p, *n;
p = new Node;
p->date = 0;
first = p;
for (size_t i = 1; i <= 5; i++) {
n = new Node;
n->date = i;
p->link = n;
p = n;
}
for (p = first; p != NULL; p = p->link)
cout << p->date << endl;
cout << "------------" << endl;
/* Add node to begin */
n = new Node;
n->date = 100;
n->link = first;
first = n;
/* Add before node */
int key = 3;
Node *q;
for (p = first; p != NULL && p->date != key; p = p->link)
q = p;
n = new Node;
n->date = 200;
n->link = q->link;
q->link = n;
/* Add after node */
key = 4;
for (p = first; p != NULL && p->date != key; p = p->link);
n = new Node;
n->date = 300;
n->link = p->link;
p->link = n;
/* Show list */
for (p = first; p != NULL; p = p->link)
cout << p->date << endl;
return 0;
}
#include <iostream>
using namespace std;
struct Node {
int data;
Node *next;
};
void show_all(Node *p)
{
while (p != NULL) {
cout << p->data << endl;
p = p->next;
}
}
Node *find_node(Node *p, int key)
{
while (p != NULL) {
if (p->data == key)
return p;
p = p->next;
}
return NULL;
}
bool delete_node(Node *p, int key)
{
Node *prev = p;
while (p != NULL) {
if (p->data == key) {
prev->next = p->next;
delete(p);
return true;
}
prev = p;
p = p->next;
}
return false;
}
Node *reverse(Node *head)
{
Node *current = head, *prev = NULL, *next = NULL;
while (current != NULL) {
next = current->next;
current->next = prev;
prev = current;
current = next;
}
return prev;
}
Node *bubble_sort(Node *head)
{
for (Node *i = 0; i != NULL; i = i->next) {
for (Node *j = 0; j != NULL; j = j->next) {
if (i->data < j->data)
swap(i->data, j->data);
}
}
return head;
}
int main()
{
int key;
Node *head, *current, *next, *p;
/* Generate linked-list */
current = new Node;
current->data = 0;
head = current;
for (size_t i = 1; i <= 5; i++) {
next = new Node;
next->data = i;
current->next = next;
current = next;
}
/* Find a node */
cout << "Enter key:";
cin >> key;
if (!(p = find_node(head, key)))
cout << "Node not found!" << endl;
else
cout << "Node found: " << p << endl;
/* Delete a node by key */
cout << "Enter key:";
cin >> key;
if (!delete_node(head, key))
cout << "Node not found!" << endl;
else
cout << "Node was deleted successfully" << endl;
show_all(head);
/* Reverse linked list */
cout << "reversed linked-list: " << endl;
head = reverse(head);
show_all(head);
/* Bubble sort */
cout << "sorted linked-list: " << endl;
head = bubble_sort(head);
show_all(head);
return 0;
}
#include <iostream>
using namespace std;
struct Node {
int data;
Node *next;
};
int main()
{
Node *head, *current, *next, *p;
current = new Node;
current->data = 1;
head = current;
for (size_t i = 1; i <= 5; i++) {
next = new Node;
next->data = i;
current->next = next;
current = next;
}
// print all nodes
p = head;
while (p != NULL) {
cout << p->data << endl;
p = p->next;
}
// find a node
int key;
Node *n = NULL;
cout << "Enter key:";
cin >> key;
p = head;
while (p != NULL) {
if (p->data == key) {
n = p;
break;
}
p = p->next;
}
if (n == NULL) {
cout << "Node with " << key << " data, was not found." << endl;
} else {
cout << "node:" << n << ", data:" << n->data << endl;
}
return 0;
}
#include <iostream>
using namespace std;
struct Node {
int data;
Node *next;
};
int main()
{
Node *head, *p;
head = new Node;
cin >> head->data;
for (size_t i = 0; i < 4; i++) {
// create next
Node *n = new Node;
cin >> n->data;
// Add to current add go more
n->next = head;
head = n;
}
p = head;
while (p != NULL) {
cout << p->data;
p = p->next;
}
return 0;
}
#include <iostream>
using namespace std;
struct Node {
int data;
Node *next;
};
int main()
{
Node *p, *head, *a, *b, *c, *d;
a = new Node;
a->data = 1;
head = a;
b = new Node;
b->data = 2;
a->next = b;
c = new Node;
c->data = 3;
b->next = c;
d = new Node;
d->data = 4;
c->next = d;
d->next = NULL;
p = head;
while (p != NULL) {
cout << p->data << endl;
p = p->next;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment