Program Double Linked List

Source Code Program :

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

//using namespace std;
struct node{
int data;
struct node *prev,*next,*info;
};
typedef struct node node;
node *head,*last,*temp,*t,*p,search,item;
int d;
void addhead();
void addmiddle();
void addtail();
void delhead();
void delmiddle();
void deltail();
int Search();
void disp();
int main(){
int ch;
while(1){
printf(“\n1. add to head “);
printf(“\n2. add to Middle “);
printf(“\n3. add to tail “);
printf(“\n4. Delete from head “);
printf(“\n5. Delete from Middle “);
printf(“\n6. Delete from tail “);
printf(“\n7.earch”);
printf(“\n8. Exit”);
printf(“\nEnter your Choice : “);
scanf(“%d”,&ch);
switch(ch){
case 1:
addhead();
disp();
break;
case 2:
addmiddle();
disp();
break;
case 3:
addtail();
disp();
break;
case 4:
delhead();
disp();
break;
case 5:
delmiddle();
disp();
break;
case 6:
deltail();
disp();
break;
case 7:
Search();
disp();
break;
case 8:
exit(0);
default:
printf(“\nInvalid Choice”);
}
getch();
}
}

void addhead()
{
temp=(node*)malloc(sizeof(node));
printf(“\nEneter the Data : “);
scanf(“%d”,&temp->data);
temp->next=temp->prev=NULL;
if(head==NULL)
head=temp;
else
{
temp->next=head;
head->prev=temp;
head=temp;
}
}
void addmiddle()
{
int d;
temp=(node*)malloc(sizeof(node));
printf(“\nEneter the Data : “);
scanf(“%d”,&temp->data);
temp->next=temp->prev=NULL;
if(head==NULL)
head=temp;
else
{
t=head;
printf(“\nEnter the node after which insertion to be made : “);
scanf(“%d”,&d);
while(t!=NULL)
{
if(t->data==d)
{
temp->next=t->next;
temp->prev=t;
t->next=temp;
return;}
else
t=t->next;
}
printf(“\nadd node not found”);
}
}
void addtail()
{
node *t;
temp=(node*)malloc(sizeof(node));
printf(“\nEneter the Data : “);
scanf(“%d”,&temp->data);
temp->next=temp->prev=NULL;
if(head==NULL)
head=temp;
else
{
t=head;
while(t->next!=NULL)
t=t->next;
t->next=temp;
temp->prev=t;
}
}
void delhaed()
{
if(head==NULL)
printf(“\nList is Empty”);
else
{
t=head;
printf(“\nDeleted node is %d\n”,t->data);
head=head->next;
head->prev=NULL;
free(t);
}
}
void delmiddle()
{
int d;
node *s,*n;
if(head==NULL)
printf(“\nList is Empty”);
else
{
printf(“\nEnter the node data to be deleted : “);
scanf(“%d”,&d);
if(head->data==d)
{
t=head;
head=head->next;
head->prev=NULL;
printf(“\nDeleted node is %d\n”,t->data);
free(t);
}
else
{
t=head;
while(t->next!=NULL)
{
if(t->data==d){
s=t;
printf(“\nDeleted node is %d\n”,s->data);
p=t->prev;
n=t->next;
p->next=t->next;
n->prev=p;
free(s);
}
else
{
p=p->next;
t=t->next;
}
}
}
} }
void deltail()
{
if(head==NULL)
printf(“\nList is Empty”);
else if(head->next==NULL)
{
t=head;
printf(“\nDeleted node is %d\n”,t->data);
head=NULL;
}
else
{
t=head;
while(t->next!=NULL)
{
t=t->next;
}
p=t->prev;
p->next=NULL;
printf(“\nDeleted node is %d\n”,t->data);
free(t);
}
}
//search

int Search()
{
while(head!=NULL)
{
if(head->info=item){ // if the values match,
//return head; // return the matching node.
head=head->next; }// otherwise, move on
}
system(“pause”);
return 0;
}

Program Circular Linked List

Source Code Program

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<alloc.h>
#define null 0
struct node{
int info;
struct node *link;
public:
}
*start,*last;
int main(){
int ch,n,m,position,i;
void create(int);
void addat(int);
void addbt(int,int);
void del(int);
void disp();
last=null;
//clrscr();
while(1){
cout<<“\nMAIN MENU”;
cout<<“\n1.Buat\n2.Tambah depan\n3.Tambah belakang\n4.Hapus\n5.Tampilkan\n6.Keluar”;
cout<<“\n\nMasukkan pilihan anda : “; cin>>ch;
switch(ch)
{
case 1:
cout<<“\n\nMasukkan jumlah elemen : “; cin>>n;
for(i=0;i<n;i++)
{
cout<<“\n\nMasukkan Elemen : “; cin>>m;
create(m);
}break;
case 2:
cout<<“\n\nMasukkan Elemen : “; cin>>m;
addat(m);
break;
case 3:
cout<<“\n\nMasukkan Elemen : “; cin>>m;
cout<<“\n\nMasukkan Posisinya : “; cin>>position;
addbt(m,position);
break;
case 4:
if(last==null)
{
cout<<“\n\nList kosong”;
continue;
}
cout<<“\n\nMasukkan posisi yang akan dihapus : “; cin>>m;
del(m);
break;
case 5:
disp();
break;
case 6:
exit(0);
break;
default:
cout<<endl<<endl<<“Pilihan yang salah”; } } } void create(int data) { struct node *q,*tmp; tmp=(struct node *)malloc(sizeof(struct node)); tmp->info=data;
tmp->link=null;
if(last==null)
{
last=tmp;
tmp->link=last;
}
else
{
tmp->link=last->link;
last->link=tmp;
last=tmp;
}
return;
}

void addat(int data){
struct node *q,*tmp;
tmp=(struct node *)malloc(sizeof(struct node));
tmp->info=data;
tmp->link=last->link;
last->link=tmp;
}
void addbt(int data,int pos){
struct node *tmp,*q;
int i;
q=last->link;;
for(i=0;i<pos-1;i++) { q=q->link;
if(q==last->link)
{
cout<<“\n\nDimana r kurang dari “<<pos<<“elemen “; return; } } tmp=(struct node *)malloc(sizeof(struct node)); tmp->link=q->link;
tmp->info=data;
q->link=tmp;
if(q==last)
last=tmp;
}
void del(int data)
{
struct node *tmp,*q;
if(last->link==last&&last->info==data)
{
tmp=last;
last=null;
free(tmp);
cout<<“\n\nElemen “<<data<” dihapus”; return; } q=last->link;
if(q->info==data)
{
tmp=q;
last->link=q->link;
free(tmp);
cout<<“\n\nElemen “<<data<” dihapus “; return; } while(q->link!=last)
{
if(q->link->info==data)
{
tmp=q->link;
q->link=tmp->link;
free(tmp);
cout<<“\n\nElemen “<<data<<” dihapus “; return; } q=q->link;
}
if(q->link==last)
{
if(q->link->info==data)
{
tmp=last;
last=q;
last->link=tmp->link;
free(tmp);
cout<<“\n\nElemen “<<data<<” dihapus “;
return;
}
}

cout<<“\n\nElemen tidak ditemukan “;

}

void disp()
{
struct node *q;
if(last==null)
{
cout<<“\n\nList kosong”; return; }q=last->link;
cout<<endl;
while(q!=last)
{
cout<info<<” “; q=q->link;
}
cout<info;
}

Program Link List dengan C++

Source Code Program :

#include <iostream>
#include <list>
#include <iterator>
#include <algorithm>
#include <conio.h>
using namespace std;

int main()
{
list<int> intList1, intList2, intList3, intList4; //Line 1

ostream_iterator<int> screen(cout,” “); //Line 2

intList1.push_back(23); //Line 3
intList1.push_back(58); //Line 4
intList1.push_back(58); //Line 5
intList1.push_back(58); //Line 6
intList1.push_back(36); //Line 7
intList1.push_back(15); //Line 8
intList1.push_back(93); //Line 9
intList1.push_back(98); //Line 10
intList1.push_back(58); //Line 11

cout<<“Line 12: intList1: “; //Line 12
copy(intList1.begin(),intList1.end(),screen);//Line 13
cout<<endl; //Line 14

intList2 = intList1; //Line 15

cout<<“Line 16: intList2: “; //Line 16
copy(intList2.begin(),intList2.end(),screen);//Line 17
cout<<endl; //Line 18

intList1.unique(); //Line 19

cout<<“Line 20: After removing the consecutive ”
<<“duplicates,”<<endl
<<” intList1: “; //Line 20
copy(intList1.begin(),intList1.end(),screen);//Line 21
cout<<endl; //Line 22

intList2.sort(); //Line 23

cout<<“Line 24: After sorting, intList2: “; //Line 24
copy(intList2.begin(),intList2.end(),screen);//Line 25
cout<<endl; //Line 26

intList3.push_back(13); //Line 27
intList3.push_back(23); //Line 28
intList3.push_back(25); //Line 29
intList3.push_back(136); //Line 30
intList3.push_back(198); //Line 31

cout<<“Line 32: intList3: “; //Line 32
copy(intList3.begin(),intList3.end(),screen);//Line 33
cout<<endl; //Line 34

intList4.push_back(-2); //Line 35
intList4.push_back(-7); //Line 36
intList4.push_back(-8); //Line 37

cout<<“Line 38: intList4: “; //Line 38
copy(intList4.begin(),intList4.end(),screen);//Line 39
cout<<endl; //Line 40

intList3.splice(intList3.begin(),intList4); //Line 41

cout<<“Line 42: After moving the elements of ”
<<“intList4 into intList3,”<<endl
<<” intList3: “; //Line 42
copy(intList3.begin(),intList3.end(),screen);//Line 43
cout<<endl; //Line 44

intList3.sort(); //Line 45

cout<<“Line 46: After sorting, intList3: “; //Line 46
copy(intList3.begin(),intList3.end(),screen);//Line 47
cout<<endl; //Line 48

intList2.merge(intList3); //Line 49

cout<<“Line 50: After merging intList2 and intList3, ”
<<“intList2: “<<endl<<” “; //Line 50
copy(intList2.begin(),intList2.end(),screen);//Line 51
cout<<endl; //Line 52

intList2.unique(); //Line 53

cout<<“Line 54: After removing the consecutive ”
<<“duplicates, intList2: “<<endl
<<” “; //Line 54
copy(intList2.begin(),intList2.end(),screen);//Line 55
cout<<endl; //Line 56
getch();
return 0;
}

Output Program

Program Penjumlahan 2 Polinomial dengan C++

Source Code Program :

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

class poli{
friend ostream& operator << (ostream& , poli& );
friend istream& operator >> (istream& , poli& );
public:
poli();
void penjumlahan(const poli&, const poli&);
void nilai(int);
private:
int elemen[100];
int banyak;
};

poli::poli(){
for(int i=0;i<banyak;i++)
elemen[i];
}
istream& operator >> (istream& in, poli& a){
cout<<“banyak elemen : “; in>>a.banyak;
cout<<“masukkan data polinomial : \n”;
for(int i=0;i<a.banyak;i++){
cout<<“variabel pangkat “<<i<<” :”;
cin>>a.elemen[i];
}
return in;
}
void poli::nilai(int i){
banyak= i;
}
ostream& operator << (ostream& out, poli& a){

for(int i=(a.banyak-1); i>=0; i–){
cout<<a.elemen[i];
if(i!=0) cout<<“x^”<<i<<“+”;
}
cout<<endl;
return out;
}
void poli::penjumlahan(const poli& a, const poli& b){
if(a.banyak>b.banyak)banyak=a.banyak;
else banyak=b.banyak;
for(int i=0;i<banyak;i++){
if ((a.banyak-1)<i) elemen[i]=b.elemen[i];
else if ((b.banyak-1)<i) elemen[i]=a.elemen[i];
else elemen[i]=a.elemen[i]+b.elemen[i];
}
}

int main(){
poli x, y, z;
cin>>x;
cout<<x;
cin>> y;
cout<<y;
z.penjumlahan(x,y);
cout<<“\n\nhasil penjumlahan 2 polinomial\n”<<z<<endl;
getch();
return 0;
}

Output program :

Program Presensi dengan C++

Source code program :

#include <cstdlib>
#include <iostream>

using namespace std;
//class Presensi
class Presensi{
public:
Presensi(){} //konstruktor
Presensi(string nama, string nim); //default konstruktor
void tambahPresensi(string nama, string nim); //method tambah presensi
void cetak(); //method untuk cetak presensi
private:
//struct Mahasiswa
struct Mahasiswa{
string Nama; // variabel Nama dengan tipe data string
string Nim; // variabel Nim dengan tipe data string
}
Mhs; //objek baru dari struct Mahasiswa dengan nama Mhs

};

Presensi::Presensi(string nama,string nim){
nama = “”;
nim = “”;
this->Mhs.Nama = nama; // assignment variabel nama ke Nama
this->Mhs.Nim = nim; // assignment variabel nim ke Nim
}

void Presensi::tambahPresensi(string nama, string nim){
this->Mhs.Nama = nama; // assignment variabel nama ke Nama
this->Mhs.Nim = nim; // assignment variabel nim ke Nim
}

void Presensi::cetak(){
cout<<Mhs.Nama<<” ( “<<Mhs.Nim<<” )”<<endl; // cetak presensi (Nama dan Nim)

}

int main(int argc, char *argv[])
{
Presensi x[2];
string a,b;

for(int i=0;i<2;i++){
cout<<“Masukkan Nama Anda :”;
cin>>a;
cout<<“Masukkan Nim Anda :”;
cin>>b;

x[i].tambahPresensi(a,b);
}

for(int j=0;j<2;j++){

x[j].cetak();

}

//Bikin main fungsi sendiri

system(“pause”);
return 0;
}

Contoh output program.

Program Interatif 1-10 dengan C++

Contoh program perhitungan interatif 1-10.

#include <iostream>

using namespace std;

class coba{
public:
coba(){n=10;}
void interatif(int n);
private:
int i, n;};
void coba::interatif(int n){
int i=1;

{
for(int i=1; i<=10; i++)

cout << i << endl;
}

}
int main(int argc, char *argv[])
{
{
coba x;
cout<<“\nAngka 1 Sampai Dengan 10 Secara Interatif:\n”;
x.interatif(0);
}

system(“pause”);
return 0;
}

Contoh output program.