Pemrograman

How to Use a Single Linked List Using C++

Linked List is a form of data structure, containing a collection of data (nodes) that are arranged sequentially, interconnected, dynamic and limited. Meanwhile Single Linked List is a linked list which uses just a pointer variable to store a lot of data using the linked list method, a list of contents that are interconnected.

OK, Santekno will provide an implementation of Single Linked List using C++ language. The most important thing in making a Single Linked List is that we will create a link which can then be connected to each other so that in order to be connected to each other we need several functions that can operate it, such as, insert front, insert back, insert in the middle, delete, size, and others.

The Linked List that Santekno will create applies the OOP (Object Oriented Programming) system so that it is easier to understand. In OOP we have to create a class which we will call SSL, or an abbreviation for Single Linked List.

Functions to be created

  • Created make() Function Node
  • Add Node behind push_back()
  • Add Node in front of push_front()
  • Add Nodes after n push_after()
  • Search Node find()
  • Searches for Nodes before n find_before()
  • Delete Node n del()
  • Prints the linked list print()
  • Play Programs

SLL Class Program Code

#include <utility>
#include <iostream>
#include <forward_list>
#include <string>

using namespace std;

typedef par<string, float> P;
typedef forward_list<P> SLLP;

class SLL{
    SLLP dt;
    void push_front(string nim,float ipk);
    void push_back(string nim, float ipk);
    void push_after(string nim,float ipk, string after);
    void del(string nim);
    SLLP::iterator find(string nim);
    void print();
};

void SLL::print(){
    SLLP::iterator it;
    for(it=dt.begin();it!=dt.end();++it){
        cout << "(" << it->first << "," << it->second << ")->";
    }
    cout << "NULL"<< endl;
}

void SLL::push_front(string nim, float ipk){
    P t=make_pair(nim,ipk);
    dt.push_front(t);
}
void SLL::push_back(string nim,float ipk){
    P t=make_pair(nim,ipk);

    if(dt.empty())
        dt.push_front(t);
    else{
        SLLP::iterator before=dt.begin();
        SLLP::iterator it=dt.begin();
        for(;it!=dt.end();before=it,++it);
        dt.insert_after(before,t);
    }
}

int main(){
    SLL list;
    list.make();
    list.push_back(100); list.push_back(50);
    list.push_front(75); list.print();
    list.push_after(35,100); list.print();
    list.del(50); list.print();
    return 0;
}

Output

comments powered by Disqus