Singly linked list in C/C++ using ChatGPT
AI is one of the most powerful and popular tool nowadays and engineers sooner or later have to know how to use the tool to save time instead of implementing everything from a scratch. In this series, I will try to implement data structures using ChatGPT and let’s verify the generated codes.
This article will go through a single linked list in C and C++. First, I search “singly linked list node in C” and got the result below.
#include <stdio.h>
#include <stdlib.h>
// Definition of a node in a singly linked list
struct Node {
int data; // Data stored in the node
struct Node* next; // Pointer to the next node
};
// Function to create a new node
struct Node* createNode(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (!newNode) {
printf("Memory allocation failed\n");
return NULL;
}
newNode->data = value;
newNode->next = NULL;
return newNode;
}
// Function to print the linked list
void printList(struct Node* head) {
struct Node* temp = head;
while (temp) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
// Example usage
int main() {
struct Node* head = createNode(10);
head->next = createNode(20);
head->next->next = createNode(30);
printList(head);
// Free allocated memory
struct Node* temp;
while (head) {
temp = head;
head = head->next;
free(temp);
}
return 0;
}
Second, I search “singly linked list node in C++” and got the result below.
#include <iostream>
using namespace std;
// Definition of a singly linked list node
struct Node {
int data; // Data stored in the node
Node* next; // Pointer to the next node
// Constructor to initialize the node
Node(int value) {
data = value;
next = nullptr;
}
};
// Function to print the linked list
void printList(Node* head) {
Node* temp = head;
while (temp) {
cout << temp->data << " -> ";
temp = temp->next;
}
cout << "NULL" << endl;
}
// Example usage
int main() {
// Creating nodes dynamically
Node* head = new Node(10);
head->next = new Node(20);
head->next->next = new Node(30);
// Print the linked list
printList(head);
// Free allocated memory
Node* temp;
while (head) {
temp = head;
head = head->next;
delete temp;
}
return 0;
}
Both results are doing exactly the same thing with a slightly different syntax by different language. I can see main difference from included header files and way to allocate the memory dynamically.
In C, standard library header file format is xxxxx.h but C++ is not.
In C, use “malloc” to allocate the memory dynamically but C++ uses “new” keyword.
In C, use “free” to de-allocate the assigned memory but C++ uses “delete” keyword.
I expect C++ version, node as a class instead of struct but output was not exactly what I expected. Current C++ works fine, however, it does not show any object oriented concept. I am going to try to search with including “class” keyword this time as “singly linked list node class in C++“. The result is below.
#include <iostream>
using namespace std;
// Definition of a singly linked list node
class Node {
public:
int data; // Data stored in the node
Node* next; // Pointer to the next node
// Constructor to initialize the node
Node(int value) {
data = value;
next = nullptr;
}
};
// Singly Linked List class
class SinglyLinkedList {
private:
Node* head;
public:
// Constructor
SinglyLinkedList() {
head = nullptr;
}
// Insert at the end
void insert(int value) {
Node* newNode = new Node(value);
if (!head) {
head = newNode;
return;
}
Node* temp = head;
while (temp->next) {
temp = temp->next;
}
temp->next = newNode;
}
// Print the linked list
void printList() {
Node* temp = head;
while (temp) {
cout << temp->data << " -> ";
temp = temp->next;
}
cout << "NULL" << endl;
}
// Destructor to free memory
~SinglyLinkedList() {
Node* temp;
while (head) {
temp = head;
head = head->next;
delete temp;
}
}
};
// Example usage
int main() {
SinglyLinkedList list;
list.insert(10);
list.insert(20);
list.insert(30);
list.printList(); // Output: 10 -> 20 -> 30 -> NULL
return 0;
}
Finally, this code shows object oriented concept. All three versions print exactly the same result. However, when you look for your own intention, you need to know how to search more specific. After including “class”, I satisfy the C++ version meeting my expectation.