/*code for linklist operation
*written by Rockrush,NJUE
*2008-05-15
*/
#define PROMPT " Rock>>"
#include <stdio.h>
#include <conio.h>
typedef struct lnode
{
char data;
struct lnode *next;
}lnode;
//definition of global symbol.
lnode *head,*pre;
int length;
/*definition of functions*/
void welcome()
{
int i,j=0,k=0;
window(1,1,80,25);
textbackground(1);
clrscr();
printf("\n\tWelcome to linklist demo!\n\n\tCopyright(c) 2008/05/29\
RockWork Corp.\n\n");
printf("\t / `._ _.' \ \n");
printf("\t( @ : `. .' : @ ) \n");
printf("\t \\ `. `. ._ _. .' .' / \n");
printf("\t \\;' `. `. \\ / .' .' `;/ \n");
printf("\t \\`. `. \\ \\_/ / .' .'/ \n");
printf("\t ) :-._`. \\ (:) / .'_.-: ( \n");
printf("\t (`.....,`.\\/:\\/.',.....') \n");
printf("\t >------._|:::|_.------< \n");
printf("\t / .'._>_.-|:::|-._<_.'. \\ \n");
printf("\t |o _.-'_.-^|:|^-._`-._ o| \n");
printf("\t |`' ;_.-'|:|`-._; `'| \n");
printf("\t "".o_.-' ;.""|:|"".; `-._o."" \n");
printf("\t "".__."" \\:/ "".__."" \n");
gotoxy(4,25);
cprintf("Press any key to continue,'a' to abort...");
if(getch()=='a')exit(1);
clrscr();
gotoxy(13,10);
cprintf("Now program is loading,please get ready. [Just for fun!]");
gotoxy(30,12);
cprintf("Process:");
gotoxy(9,13);
printf("<---------------------------------------------------------------->");
delay(800);
printf("\r\t<");
for(i=26;i>0;i--){delay(80);gotoxy(38,12);cprintf(" %d%%",2*j++-2);gotoxy(10+k++,13);cprintf("@");}
printf(" Loading... ");
for(i=26;i>0;i--){delay(80);gotoxy(38,12);cprintf(" %d%%",2*j++-2);gotoxy(22+k++,13);cprintf("@");}
clrscr();
}
void initlist()
{
lnode *t,*p;
char ch;
head=(lnode *)malloc(sizeof(lnode));
head->data=':';
t=head;
getchar();
printf(" Input charactors:");
while((ch=getchar())!='\n')
{
p=(lnode*)malloc(sizeof(lnode));
p->data=ch;
p->next=NULL;
t->next=p;
t=t->next;
length++;
}
if(head!=NULL)printf(" Linklist create successfully,Enter to continue.\n");
}
void destroy()
{
lnode *pp,*pn;
pp=head;
while(pp!=NULL)
{
pn=pp->next;
free(pp);
pp=pn;
}
head=NULL;
if(head==NULL)puts("\t Linklist destroy successfully.");
}
int listempty()
{
if(head->next==NULL)return 1;
else return 0;
}
void locate()
{
char sc;
int i;
printf("\tInput the charactor you are finding for:");
getchar();
sc=getchar();
pre=head;
for(i=0;i<length+1;i++)
{
if(pre->data==sc)break;
else pre=pre->next;
}
if(i<length+1)printf("\tCharactor found,the first one is the %dth element.\n",i);
else printf("\tThe charactor is not found.\n");
}
void insert()
{
char elem;
int loc;
lnode *pe;
pre=head;
getchar();
printf("\tInput the element(charactor) you want to insert:");
scanf("%c",&elem);
printf("\tInput the location you want the element to be inserted:");
scanf("%d",&loc);
for(loc;loc>1;loc--)pre=pre->next;
pe=(lnode *)malloc(sizeof(lnode));
pe->data=elem;
pe->next=pre->next;
pre->next=pe;
length++;
}
void deletel()
{
int loc;
lnode *pe;
pre=head;
printf(" Input location of the element you want to delete:");
scanf("%d",&loc);
for(loc;loc>1;loc--)pre=pre->next;
pe=pre->next;
pre->next=pe->next;
free(pe);
length--;
}
void traverse()
{
lnode *pp;
pp=head;
printf(" The elements are as follows");
while(pp)
{
printf(" %c",pp->data);
pp=pp->next;
}
printf("\n");
}
void bye()
{
int i,j=0,k=0;
clrscr();
gotoxy(9,13);
printf(">----------------------------------------------------------------<");
delay(1000);
printf("\r\t>");
for(i=26;i>0;i--){delay(30);gotoxy(38,12);cprintf(" %d%%",2*j++-2);gotoxy(10+k++,13);cprintf("@");}
printf(" Bye-bye! ");
for(i=26;i>0;i--){delay(30);gotoxy(38,12);cprintf(" %d%%",2*j++-2);gotoxy(22+k++,13);cprintf("@");}
clrscr();
textbackground(2);
window(16,12,61,14);
clrscr();
textbackground(4);
window(20,13,60,13);
cprintf("Believe in yourself,you can do better!");
delay(2000);
exit(1);
}
void option()
{
printf("\n Selections:\n");
printf(" LEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEI\n");
printf(" H H\n");
printf(" H A:Initialise a linklist with charactors given by user. H\n");
printf(" H B:Destroy the linklist created before. H\n");
printf(" H C:Judge if the pre-created linklist is empty H\n");
printf(" H D:Get the length of created linklist and display on monitor. H\n");
printf(" H E:Find an special element given by user. H\n");
printf(" H F:Insert an special element given by user. H\n");
printf(" H G:Delete an special element given by user. H\n");
printf(" H H:Display the whole linklist on monitor. H\n");
printf(" H H\n");
printf(" H H\n");
printf(" H S:Clear the screen H\n");
printf(" H Z:Exit H\n");
printf(" H H\n");
printf(" NEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEK\n");
printf("\tNow enter the first letter to select operation \n\
Type 'M' to call this menu again.\n");
}
void select(char a)
{
switch(a)
{
case 'A':initlist();break;
case 'B':destroy();break;
case 'C':if(listempty())puts("\tThe linklist is empty.");
else puts("\tThe linklist is not empty.");
break;
case 'D':printf("\tThe length of linklist is %d.\n",length);
break;
case 'E':locate();break;
case 'F':insert();break;
case 'G':deletel();break;
case 'H':traverse();break;
case 'M':option();break;
case 'S':clrscr();break;
case 'Z':bye();
default: clrscr();printf("\t************* Bad command! Try again. *************");delay(3000);option();
}
getchar();
}
void main()
{
char op;
welcome();
clrscr();
option();
while(1)
{
printf(PROMPT);
scanf("%c",&op);
select(op);
}
}/*end of function main() */