Jumat, 30 November 2012

Circular Link List


#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<<q->info<<" ";
                                   q=q->link;
                   }
                   cout<<last->info;
   }

Dobel Link List


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

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);
}
}


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 Memecah Stack


#include <iostream>;
using namespace std;
                class stack
                {
                 
public :
                void input();
                void gabung();
                void output();
                 
private:
                char b[10], c[10], a[10];
                int k,l;
                };
                 
                void stack::input()
                {
                cout << "massukkan banyaknya tumpukan 1 : "; cin >> k;
                for (int i=0; i<k; i++)
                {cout << "masukkan elemen : "; cin >> b[i];}
                 
                cout << "massukkan banyaknya tumpukan 2 : "; cin >> l;
                for (int j=0; j<l; j++)
                {cout << "masukkan elemen : "; cin >> c[j];}
                }
                 void stack::gabung()
                {
                int j=k-1;
                int g=l;
                 for(int i=(k+l); i>=0; i--)
                {                  
                if(i<k)
                {a[i]=b[j];
                j--;}
                else
                {a[i]=c[g];
                g--;}
                }
                cout << "isi elemen tumpukan adalah"<<endl;
                for (int j =0; j< k+l; j++)
                {cout << a[j]<<" ";}
                 }
                 int main ()
                {
                 stack x;
                x.input();
                x.gabung();
                     system("PAUSE");
                    return EXIT_SUCCESS;
                }