⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 链表遍历.txt

📁 该程序实现链表的遍历功能
💻 TXT
字号:
#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;
}/*排列顺序(递增)函数定义*/


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;
}/*打印函数定义*/

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;
}/*查找函数定义*/

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;
}/*查找已知字母匹配首结点函数定义*/

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;
}/*插入函数定义*/

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;}/*删除函数定义*/

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;}/*求表长函数定义*/

main()   /*主函数*/
{
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;
/*建立头结点*/
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个元素*/
printf("Input which LNode you want to locate(Input number):");
scanf("%d",&a);getchar();
Locate(l,n,a);
/*查找与已知字符相同的第一个结点*/
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);
/*插入已知字符的结点*/
printf("Input the elem you want to insert:");
scanf("%c",&s);getchar();
Insert(l,n,s);
n=n+1;
/*删除第i个结点*/
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;
/*计算链表长度*/
Length(n);
}
else printf("ERROR!");
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -