About Me

Hayy apa kabar, kembali lagi dengan saya konan kato ingin berbagi di blok saya tentang Linked Lists di bahasa programan C++, langsung saja lihat Source code C++ di bawah ini

Singly Linked Lists


/* definition of the list node class */
class Node
{
    friend class LinkedList;
private:
    int _value; /* data, can be any data type, but use integer for easiness */
    Node *_pNext; /* pointer to the next node */
   
public:
    /* Constructors with No Arguments */
    Node(void)
    : _pNext(NULL)
    { }
   
    /* Constructors with a given value */
    Node(int val)
    : _value(val), _pNext(NULL)
    { }
   
    /* Constructors with a given value and a link of the next node */
    Node(int val, Node* next)
    : _value(val), _pNext(next)
    {}
   
    /* Getters */
    int getValue(void)
    { return _value; }
   
    Node* getNext(void)
    { return _pNext; }
};

/* definition of the linked list class */
class LinkedList
{
private:
    /* pointer of head node */
    Node *_pHead;
    /* pointer of tail node */
    Node *_pTail;
   
public:
    /* Constructors with No Arguments */
    LinkedList(void);
    /* Constructors with a given value of a list node */
    LinkedList(int val);
    /* Destructor */
    ~LinkedList(void);
   
    /* Traversing the list and printing the value of each node */
    void traverse_and_print();
};

LinkedList::LinkedList()
{
    /* Initialize the head and tail node */
    _pHead = _pTail = NULL;
}

LinkedList::LinkedList(int val)
{
    /* Create a new node, acting as both the head and tail node */
    _pHead = new Node(val);
    _pTail = _pHead;
}

LinkedList::~LinkedList()
{
    /*
     * Leave it empty temporarily.
     * It will be described in detail in the example "How to delete a linkedlist".
     */
}

void LinkedList::traverse_and_print()
{
    Node *p = _pHead;
   
    /* The list is empty? */
    if (_pHead == NULL) {
        cout << "The list is empty" << endl;
        return;
    }
   
    cout << "LinkedList: ";
    /* A basic way of traversing a linked list */
    while (p != NULL) { /* while there are some more nodes left */
        /* output the value */
        cout << p->_value;
        /* The pointer moves along to the next one */
        p = p->_pNext;
    }
    cout << endl;
}

int main(int argc, const char * argv[])
{
    /* Create an empty list */
    LinkedList list1;
    cout << "Created an empty list named list1." << endl;
    /* output the result */
    cout << "list1:" << endl;
    list1.traverse_and_print();
   
    /* Create a list with only one node */
    LinkedList list2(10);
    cout << "Created a list named list2 with only one node." << endl;
    /* output the result */
    cout << "list2:" << endl;
    list2.traverse_and_print();
   
    return 0;
}

Circularly Linked Lists
 
 
#include <iostream>
using namespace std;

template <class T>
struct Node
{
  T data;
  Node * next;
  Node(T data) : data(data), next(NULL) {}
};

template <class T>
class CircularLinkedList
{
public:
  CircularLinkedList() : head(NULL) {}
  ~CircularLinkedList();
  void addNode(T data);
  void deleteNode(T data);
  template <class U>
  friend std::ostream & operator<<(std::ostream & os, const CircularLinkedList<U> & cll);
private:
  Node<T> * head;
};

template <class T>
CircularLinkedList<T>::~CircularLinkedList()
{
  if (head)
    {
      Node<T> * tmp = head;
      while (tmp->next != head)
        {
          Node<T> * t = tmp;
          tmp = tmp->next;
          delete(t);
        }
      delete tmp;
      head = NULL;
    }
}

template <class T>
void CircularLinkedList<T>::addNode(T data)
{
  Node<T> * t = new Node<T>(data);

  if (head == NULL)
    {
      t->next = t;
      head = t;
      return;
    }

  Node<T> * tmp = head;
  while (tmp->next !=  head)
    {
      tmp = tmp->next;
    }

  tmp->next = t;
  t->next = head;
}

template <class T>
void CircularLinkedList<T>::deleteNode(T data)
{
  Node<T> * tmp = head;
  Node<T> * prev = NULL;
  while (tmp->next !=  head)
    {
      if (tmp->data == data) break;
      prev = tmp;
      tmp = tmp->next;
    }

  if (tmp == head)
    {
      while (tmp->next != head)
        {
          tmp = tmp->next;
        }
      tmp->next = head->next;
      delete head;
      head = tmp->next;
    }
  else
    {
      prev->next = tmp->next;
      delete tmp;
    }
}

template <class U>
std::ostream & operator<<(std::ostream & os, const CircularLinkedList<U> & cll)
{
  Node<U> * head = cll.head;
  if (head)
    {
      Node<U> * tmp = head;
      while (tmp->next != head)
        {
          os << tmp->data << " ";
          tmp = tmp->next;
        }
      os << tmp->data;
    }
  return os;
}

int main()
{
  CircularLinkedList<int> cll;

  cll.addNode(1);
  cll.addNode(2);
  cll.addNode(3);
  cll.addNode(4);
  cll.addNode(5);

  cout << cll << endl;

  cll.deleteNode(3);
  cll.deleteNode(1);
  cll.deleteNode(5);

  cout << cll << endl;

  return 0;
}

Doubly Linked Lists 

#include<iostream>
using namespace std;

/* Linked list structure */
struct list {
 struct list *prev;
 int data;
 struct list *next;
} *node = NULL, *first = NULL, *last = NULL, *node1 = NULL, *node2 = NULL;

class linkedlist {
 public: 

  /* Function for create/insert node at the beginning of Linked list */
  void insert_beginning() {
   list *addBeg = new list;
   cout << "Enter value for the node:" << endl;
   cin >> addBeg->data;
   if(first == NULL) {
    addBeg->prev = NULL;
    addBeg->next = NULL;
    first = addBeg;
    last = addBeg;
    cout << "Linked list Created!" << endl;
   }
   else {
    addBeg->prev = NULL;
    first->prev = addBeg;
    addBeg->next = first;
    first = addBeg;
    cout << "Data Inserted at the beginning of the Linked list!" << endl;
   }
  }

  /* Function for create/insert node at the end of Linked list */
  void insert_end() {
   list *addEnd = new list;
   cout << "Enter value for the node:" << endl;
   cin >> addEnd->data;
   if(first == NULL) {
    addEnd->prev = NULL;
    addEnd->next = NULL;
    first = addEnd;
    last = addEnd;
    cout << "Linked list Created!" << endl;
   }
   else {
    addEnd->next = NULL;
    last->next = addEnd;
    addEnd->prev = last;
    last = addEnd;
    cout << "Data Inserted at the end of the Linked list!" << endl;
   }
  }

  /* Function for Display Linked list */
  void display() {
   node = first;
   cout << "List of data in Linked list in Ascending order!" << endl;
   while(node != NULL) {
    cout << node->data << endl;
    node = node->next;
   }
   node = last;
   cout << "List of data in Linked list in Descending order!" << endl;
   while(node != NULL) {
    cout << node->data << endl;
    node = node->prev;
   }
  }
 
  /* Function for delete node from Linked list */
  void del() {
   int count = 0, number, i;
   node = node1 = node2 = first;
   for(node = first; node != NULL; node = node->next)
    cout << "Enter value for the node:" << endl;
   count++;
   display();
   cout << count << " nodes available here!" << endl;
   cout << "Enter the node number which you want to delete:" << endl;
   cin >> number;
   if(number != 1) {
    if(number < count && number > 0) {
     for(i = 2; i <= number; i++)
      node = node->next;
     for(i = 2; i <= number-1; i++)
      node1 = node1->next;
     for(i = 2; i <= number+1; i++)
      node2 = node2->next;
     node2->prev = node1;
     node1->next = node2;
     node->prev = NULL;
     node->next = NULL;
     node = NULL;
    }
    else if(number == count) {
     node = last;
     last = node->prev;
     last->next = NULL;
     node = NULL;
    }
    else
     cout << "Invalid node number!" << endl;
   }
   else {
    node = first;
    first = node->next;
    first->prev = NULL;
    node = NULL;
   }
   cout << "Node has been deleted successfully!" << endl;
  }
 
};

int main() {
 int op = 0;
 linkedlist llist = linkedlist();
 while(op != 4) {
  cout << "1. Insert at the beginning\n2. Insert at the end\n3. Delete\n4. Display\n5. Exit" << endl;
  cout << "Enter your choice:" << endl;
  cin >> op;
  switch(op) {
   case 1:
    llist.insert_beginning();
    break;
   case 2:
    llist.insert_end();
    break;
   case 3:
    llist.del();
    break;
   case 4:
    llist.display();
    break;
   case 5:
    cout << "Bye Bye!" << endl;
    return 0;
    break;
   default:
    cout << "Invalid choice!" << endl;
  }
 }
 return 0;
}
 

0 komentar:

Posting Komentar