Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<Windows.h>
- struct data{
- int angka;
- struct data *prev;
- struct data *next;
- }*head,*now,*temp,*tail;
- void ins(int x){
- now=head;
- for(now=head;now!=NULL;now=now->next){
- if(x>now->angka){
- temp=now;
- now=(struct data*)malloc(sizeof(data));
- now->angka=x;
- now->next=temp;
- now->prev=temp->prev;
- temp->prev=now;
- if(now->prev!=NULL){
- now=now->prev;
- now->next=temp->prev;
- }//kalo dia head
- else
- head=now;
- break;
- }
- else if (now->next==NULL){//kalo tail
- temp=now;
- now=(struct data*)malloc(sizeof(data));
- now->angka=x;
- now->prev=temp;
- now->next=0;
- tail=now;
- temp->next=now;
- break;
- }
- }
- }
- void intz(int x){
- now=(struct data*)malloc(sizeof(data));
- if(head==0)//data perdana
- tail=now;
- now->angka=x;
- now->next=head;//Double Link list Start
- head=now;
- now->prev=NULL;
- if(now->next!=0){
- temp=now;
- now=now->next;
- now->prev=temp;
- }// Double Link list end
- }
- void cetak(){
- for(now=tail;now!=NULL;now=now->prev){
- printf("%d",now->angka);
- if (now->prev!=NULL)
- printf(" -> ");
- }
- printf("\n");
- }
- int main(){
- head=0;
- temp=0;
- printf("Sebelum insert\n");
- intz(1);
- intz(3);
- cetak();
- printf("Setelah insert awal\n");
- ins(5);
- cetak();
- printf("Setelah insert akhir\n");
- ins(0);
- cetak();
- printf("Setelah insert tengah\n");
- ins(2);
- cetak();
- now=head;
- getchar();
- }
Advertisement
Add Comment
Please, Sign In to add comment