?? 鏈表遍歷.txt
字號(hào):
#include<stdlib.h>
#include<stdio.h>
struct LNode{
char elem;
struct LNode* next;
}*l,*p,*v;
void Sequence(struct LNode * l, int n)
{
int i;char swap,*e,*f;
for(i=1;i<=n-1;i++)
{p=l->next;
while(p->next!=l)
{ if(p->elem>p->next->elem)
{e=&p->elem;
f=&p->next->elem;
swap=*e;
*e=*f;
*f=swap;
}
p=p->next;
}
}
return;
}/*排列順序(遞增)函數(shù)定義*/
void Print(struct LNode * l, int n)
{
int i;
p=l->next;
for(i=1;i<=n;i++)
{
printf("%c\t",p->elem);
p=p->next;
}
printf("\n");
return;
}/*打印函數(shù)定義*/
void Locate(struct LNode * l, int n,int m)
{
int i;
if(m>n) { printf("FALSE!\t");return; }
else { p=l;
for(i=1;i<=m;i++)
{p=p->next;}
printf("The elem is:%c\n",p->elem);
}
return;
}/*查找函數(shù)定義*/
void LocateLNode(struct LNode * l, int n,char m)
{
int i;
p=l;
for(i=1;i<=n;i++)
{p=p->next; if(p->elem==m) {printf("TRUE!\n");return;}}
if(i>n) printf("FALSE!\n");
return;
}/*查找已知字母匹配首結(jié)點(diǎn)函數(shù)定義*/
void Insert(struct LNode * l, int n,char m)
{
v=(struct LNode *)malloc(sizeof(struct LNode));
v->next=l->next;
l->next=v;
v->elem=m;
n=n+1;
Sequence(l,n);
Print(l,n);
return;
}/*插入函數(shù)定義*/
void Delete(struct LNode * l, int n,int m)
{
int i;
p=l;
for(i=1;i<m;i++)
{p=p->next;}
p->next=p->next->next;
n=n-1;
printf("The new list is:");
Print(l,n);
return;}/*刪除函數(shù)定義*/
void Length(int n)
{
int i;int length=0;
for(i=1;i<=n+1;i++)
{length=length+sizeof(struct LNode);}
printf("The length of the list is:%d",length);
return;}/*求表長(zhǎng)函數(shù)定義*/
main() /*主函數(shù)*/
{
void Print(struct LNode * l, int n);
void Locate(struct LNode * l, int n,int m);
void LocateLNode(struct LNode * l, int n,char m);
void Insert(struct LNode * l, int n,char m);
void Delete(struct LNode * l, int n,int m);
void Length(int n);
int i,a,k,n;char c,s;
/*建立頭結(jié)點(diǎn)*/
l=(struct LNode *)malloc(sizeof(struct LNode));
l->next=NULL;
printf("Input the total of the elems:");
scanf("%d",&n);getchar();
/*建立鏈表并輸入元素*/
if(n<=15) {
for(i=n;i>0;i--)
{
v=(struct LNode *)malloc(sizeof(struct LNode));
v->next=l->next;l->next=v;}
p=l;
while(p->next!=NULL) p=p->next;
p->next=l;
printf("Input elems:");
p=l->next;
for(i=1;i<=n;i++)
{
scanf("%c",&p->elem);getchar();
p=p->next;
}
/*排序并打印鏈表*/
Sequence(l,n);
printf("The orignal list is:");
Print(l,n);
/*查找第i個(gè)元素*/
printf("Input which LNode you want to locate(Input number):");
scanf("%d",&a);getchar();
Locate(l,n,a);
/*查找與已知字符相同的第一個(gè)結(jié)點(diǎn)*/
printf("Input which char you want to locate to see ");
printf("if there is the same elem of the LNode in the list(Input letter):");
scanf("%c",&c);getchar();
LocateLNode(l,n,c);
/*插入已知字符的結(jié)點(diǎn)*/
printf("Input the elem you want to insert:");
scanf("%c",&s);getchar();
Insert(l,n,s);
n=n+1;
/*刪除第i個(gè)結(jié)點(diǎn)*/
printf("Input which one you want to delete:");
scanf("%d",&k);
if(k<1||k>n)printf("ERROR!");
else{Delete(l,n,k);}
n=n-1;
/*計(jì)算鏈表長(zhǎng)度*/
Length(n);
}
else printf("ERROR!");
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -