c程序改成java 双向链表的演示 哪位大哥帮帮忙。。。
#include <graphics.h>#include <stdio.h>
#include <string.h>
#define x0 30
#define y0 200
#define tabcolor 11
#define h_pointcolor 15
#define s_pointcolor 13
#define del_pointcolor 14
#define freecolor 4
#define strcolor 12
#define u_dpointhigh 25
#define wid 80
#define high 30
#define tabge 20
#define shannum 2
#define shanspeed 65535
#define autospeed 65535
typedef struct two
{
struct two *before;
int x;
int y;
int num;
struct two *next;
}twopoint;
int checkplaydemo()
{
char choice;
again:
printf("\n\nDo you want to look the Demo?[Y/N]\nPlease Enter:");
choice=getche();
getch();
if(toupper(choice)=='Y')return 1;
else if(toupper(choice)=='N')return 0;
else {printf("\n\nEnter error! Please Enter again.\nPress any key to continue ...");
goto again;
}
}
int pause(int fast)
{
int esc;
if(!fast)
{
if(kbhit())
{
esc=getch();
if(esc==27)return 0;
else delay(autospeed);
}
else
delay(autospeed);
}
return 1;
}
kuang(twopoint *p)
{
int oldcolor;
char numchar[10];
oldcolor=getcolor();
setcolor(tabcolor);
rectangle(p->x,p->y,p->x+wid,p->y+high);
line(p->x+(int)(7*wid/8.0),p->y+1,p->x+(int)(7*wid/8.0),p->y+high-1);
line(p->x+(int)(wid/8.0),p->y+1,p->x+(int)(wid/8.0),p->y+high-1);
sprintf(numchar,"%d",p->num);
setcolor(2);
if(p->x!=x0||p->y!=y0)outtextxy(p->x+(int)(wid/2.0)-(int)(textwidth(numchar)/2.0),p->y+13,numchar);
else outtextxy(p->x+(int)(wid/8.0)+13,p->y+13,"head");
setcolor(oldcolor);
}
/*headkuang(twopoint *p)
{
int oldcolor;
oldcolor=getcolor();
setwritemode(1);
p->x=x0;p->y=y0;
kuang(p);
setcolor(tabcolor);
line(p->x+(int)(wid/2.0),p->y+1,p->x+(int)(wid/2.0),p->y+high-1);
setcolor(4);
outtextxy(p->x+(int)(wid/8.0)+13,p->y+13,"head");
setcolor(oldcolor);
} */
pointdown(twopoint *p,char str[])
{
int oldcolor;
oldcolor=getcolor();
setcolor(s_pointcolor);
setwritemode(1);
line(p->x+(int)(wid/2.0),p->y-u_dpointhigh,p->x+(int)(wid/2.0),p->y-3);
moveto(p->x+(int)(wid/2.0),p->y-3);
lineto(p->x+(int)(wid/2.0)-3,p->y-11);
lineto(p->x+(int)(wid/2.0),p->y-8);
lineto(p->x+(int)(wid/2.0)+3,p->y-11);
lineto(p->x+(int)(wid/2.0),p->y-3);
setcolor(strcolor);
if(strcmp(str,"Head")) settextstyle(2,0,7);
outtextxy(p->x+(int)(wid/2.0)-(int)(textwidth(str)/2.0),p->y-u_dpointhigh-23,str);
settextstyle(0,0,1);
setwritemode(0);
setcolor(oldcolor);
}
pointup(twopoint *p,char str[])
{
int oldcolor;
oldcolor=getcolor();
setcolor(s_pointcolor);
setwritemode(1);
line(p->x+(int)(wid/2.0),p->y+high+u_dpointhigh,p->x+(int)(wid/2.0),p->y+high+3);
moveto(p->x+(int)(wid/2.0),p->y+high+3);
lineto(p->x+(int)(wid/2.0)-3,p->y+high+11);
lineto(p->x+(int)(wid/2.0),p->y+high+8);
lineto(p->x+(int)(wid/2.0)+3,p->y+high+11);
lineto(p->x+(int)(wid/2.0),p->y+high+3);
setcolor(strcolor);
if(strcmp(str,"Head")) settextstyle(2,0,7);
outtextxy(p->x+(int)(wid/2.0)-(int)(textwidth(str)/2.0),p->y+high+u_dpointhigh,str);
settextstyle(0,0,1);
setwritemode(0);
setcolor(oldcolor);
}
pointnext(twopoint *p,int fast)
{
int oldcolor,i,n;
setwritemode(1);
oldcolor=getcolor();
setcolor(h_pointcolor);
if(!fast)i=2*shannum+1;
else i=1;
for(n=1;n<=i;n++)
{
line(p->x+(int)(15*wid/16.0),p->y+(int)(high/4.0),p->x+wid+tabge+(int)(wid/16.0),p->y+(int)(high/4.0));
moveto(p->x+wid+tabge+(int)(wid/16.0),p->y+(int)(high/4.0));
lineto(p->x+wid+tabge+(int)(wid/16.0)-9,p->y+(int)(high/4.0)-2);
lineto(p->x+wid+tabge+(int)(wid/16.0)-7,p->y+(int)(high/4.0));
lineto(p->x+wid+tabge+(int)(wid/16.0)-9,p->y+(int)(high/4.0)+2);
lineto(p->x+wid+tabge+(int)(wid/16.0),p->y+(int)(high/4.0));
if(!fast)delay(shanspeed);
}
setcolor(oldcolor);
setwritemode(0);
}
pointbefore(twopoint *p,int fast)
{
int oldcolor,i,n;
setwritemode(1);
oldcolor=getcolor();
setcolor(h_pointcolor);
if(!fast)i=2*shannum+1;
else i=1;
for(n=1;n<=i;n++)
{
line(p->x+wid+tabge+(int)(wid/16.0),p->y+(int)(3*high/4.0),p->x+(int)(15*wid/16.0),p->y+(int)(3*high/4.0));
moveto(p->x+(int)(15*wid/16.0),p->y+(int)(3*high/4.0));
lineto(p->x+(int)(15*wid/16.0)+9,p->y+(int)(3*high/4.0)-2);
lineto(p->x+(int)(15*wid/16.0)+7,p->y+(int)(3*high/4.0));
lineto(p->x+(int)(15*wid/16.0)+9,p->y+(int)(3*high/4.0)+2);
lineto(p->x+(int)(15*wid/16.0),p->y+(int)(3*high/4.0));
if(!fast)delay(shanspeed);
}
setcolor(oldcolor);
setwritemode(0);
}
link_w_h(twopoint *head,twopoint *p,int fast)
{
int oldcolor,i,n;
setwritemode(1);
oldcolor=getcolor();
setcolor(h_pointcolor);
if(!fast)i=2*shannum+1;
else i=1;
for(n=1;n<=i;n++)
{ line(p->x+(int)(15*wid/16.0),p->y+(int)(high/4.0),p->x+wid+tabge,p->y+(int)(high/4.0));
moveto(p->x+wid+tabge,p->y+(int)(high/4.0));
lineto(p->x+wid+tabge,p->y+2*high);
lineto(head->x-tabge,p->y+2*high);
lineto(head->x-tabge,p->y+(int)(high/4.0));
lineto(head->x+(int)(wid/16.0),p->y+(int)(high/4.0));
lineto(head->x+(int)(wid/16.0)-9,head->y+(int)(high/4.0)-2);
lineto(head->x+(int)(wid/16.0)-7,head->y+(int)(high/4.0));
lineto(head->x+(int)(wid/16.0)-9,head->y+(int)(high/4.0)+2);
lineto(head->x+(int)(wid/16.0),head->y+(int)(high/4.0));
if(!fast)delay(shanspeed);
}
setcolor(oldcolor);
setwritemode(0);
}
link_h_w(twopoint *head,twopoint *p,int fast)
{
int oldcolor,i,n;
setwritemode(1);
oldcolor=getcolor();
setcolor(h_pointcolor);
if(!fast)i=2*shannum+1;
else i=1;
for(n=1;n<=i;n++)
{ line(p->x+(int)(15*wid/16.0),p->y+(int)(3*high/4.0),p->x+wid+(int)(2*tabge/5.0),p->y+(int)(3*high/4.0));
moveto(p->x+wid+(int)(2*tabge/5.0),p->y+(int)(3*high/4.0));
lineto(p->x+wid+(int)(2*tabge/5.0),p->y+high+(int)(high/2.0));
lineto(head->x-(int)(2*tabge/5.0),p->y+high+(int)(high/2.0));
lineto(head->x-(int)(2*tabge/5.0),p->y+(int)(3*high/4.0));
lineto(head->x+(int)(wid/16.0),p->y+(int)(3*high/4.0));
moveto(p->x+(int)(15*wid/16.0),p->y+(int)(3*high/4.0));
lineto(p->x+(int)(15*wid/16.0)+9,p->y+(int)(3*high/4.0)-2);
lineto(p->x+(int)(15*wid/16.0)+7,p->y+(int)(3*high/4.0));
lineto(p->x+(int)(15*wid/16.0)+9,p->y+(int)(3*high/4.0)+2);
lineto(p->x+(int)(15*wid/16.0),p->y+(int)(3*high/4.0));
if(!fast)delay(shanspeed);
}
setcolor(oldcolor);
setwritemode(0);
}
int creatchaindemo(twopoint *head,int fast)
{
twopoint *p,*q;
int n=0;
p=head;
q=NULL;
setbkcolor(1);
if(!pause(fast))return 0;
kuang(head);
pointdown(head,"Head");
if(!pause(fast))return 0;
pointup(p,"p");
while(p->next!=head)
{n++;
getch();
if(!pause(fast))return 0;
if(n!=1)pointdown(q,"q");
q=p->next;
kuang(q);
pointdown(q,"q");
if(!pause(fast))return 0;
pointnext(p,fast);
if(!pause(fast))return 0;
pointbefore(p,fast);
if(!pause(fast))return 0;
pointup(p,"p");
p=q;
pointup(p,"p");
}
if(!pause(fast))return 0;
link_w_h(head,p,fast);
if(!pause(fast))return 0;
link_h_w(head,p,fast);
return 1;
}
deleltedemo(twopoint *head,int n)
{
twopoint *p;
int i,oldcolor;
p=head;
oldcolor=getcolor();
pointup(head->before,"p");
pointdown(head->before,"q");
pointup(p,"p");
if(!pause(0))return;
for(i=1;i<=n;i++)
{
pointup(p,"p");
p=p->next;
if(!pause(0))return;
pointup(p,"p");
if(!pause(0))return;
}
if(!pause(0))return;
pointnext(p->before,0);
if(!pause(0))return;
setwritemode(1);
setcolor(del_pointcolor);
for(i=1;i<=2*shannum+1;i++)
{ if(i==2*shannum+1)setwritemode(0);
line(p->before->x+(int)(15*wid/16.0),p->before->y+(int)(high/4.0),p->before->x+wid+(int)(tabge/3.0),p->before->y+(int)(high/4.0));
moveto(p->before->x+wid+(int)(tabge/3.0),p->before->y+(int)(high/4.0));
lineto(p->before->x+wid+(int)(tabge/3.0),p->before->y-(int)(2*high/3.0));
lineto(p->next->x-tabge/3,p->next->y-(int)(2*high/3.0));
lineto(p->next->x-tabge/3,p->next->y+(int)(high/4.0));
if(i==1)
{ setcolor(h_pointcolor);
lineto(p->next->x+wid/16.0,p->next->y+(int)(high/4.0));
lineto(p->next->x+(int)(wid/16.0)-9,p->next->y+(int)(high/4.0)-2);
lineto(p->next->x+(int)(wid/16.0)-7,p->next->y+(int)(high/4.0));
lineto(p->next->x+(int)(wid/16.0)-9,p->next->y+(int)(high/4.0)+2);
lineto(p->next->x+(int)(wid/16.0),p->next->y+(int)(high/4.0));
}
setcolor(del_pointcolor);
moveto(p->next->x-tabge/3,p->next->y+(int)(high/4.0));
lineto(p->next->x+wid/16.0,p->next->y+(int)(high/4.0));
lineto(p->next->x+(int)(wid/16.0)-9,p->next->y+(int)(high/4.0)-2);
lineto(p->next->x+(int)(wid/16.0)-7,p->next->y+(int)(high/4.0));
lineto(p->next->x+(int)(wid/16.0)-9,p->next->y+(int)(high/4.0)+2);
lineto(p->next->x+(int)(wid/16.0),p->next->y+(int)(high/4.0));
delay(shanspeed);
}
if(!pause(0))return;
if(p->next==head)link_h_w(head,p,0);
else pointbefore(p,0);
if(!pause(0))return;
setwritemode(1);
for(i=1;i<=2*shannum+1;i++)
{ if(i==2*shannum+1)setwritemode(0);
line(p->next->x+(int)(wid/16.0),p->next->y+(int)(3*high/4.0),p->next->x-(int)(2*tabge/3.0),p->next->y+(int)(3*high/4.0));
moveto(p->next->x-(int)(2*tabge/3.0),p->next->y+(int)(3*high/4.0));
lineto(p->next->x-(int)(2*tabge/3.0),p->next->y-(int)(high/3.0));
lineto(p->before->x+wid+(int)(2*tabge/3.0),p->before->y-(int)(high/3.0));
lineto(p->before->x+wid+(int)(2*tabge/3.0),p->before->y+(int)(3*high/4.0));
if(i==1)
{ setcolor(h_pointcolor);
lineto(p->before->x+(int)(15*wid/16.0),p->before->y+(int)(3*high/4.0));
lineto(p->before->x+(int)(15*wid/16.0)+9,p->before->y+(int)(3*high/4.0)-2);
lineto(p->before->x+(int)(15*wid/16.0)+7,p->before->y+(int)(3*high/4.0));
lineto(p->before->x+(int)(15*wid/16.0)+9,p->before->y+(int)(3*high/4.0)+2);
lineto(p->before->x+(int)(15*wid/16.0),p->before->y+(int)(3*high/4.0));
}
setcolor(del_pointcolor);
moveto(p->before->x+wid+(int)(2*tabge/3.0),p->before->y+(int)(3*high/4.0));
lineto(p->before->x+(int)(15*wid/16.0),p->before->y+(int)(3*high/4.0));
lineto(p->before->x+(int)(15*wid/16.0)+9,p->before->y+(int)(3*high/4.0)-2);
lineto(p->before->x+(int)(15*wid/16.0)+7,p->before->y+(int)(3*high/4.0));
lineto(p->before->x+(int)(15*wid/16.0)+9,p->before->y+(int)(3*high/4.0)+2);
lineto(p->before->x+(int)(15*wid/16.0),p->before->y+(int)(3*high/4.0));
delay(shanspeed);
}
setwritemode(1);
setcolor(h_pointcolor);
if(!pause(0))return;
for(i=1;i<=2*shannum+1;i++)
{ line(p->x+(int)(wid/16.0),p->y+(int)(3*high/4.0),p->x-(int)(tabge/3.0),p->y+(int)(3*high/4.0));
delay(shanspeed);
}
if(!pause(0))return;
for(i=1;i<=2*shannum+1;i++)
{ line(p->x+(int)(15*wid/16.0),p->y+(int)(high/4.0),p->x+wid+(int)(2*tabge/3.0),p->y+(int)(high/4.0));
delay(shanspeed);
}
setwritemode(0);
setcolor(del_pointcolor);
line(p->x+wid+tabge/3.0+1,p->y-high/3.0,p->x+wid+tabge/3.0+1,p->y+3*high/4.0);
if(!pause(0))return;
setcolor(freecolor);
arc(p->x+wid,p->y,90,190,wid/2.0);
moveto(p->x+wid,p->y-(int)(wid/2.0));
lineto(p->x+wid-9,p->y-(int)(wid/2.0)-2);
lineto(p->x+wid-7,p->y-(int)(wid/2.0));
lineto(p->x+wid-9,p->y-(int)(wid/2.0)+2);
lineto(p->x+wid,p->y-(int)(wid/2.0));
setcolor(strcolor);
outtextxy(p->x+wid-textwidth("free")/2.0,p->y-(int)(wid/2.0)-15,"free");
setcolor(oldcolor);
setwritemode(0);
}
outputchain(twopoint *head)
{
twopoint *p;
int nextorbefore;
again:
clrscr();
printf("------Output the chain------\nFor next or For before output?[N/B]\nPlease Enter:");
nextorbefore=getche();
getch();
if(tolower(nextorbefore)=='n')
{
printf("\n\t\b\bp -> num\n");
p=head->next;
while(p!=head)
{
printf("\n\t%d\n",p->num);
p=p->next;
}
}
else if(tolower(nextorbefore)=='b')
{
printf("\n\t\b\bp -> num\n");
p=head->before;
while(p!=head)
{
printf("\n\t%d\n",p->num);
p=p->before;
}
}
else {printf("\n\nEnter error! Please enter again.\nPress any key to continue ...");getch();goto again;}
printf("\nPress any key to continue ...");
getch();
}
creatchain(twopoint *head)
{
twopoint *p,*q;
int n=1,allnum;
head->x=x0;
head->y=y0;
p=head;
q=NULL;
again:
clrscr();
printf("----------Creat chain----------\nPlease Enter the point num( num<=5 ).\nnum=");
scanf("%d",&allnum);
if(allnum<=0||allnum>5){printf("The number is full!Enter again.");getch();goto again;}
printf("\nPlease enter the value(int) of every point.");
for(n=1;n<=allnum;n++)
{
q=(twopoint *)malloc(sizeof(twopoint));
printf("\nPoint %d: ",n);
scanf("%d",&q->num);
q->x=x0+n*(wid+tabge);
q->y=y0;
p->next=q;
q->before=p;
p=q;
}
p->next=head;
head->before=p;
printf("The chain has been created!It will be Displayed.\nPress any key to continue ...\n");
getch();
}
int delete(twopoint *head)
{
twopoint *p,*q;
int n,i,mx,my,nx,ny;
again:
p=head;
clrscr();
printf("---------- Delete a point ----------\nPlease Enter the deleted point:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{ p=p->next;
if(p==head)
{ printf("The number %d is wrong!Please enter again.\nPress any key to continue ...",n);
getch();
goto again;
}
}
p->before->next=p->next;
p->next->before=p->before;
q=p;
mx=q->x;
my=q->y;
while(1)
{ nx=q->next->x;ny=q->next->y;
q->next->x=mx;q->next->y=my;
q=q->next;
if(q->next==head)break;
mx=q->next->x;my=q->next->y;
q->next->x=nx;q->next->y=ny;
q=q->next;
if(q->next==head)break;
}
p->before=NULL;
p->next=NULL;
free(p);
printf("\n\nThe %dth point has been deleted,it will display the chain.\nPress any key continue ...",n);
getch();
return n;
}
int insert(twopoint *head)
{
twopoint *p,*q;
int n,i;
p=head;
q=(twopoint *)malloc(sizeof(twopoint));
clrscr();
printf("---------- Insert a point ---------\n");
printf("Enter the insert address:");
scanf("%d",&n);
printf("\nand the number:");
scanf("%d",&q->num);
for(i=1;i<=n;i++)p=p->next;
q->x=p->x-(int)((tabge+wid)/2.0);
q->y=p->y-2*high;
p->before->next=q;
q->before=p->before;
q->next=p;
p->before=q;
printf("\n\nThe point before %dth point has been inserted,it will display the chain.\nPress any key continue ...",n);
getch();
return n;
}
copychain(twopoint *midhead,twopoint *head)
{
twopoint *p,*m,*n;
p=head->next;
m=midhead;
midhead->x=head->x;
midhead->y=head->y;
while(p!=head)
{
n=(twopoint *)malloc(sizeof(twopoint));
n->x=p->x;
n->y=p->y;
n->num=p->num;
m->next=n;n->before=m;
m=n;
p=p->next;
}
m->next=midhead;
midhead->before=m;
}
main()
{
twopoint chainhead,*head=&chainhead;
twopoint midchainhead,*midhead=&midchainhead;
int gd=0,gm=1;
int n;
initgraph(&gd,&gm,"");
restorecrtmode();
creatchain(head);
outputchain(head);
if(checkplaydemo())
{setgraphmode(2);if(creatchaindemo(head,0))getch();}
restorecrtmode();
copychain(midhead,head);
n=delete(head);
outputchain(head);
if(checkplaydemo())
{setgraphmode(2);
creatchaindemo(midhead,1);deleltedemo(midhead,n);getch();}
cleardevice();
creatchaindemo(head,1);
getch();
/* restorecrtmode();
copychain(midhead,head);
n=insert(head);
outputchain(midhead);
outputchain(head);
outputchain(midhead);
/* if(checkplaydemo())
{setgraphmode(2);
creatchaindemo(midhead,1);insertdemo(midhead,n);}*/*/
}