PERTEMUAN 16

 PENJELASAN MATERI 

PENJELASAN MATERI : 

    Circular Single Linked List adalah singly Linked List dimana link simpul terakhir bukan diisi dengan null, tetapi diisi dengan alamat simpul pertama yaitu simpul yang ditunjuk oleh pointer FIRST.

Soal :

Buat program animasi Circular Linked List untuk mengelola data mahasiswa dengan struktur mahasiswa sbb : NAMA, NIM, GENDER, NILAI . Data terurut naik berdasarkan NIM. Program dibuat dalam bentuk menu dengan pilihan : INSERT DATA, HAPUS DATA, CETAK DATA, EXIT.

Program :

#include<iostream>


#include<cstdio>


#include<cstdlib>


using namespace std;




 struct node


 {


 int info;


 struct node *next;


 }*last, *baru, *start=NULL, *tail=NULL,*help,*del;


 int counter = 0;


 class circular_link_list {


 public:


 void create_node(int data);


 void add_begin(int data);


 


 //buat fungsi untuk add after dengan parameter data dan position berikut :


 void add_after(int data, int position);


 //


 


 void delete_element(int data);


 void search_element(int data);


 void display_list();


 void update();


 void sort();


 circular_link_list()


 {


  last = NULL;


 }


 };


 int main()


 {


  int pilihan, element, position;


  circular_link_list cl;


  while (1) 


  {


   cout<<"========Fikriansyah |191011400352 ========"<<endl;


   cout<<"=====Circular singly linked list====="<<endl;


   cout<<"---------------------------"<<endl;


   cout<<"\t1. INSERT DATA"<<endl;


   cout<<"\t2. HAPUS DATA"<<endl;


   cout<<"\t3. CETAK DATA"<<endl;


   cout<<"\t4. EXIT"<<endl;


   cout<<"Enter your pilihan : ";


   cin>>pilihan;


  switch(pilihan)


  {


   case 1 : cout<<"Enter the element: ";


   cin>>element;


   cl.create_node(element);


   cout<<endl;


   break;


   case 2 : cout<<"Enter the element: ";


   cin>>element;


   cl.add_begin(element);


   cout<<endl;


   break;


   


   //pemanggilan fungsi after position


   case 3 : cout<<"Enter the element: ";


   cin>>element;


   cout<<"Insert element after position: ";


   cin>>position;


   cl.add_after(element, position);


   cout<<endl;


   break;


   //


   


   case 4 : if (last == NULL) {


    cout<<"List is empty, nothing to delete"<<endl;


   break;


    }


    cout<<"Enter the element for deletion: ";


    cin>>element;


    cl.delete_element(element);


    cout<<endl;


   break;


   


   case 5 : cl.display_list();


   break;


 


   case 6 : exit(1);


   break;


   default : cout<<"Wrong pilihan"<<endl;


  }


  }


 return 0;


 }




 void circular_link_list::create_node(int data) {


 struct node *temp;


 temp = new(struct node);


 temp->info = data;


 if (last == NULL) {


  last = temp;


  temp->next = last;


 } else {


  temp->next = last->next;


  last->next = temp;


  last = temp; }


 }




 void circular_link_list::add_begin(int data) {


 if (last == NULL) {


  cout<<"First Create the list."<<endl;


  return;


 }


 struct node *temp;


 temp = new(struct node);


 temp->info = data;


 temp->next = last->next;


 last->next = temp;


 }

 void circular_link_list::delete_element(int data)


 {


  struct node *temp, *s;


  s = tail->next;




  if (tail->next == tail && tail->info == data) {


   temp = tail;


   tail = NULL;


   free(temp);


   return;


  }


  if (s->info == data) 


  {


   temp = s;


   tail->next = s->next;


   free(temp);


   return;


  }


  while (s->next != tail) {




   if (s->next->info == data) {


    temp = s->next;


    s->next = temp->next;


    free(temp);


    cout<<"Element "<<data;


    cout<<" deleted from the list"<<endl;

    return;

   }

  s = s->next;

  }

  if (s->next->info == data) {

   temp = s->next;

   s->next = tail->next;

   free(temp);

   tail = s;

   return;

  }

  cout<<"Element "<<data<<" not found in the list"<<endl;

 }

 void circular_link_list::display_list() {

 struct node *s;

 if (last == NULL) {

  cout<<"List is empty"<<endl;

  return;

  }

 s = last->next;

 cout<<"Circular Link List: "<<endl;

 while (s != last) {

  cout<<s->info<<"->";

  s = s->next;

  }

 cout<<s->info<<endl;

 }

void circular_link_list::add_after(int data,int position)

{

    if (last == NULL)

    {

        cout<<"Data kosong, Mohon Buat List Dahulu"<<endl;

        return;

    }

    struct node *temp, *s;

    s = last->next;

    for (int i = 0;i < position-1;i++)

    {

        s = s->next;

        if (s == last->next)

        {

            cout<<"List kurang dari :  ";

            cout<<position<<"list"<<endl;

            return;

        }

    }

    temp = new(struct node);

    temp->next = s->next;

    temp->info = data;

    s->next = temp;

    if (s == last)

    { 

        last=temp;

    }

}

HASIL :



Komentar