PROGRAM LINKED LIST MENGHAPUS BELAKANG DAN TENGAH
01.49
By
Unknown
PROGRAMKE-1
0
komentar
#include <cstdlib>
#include <iostream>
using namespace std;
struct node{
int data;
node *next;
};
class Linkedlist{
public:
Linkedlist();
void tambahDepan(int x);
void hapusBelakang();
void hapusTengah(int x);
void cetak();
private:
node *link;
int jml;
};
Linkedlist::Linkedlist(){
cout<<"Program Linked List"<<endl;
jml = 0;
}
void Linkedlist::tambahDepan(int x){
node *temp;
temp = new node;
temp->data = x;
temp->next = NULL;
if(jml==0){
link = temp;
link->next = NULL;
}else{
temp->next = link;
link = temp;
}
jml++;
}
void Linkedlist::hapusBelakang(){
node *temp;
node *prev;
prev=link;
temp=link->next;
if(jml==0){
cout<<"data masih kosong"<<endl;
}else{
while(temp->next!=NULL){
temp = temp->next;
prev=prev->next;
}
prev->next=NULL;
delete temp;
}
}
void Linkedlist::hapusTengah(int y){
node *after;
node *prev;
node *del;
prev=link;
int i=1;
if(jml==0){
cout<<"data masih kosong"<<endl;
}else{
while(i!=y){
prev=prev->next;
i++;
}
del=prev->next;
after=del->next;
prev->next=after;
delete del;
}
}
void Linkedlist::cetak(){
node *temp;
temp = link;
cout<<"Banyak data di Linked List : "<<jml<<endl;
while(temp!=NULL){
cout<< temp->data <<" -> ";
temp = temp->next;
}
cout<<endl;
}
int main(int argc, char *argv[])
{ Linkedlist newLink;
newLink.tambahDepan(1);
newLink.tambahDepan(2);
newLink.tambahDepan(3);
newLink.tambahDepan(4);
newLink.tambahDepan(5);
newLink.tambahDepan(6);
cout<<endl;
newLink.cetak();
cout<<endl;
cout<<"hapus data dibelakang"<<endl;
newLink.hapusBelakang();
newLink.cetak();
cout<<endl;
cout<<"hapus data ditengah"<<endl;
newLink.hapusTengah(2);
newLink.cetak();
cout<<endl;
system("PAUSE");
return 0;
}
#include <iostream>
using namespace std;
struct node{
int data;
node *next;
};
class Linkedlist{
public:
Linkedlist();
void tambahDepan(int x);
void hapusBelakang();
void hapusTengah(int x);
void cetak();
private:
node *link;
int jml;
};
Linkedlist::Linkedlist(){
cout<<"Program Linked List"<<endl;
jml = 0;
}
void Linkedlist::tambahDepan(int x){
node *temp;
temp = new node;
temp->data = x;
temp->next = NULL;
if(jml==0){
link = temp;
link->next = NULL;
}else{
temp->next = link;
link = temp;
}
jml++;
}
void Linkedlist::hapusBelakang(){
node *temp;
node *prev;
prev=link;
temp=link->next;
if(jml==0){
cout<<"data masih kosong"<<endl;
}else{
while(temp->next!=NULL){
temp = temp->next;
prev=prev->next;
}
prev->next=NULL;
delete temp;
}
}
void Linkedlist::hapusTengah(int y){
node *after;
node *prev;
node *del;
prev=link;
int i=1;
if(jml==0){
cout<<"data masih kosong"<<endl;
}else{
while(i!=y){
prev=prev->next;
i++;
}
del=prev->next;
after=del->next;
prev->next=after;
delete del;
}
}
void Linkedlist::cetak(){
node *temp;
temp = link;
cout<<"Banyak data di Linked List : "<<jml<<endl;
while(temp!=NULL){
cout<< temp->data <<" -> ";
temp = temp->next;
}
cout<<endl;
}
int main(int argc, char *argv[])
{ Linkedlist newLink;
newLink.tambahDepan(1);
newLink.tambahDepan(2);
newLink.tambahDepan(3);
newLink.tambahDepan(4);
newLink.tambahDepan(5);
newLink.tambahDepan(6);
cout<<endl;
newLink.cetak();
cout<<endl;
cout<<"hapus data dibelakang"<<endl;
newLink.hapusBelakang();
newLink.cetak();
cout<<endl;
cout<<"hapus data ditengah"<<endl;
newLink.hapusTengah(2);
newLink.cetak();
cout<<endl;
system("PAUSE");
return 0;
}
0 komentar: