bunga

Program bahasa C dengan Prinsip Linked List

Buatlah program (diutamakan bahasa C) yang menggunakan prinsip linked list. Isi program bebas.
Jawab :

Contoh program 1 :

#include
#include
#include
#include

class Node
{
public:
int x;
Node *ka;
};

void main()
{
clrscr();
int n,i;
Node *awal, *akhir, *temp;
awal = new Node;
akhir = awal;
cout<<"N = " ; cin>>n;
for ( i=1; i<=n; ++i)
{
temp = new Node;
temp->ka = NULL;
cout<<"Data ke " < temp = awal->ka;
while (temp->ka !=NULL)
{
cout << temp->x < temp = temp-> ka;
}
cout<< temp-> x < cout<<"Seluruh Data2\n";
temp = awal->ka;
while (temp !=NULL)
{
cout << temp->x < temp = temp-> ka;
}

int p;
cout<<"Hapus posisi ke " ; cin>>p;
Node *temp1, *temp2;
temp1 = awal;
for ( i= 1; i temp1=temp1->ka;
temp2 = temp1->ka;
temp1->ka = temp2->ka;
delete temp2;

cout<<"Seluruh Data\n";
temp = awal->ka;
while (temp !=NULL)
{
cout << temp->x < temp = temp-> ka;
}
getch();
}
Contoh program 2 :
Program untuk mendemostrasikan operasi linked list:
#include
#include
#include"malloc.h"
struct node
{
int data;
struct node*link;
};
void main()
{
int a=111,b=2,c=3,will,wish,num;
struct node*ptr,*ptr2,*result,*temp;
void add(struct node**,int );
struct node*search(struct node*);
void display(struct node *);
void invert(struct node*);
void del(struct node*,int);
struct node*concat(struct node*,struct node*);
ptr=NULL;
ptr2=NULL;
result=NULL; //result for storing the result of concatenation
clrscr();
will=1;
while(will==1)
{
printf("
Main Menu
1.Add element
2.Delete element
3.Search element
4.Linked List concatenation
5.Invert linked list
6.Display elements
Please enter the choice");
scanf("%d",&wish);
switch(wish)

{
case1:
printf("
Enter the element you want to add ");
scanf("%d",&num);
add(&ptr,num);
display(ptr);
break;
case2:
printf("
Enter the element to delete");
scanf("%d",&num);
del(ptr,num);
break;
case3:
printf("
Now demonstrating search");
temp=search(ptr);
printf("
Address of first occurenceis %u",temp);
break;
case4:
/* Inputs given internally fo rdemo only*/
printf(" Now demonstrating linked list concatenation
Press anykey tocontinue...");
add(&ptr2,2);
add(&ptr2,4);
add(&ptr2,6);
getch();
printf("

Displaying second Linked List
");
display(ptr2);
getch();
result =concat(ptr,ptr2);
clrscr();
printf("
Now Displaying the result of concatenation");
display(result);
getch();
break;
case5:
printf("

Inverting the list ...
Press anykey tocontinue...");
invert(ptr);
break;
case6:
display(ptr);
break;
default:
printf("

Illegal choice
");
}
printf("
DO you want tocontinue(press 1 for yes ");
scanf("%d",&will);
} //endofwhile
}
void add(struct node**q,int num)
{
struct node*temp;
temp=*q;
if(*q==NULL)
{
*q=malloc(sizeof(struct node));
temp=*q;
}
else
{
while((temp->link)!=NULL)
{
temp=temp->link;
}
temp->link=malloc(sizeof(struct node));
temp=temp->link;
}
temp->data =num;
temp->link =NULL;
}
void display(struct node *pt)
{
while(pt!=NULL)
{
printf("
Data: %d",pt->data);
printf("
Link: %d",pt->link);
pt=pt->link;
}
}
voidinvert(struct node*ptr)
{
struct node *p,*q,*r;
p=ptr;
q=NULL;
while(p!=NULL)
{
r=q;
q=p;
p=p->link;
q->link=r;
}
ptr=q;
display(ptr);
}
// CONCATENATION OF LINKED LISTS
struct node*concat(struct node*p,struct node*q)
{
struct node*x,*r;
if(p==NULL)
r=q;
if(q==NULL)
r=p;
else
{
x=p;
r=x;
while(x->link!=NULL)
x=x->link;
x->link=q;
}
return(r);
}
// SEARCHING AN ELEMENT INTHE LINKED LIST
// THIS FUNCTION FINDS THE FIRST OCCURRENCE OF
// A DATA AND RETURNS A POINTER TO ITS ADDRESS
struct node*search(struct node*p)
{
struct node*temp;
int num;
temp=p;
printf("
Enter the data that you want tosearch ");
scanf("%d",&num);
printf("
Link of temp%u",temp->link);
while(temp->link!=NULL)
{
printf("
In while");
if(temp->data==num)
return(temp);
temp=temp->link;
}
return(NULL);
}
// DELETING DATA FROM THE LINKED LIST//
voiddel(struct node*p,int num)
{
struct node*temp,*x;
temp=p;
x=NULL;
while(temp->link!=NULL)
{
if(temp->data==num)
{
if(x==NULL)
{
p=temp->link;
free(temp);
return;
}
else
{
x->link=temp->link;
free(temp);
return;
}
} //end of outer if
x=temp;
temp=temp->link;
} //end of while
printf("
No such entry to delete ");
} //end of fn.