Thursday, July 24, 2008

C++ Singly Linked Lists

3 comments
What is a singly linked list? How to implement singly linked lists in C++?

  • Linked lists are building blocks for many other data structures like stacks and queues.
  • Linked lists are a sequence of nodes containing data fields and pointers to the next node (or) next node and previous nodes based on its type.
  • Linked lists permit addition/ removal of node in constant time.
  • Unlike arrays the order of linked list elements need not be contiguous in memory.
  • Unlike arrays there is no upper limit on the amount of memory reserved.
  • A singly linked list is the simplest of linked lists.
  • Each node of a singly linked list has data elements and a single link (pointer) that points to the next node of the list (or) NULL if it is the last node of the list.
  • Addition/ Deletion of a node to the singly linked list involves the creation/ deletion of the node and adjusting the node pointers accordingly.
  • A singly linked list could be represented as below:-

EXAMPLE:- Demonstrate the implementation of a singly linked list

#include <iostream>
using namespace std;

// Node class
class Node {
int data;
Node* next;

public:
Node() {};
void SetData(int aData) { data = aData; };
void SetNext(Node* aNext) { next = aNext; };
int Data() { return data; };
Node* Next() { return next; };
};

// List class
class List {
Node *head;
public:
List() { head = NULL; };
void Print();
void Append(int data);
void Delete(int data);
};

/**
* Print the contents of the list
*/
void List::Print() {

// Temp pointer
Node *tmp = head;

// No nodes
if ( tmp == NULL ) {
cout << "EMPTY" << endl;
return;
}

// One node in the list
if ( tmp->Next() == NULL ) {
cout << tmp->Data();
cout << " --> ";
cout << "NULL" << endl;
}
else {
// Parse and print the list
do {
cout << tmp->Data();
cout << " --> ";
tmp = tmp->Next();
}
while ( tmp != NULL );

cout << "NULL" << endl;
}
}

/**
* Append a node to the linked list
*/
void List::Append(int data) {

// Create a new node
Node* newNode = new Node();
newNode->SetData(data);
newNode->SetNext(NULL);

// Create a temp pointer
Node *tmp = head;

if ( tmp != NULL ) {
// Nodes already present in the list
// Parse to end of list
while ( tmp->Next() != NULL ) {
tmp = tmp->Next();
}

// Point the last node to the new node
tmp->SetNext(newNode);
}
else {
// First node in the list
head = newNode;
}
}

/**
* Delete a node from the list
*/
void List::Delete(int data) {

// Create a temp pointer
Node *tmp = head;

// No nodes
if ( tmp == NULL )
return;

// Last node of the list
if ( tmp->Next() == NULL ) {
delete tmp;
head = NULL;
}
else {
// Parse thru the nodes
Node *prev;
do {
if ( tmp->Data() == data ) break;
prev = tmp;
tmp = tmp->Next();
} while ( tmp != NULL );

// Adjust the pointers
prev->SetNext(tmp->Next());

// Delete the current node
delete tmp;
}
}

int main()
{
// New list
List list;

// Append nodes to the list
list.Append(100);
list.Print();
list.Append(200);
list.Print();
list.Append(300);
list.Print();
list.Append(400);
list.Print();
list.Append(500);
list.Print();

// Delete nodes from the list
list.Delete(400);
list.Print();
list.Delete(300);
list.Print();
list.Delete(200);
list.Print();
list.Delete(500);
list.Print();
list.Delete(100);
list.Print();
}

OUTPUT:-
100 --> NULL
100 --> 200 --> NULL
100 --> 200 --> 300 --> NULL
100 --> 200 --> 300 --> 400 --> NULL
100 --> 200 --> 300 --> 400 --> 500 --> NULL
100 --> 200 --> 300 --> 500 --> NULL
100 --> 200 --> 500 --> NULL
100 --> 500 --> NULL
100 --> NULL
EMPTY

If You Enjoyed This, Take 5 Seconds To Share It

3 comments:

  1. It seems to me that your site has a lot of interesting and informative articles. Surely, I will introduce it to my friends. Keep it up!

    Realities and Realizations
    Innovations and Services

    ReplyDelete
  2. Your post really helped me, but i have a request: it would be great if you'll have an article about double linked list in c++ using classes and how to implement it.

    ReplyDelete
  3. Very useful site. Good tutorials on C++.
    Thanks.

    ReplyDelete