Single Linked List Head & Tail – C++ ( Struktur Data )

//index.cpp

#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <singleLHT.cpp>

using namespace std;

int main(){
    int databaru,pil;
    char j,w;
    do{
		cout<< "berapa nilai yang ingin anda masukkan\n";
		cin>>databaru;
		cout<<"\ndepan(1) atau belakang(2)?\n";
		cin>>pil;
		if(pil==1){
			insertDepan(databaru);
		}else{
			insertBelakang(databaru);
		}
		tampil();
		cout<<"apakah anda ingin memasukkan data (y/t)? ";
		cin>>j;
    }
    while(j=='y');

    do{
		cout<<"hapus depan(1) dan hapus belakang(2)? ";
		cin>>pil;
		if(pil==1){
			hapusDepan();
		}else{
			hapusBelakang();;
		}
		tampil();
		cout<<"apakah anda ingin hapus data (y/t)? ";
		cin>>w;
    }
    while(w=='y');
    getch();
    return 0;
}

//singleLH.cpp

#include <iostream.h>
#include <conio.h>
#include <stdio.h>

typedef struct TNode{
	int data;
	TNode *next;
};

TNode *head;
TNode *bantu;
TNode *baru;

int isEmpty(){
	if(head == NULL)
    {return 1;}
	else return 0;
}

void insertDepan(int databaru){
	baru = new TNode;
	baru->data = databaru;
	baru->next = NULL;
	if(isEmpty()==1){
		head=baru;
		head->next = NULL;
	}else {
		baru->next = head;
		head = baru;
	}
	cout << "\ndatabaru masuk :::"<<head->data;
}

void insertBelakang (int databaru){
	TNode *baru,*bantu;
	baru = new TNode;
	baru->data = databaru;
	baru->next = NULL;
	if(isEmpty()==1){
		head=baru;
		head->next = NULL;
	}else {
		bantu=head;
		while(bantu->next!=NULL){
			bantu=bantu->next;
		}
		bantu->next = baru;
	}
	cout<<"Data masuk\n";
}

void hapusDepan (){
	TNode *hapus;
	int d;
	if (isEmpty()==0){
		if(head->next != NULL){
			hapus = head;
			d = hapus->data;
			head = head->next;
			delete hapus;
		}else{
			d = head->data;
			head = NULL;
		}
		cout<<d<<" terhapus\n";
	} else cout<<"Masih kosong\n";
}
void hapusBelakang(){
	TNode *hapus,*bantu;
	int d;
	if (isEmpty()==0){
		if(head->next != NULL){
			bantu = head;
			while(bantu->next->next!=NULL){
				bantu = bantu->next;
			}
			hapus = bantu->next;
			d = hapus->data;
      		bantu->next = NULL;
			delete hapus;
		}else{
			d = head->data;
			head = NULL;
		}
		cout<<d<<" terhapus\n";
	} else cout<<"Masih kosong\n";
}

void tampil(){
	bantu = head;
	if(isEmpty()==0){
        cout<< "\ndata yang ada dalam list\n";
		while(bantu!=NULL){
			cout<<bantu->data<<"\n";
			bantu=bantu->next;
		}
		cout<<endl;
	} else cout<<"Masih kosong\n";
}
//singleLHT.cpp

#include <iostream.h>
#include <conio.h>
#include <stdio.h>

typedef struct TNode{
	int data;
	TNode *next;
};

TNode *head, *tail;
TNode *bantu;
TNode *baru;

int isEmpty(){
	if(tail == NULL){return 1;}
	else return 0;
}

void insertDepan(int databaru){
	TNode *baru;
	baru = new TNode;
	baru->data = databaru;
	baru->next = NULL;
	if(isEmpty()==1){
		head=tail=baru;
		tail->next=NULL;
	}else {
		baru->next = head;
		head = baru;
	}
	cout<<"Data masuk\n";
}

void insertBelakang(int databaru){
	TNode *baru,*bantu;
	baru = new TNode;
	baru->data = databaru;
	baru->next = NULL;
	if(isEmpty()==1){
		head=baru;
		tail=baru;
		tail->next = NULL;
	}else {
		tail->next = baru;
		tail=baru;
	}
	cout<<"Data masuk\n";
}

void hapusDepan(){
	TNode *hapus;
	int d;
	if (isEmpty()==0){
		if(head!=tail){
			hapus = head;
			d = hapus->data;
			head = head->next;
			delete hapus;
		} else {
			d = tail->data;
			head=tail=NULL;
		}
		cout<<d<<" terhapus\n";
	} else cout<<"Masih kosong\n";
}

void hapusBelakang(){
	TNode *bantu,*hapus;
	int d;
	if (isEmpty()==0){
		bantu = head;
		if(head!=tail){
			while(bantu->next!=tail){
				bantu = bantu->next;
			}
			hapus = tail;
			tail=bantu;
			d = hapus->data;
			delete hapus;
			tail->next = NULL;
		}else{
			d = tail->data;
			head=tail=NULL;
		}
		cout<<d<<" terhapus\n";
	} else cout<<"Masih kosong\n";
}

void tampil(){
	bantu = head;
	if(isEmpty()==0){
        cout<< "\ndata yang ada dalam list\n";
		while(bantu!=NULL){
			cout<<bantu->data<<"\n";
			bantu=bantu->next;
		}
		cout<<endl;
	} else cout<<"Masih kosong\n";
}

9 thoughts on “Single Linked List Head & Tail – C++ ( Struktur Data )

    • kenapa Anda mengajukan pertanyaan retorik?
      kamu baru bisa nanya seperti itu kalo ini hasil copas…
      coba di pikir dulu jangan asal copas trus waktu running ada yang gag jalan di editor&compiler mu, lalu kamu menilai bahwa ini blm jadi…
      thanks :mrgreen:

Leave a Reply