?? list1.c
字號(hào):
#include<stdio.h>
struct Link
{
int data;
struct Link *next;
};
/*創(chuàng)建鏈表并將文件中的數(shù)據(jù)讀入鏈表中*/
struct Link *CreatLink()
{
struct Link *p,*head;
FILE *fp;
int num;
char filename[20];
printf("please input the filename:");
scanf("%s",filename);
if((fp=fopen(filename,"r"))==NULL)
{
printf("cannot open file\n");
getch();
exit(0);
}
head=(struct Link *)malloc(sizeof(struct Link));
head->next=NULL;
p=head;
do
{
fscanf(fp,"%d",&p->data);
p->next=(struct Link *)malloc(sizeof(struct Link));
p=p->next;
p->next=NULL;
}while(!feof(fp));
printf("you have create the link!\n");
fclose(fp);
return head;
}
/*此函數(shù)用于顯示鏈表*/
void DispLink(struct Link *head)
{
struct Link *q;
q=head;
do{
printf("%d\t",q->data);
q=q->next;
}while(q->next!=NULL);
printf("\n");
}
/*此函數(shù)用于刪除鏈表中的某一元素,需要參數(shù)為鏈表頭指針,要?jiǎng)h除的元素*/
struct Link *DelNode(struct Link *head,long num)
{
struct Link *p,*pr;
if(head==NULL)
{
printf("\n No Linkde Table\n");
return head;
}
p=head;
while(num!=p->data&&p->next!=NULL)
{
pr=p;
p=p->next;
}
if(num==p->data)
{
if(p==head)
head=p->next;
else
pr->next=p->next;
free(p);
printf("delete the node\n");
}
else
{
printf("Not found the node\n");
}
return head;
}
/*這個(gè)函數(shù)用于查找鏈表中的元素*/
struct Link *FinNode(struct Link *head,long num)
{
struct Link *p;
int i=1;
if(head==NULL)
{
printf(" No Linkde Table\n");
return head;
}
p=head;
while(num!=p->data&&p->next!=NULL)
{
p=p->next;
i++;
}
if(num==p->data)
{
printf("it is the %d number\n",i);
}
else
printf("Not found the node\n");
}
/*這個(gè)函數(shù)用于向鏈表中插入元素*/
struct Link *InsNode(struct Link *head)
{
struct Link *p,*pr;
int i=1,tdata,num;
p=(struct Link*)malloc(sizeof(struct Link));
p->next=NULL;
if(p==NULL)
{
printf("Can't enough memory to alloc\n");
exit(0);
}
pr=head;
printf("please input the number you want to insert:");
scanf("%d",&tdata);
p->data=tdata;
printf("please input the position you want to insert the number:");
scanf("%d",&num);
while(i<num&&pr->next!=NULL)
{
pr=pr->next;
i++;
}
if(pr==head)
{
printf("you have input the wrong position!\n");
}
else
{
p->next=pr->next;
pr->next=p;
}
return head;
}
/*這個(gè)函數(shù)用于計(jì)算鏈表的長(zhǎng)度*/
void LonNode(struct Link *head)
{
struct Link *p;
int i=0;
p=head;
while(p->next!=NULL)
{
p=p->next;
i++;
}
printf("the length of the link is %d\n",i);
}
/*這個(gè)函數(shù)用于實(shí)現(xiàn)鏈表的倒轉(zhuǎn)*/
struct Link *ReNode(struct Link *head)
{
struct Link *p,*q;
q=(struct Link*)malloc(sizeof(struct Link));
q->next=NULL;
p=head->next;
head->next=q;
while(p->next!=NULL)
{
q=p->next;
p->next=head;
head=p;
p=q;
}
free(p);
return head;
}
/*下面兩個(gè)函數(shù)用于實(shí)現(xiàn)鏈表的升序*/
struct Link *Fin(struct Link *head)
{
struct Link *p,*q;
int n;
p=head;
n=p->data;
for(;p->next!=NULL;p=p->next)
{
if(n>p->data)
n=p->data;
}
while(n!=p->data)
{
q=p;
p=p->next;
}
q->next=p->next;
return p;
}
struct Link *Paixu(struct Link *head)
{
struct Link *q,*p;
p=Fin(head);
q=p;
do{
q->next=Fin(head);
q=q->next;
}while(Fin(head)!=NULL);
q->next=NULL;
return p;
}
main()
{
FILE *fp;
char op;
struct Link *head;
int N;
while(1)
{
printf("input 'D' to delete a node from the link,\n'I'to insert a node to the link,\n'F'to find the node from the link,\n'L'to show the length of the link,\n'R'to reaval the node of the link,\n'C'to creat a node,\n'P'to show the link,\n,'X' to make the link from small one to bigger one.\n'E' to exit \n");
scanf("%1s",&op);
switch (op)
{
case 'C':
head=CreatLink();
break;
case 'D':
printf("please input the node you want to delete:");
scanf("%d",&N);
head=DelNode(head,N);
DispLink(head);
break;
case 'I':
head=InsNode(head);
DispLink(head);
break;
case 'F':printf("please input the number you want to find:");
scanf("%d",&N);
FinNode(head,N);
break;
case 'L':
LonNode(head);
break;
case 'R':
head=ReNode(head);
DispLink(head);
break;
case 'P':
DispLink(head);
break;
case 'X':
head=Paixu(head);
DispLink(head);
break;
case 'E':
exit(0);
default:
printf("you have enter the wrong letter!\n");
} }
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -