Skip to content

按值查找**

c
#include<stdio.h>
#include<malloc.h>
typedef struct node                      /*定义结点的存储结构*/
{
    int data;
    struct node *next;
}NODE;

NODE *create()                        /*此函数采用后插入方式建立单链表,并返回一个指向链表表头的指针*/
{
    NODE *head,*q,*p;                 /*定义指针变量*/
    char ch;
    int a;
    head=(NODE*)malloc(sizeof(NODE));  /*申请新的存储空间,建立表头结点*/
    q=head;
    ch='*';
    printf("\nInput the list :");
    while(ch!='?')                          /*"ch"为是否建立新结点的标志,若"ch"为"?"则输入结束*/
    {
	      scanf("%d",&a);                    /*输入新元素*/
	      p=(NODE*)malloc(sizeof(NODE));
        p->data=a;
        q->next=p;
        q=p;
        ch=getchar();                       /*读入输入与否的标志*/
    }
    q->next=NULL;
    return(head);                           /*返回表头指针head*/
}

NODE *locate(NODE *head,int x)            /*在已知链表中查找给定的值x*/
{
    NODE *p;
    p=head->next;
    while((p!=NULL)&&(p->data!=x))       /*未到表尾且未找到给定数据*/
        p=p->next;                       /*指向下一个元素*/
    return(p);
}

main()                                  /*主程序*/
{
   int y;
   NODE *a,*b;
   a=create();
   printf("Input x: ");
   scanf("%d",&y);
   b=locate(a,y);
   if(b!=NULL)
   {
	   printf("find:");
       printf("%5d",b->data);              /*查找成功*/
   }
   else
      printf("error");                     /*查找失败*/
}

按序号查找

c
#include<stdio.h>
#include<malloc.h>
typedef struct node                      /*定义结点的存储结构*/
{
    int data;
    struct node *next;
}NODE;

NODE *create()                        /*此函数采用后插入方式建立单链表,并返回一个指向链表表头的指针*/
{
    NODE *head,*q,*p;                 /*定义指针变量*/
    char ch;
    int a;
    head=(NODE*)malloc(sizeof(NODE));  /*申请新的存储空间,建立表头结点*/
    q=head;
    ch='*';
    printf("\nInput the list :");
    while(ch!='?')                      /*"ch"为是否建立新结点的标志,若"ch"为"?"则输入结束*/
    {
	    scanf("%d",&a);                /*输入新元素*/
	    p=(NODE*)malloc(sizeof(NODE));
        p->data=a;
        q->next=p;
        q=p;
        ch=getchar();                    /*读入输入与否的标志*/
    }
    q->next=NULL;
    return(head);                        /*返回表头指针head*/
}

NODE *find(NODE *head,int i)           /*在已知链表中查找给定的值i*/
{
    int j=1;
    NODE *p;
    p=head->next;
    while((p!=NULL)&&(j<i))           /*未到表尾且未找到给定数据*/
    {
        p=p->next;                    /*指向下一个元素*/
        j++;
    }
    return(p);
}

main()                             /*主程序*/
{
   int i;
   NODE *a,*b;
   a=create();
   printf("Input i: ");
   scanf("%d",&i);
   b=find(a,i);
   if(b!=NULL)
   {
       printf("find:");
       printf("%5d",b->data);        /*查找成功*/
   }
   else
      printf("error");               /*查找失败*/
}

Layout Switch

Adjust the layout style of VitePress to adapt to different reading needs and screens.

Expand all
The sidebar and content area occupy the entire width of the screen.
Expand sidebar with adjustable values
Expand sidebar width and add a new slider for user to choose and customize their desired width of the maximum width of sidebar can go, but the content area width will remain the same.
Expand all with adjustable values
Expand sidebar width and add a new slider for user to choose and customize their desired width of the maximum width of sidebar can go, but the content area width will remain the same.
Original width
The original layout width of VitePress

Page Layout Max Width

Adjust the exact value of the page width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the page layout
A ranged slider for user to choose and customize their desired width of the maximum width of the page layout can go.

Content Layout Max Width

Adjust the exact value of the document content width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the content layout
A ranged slider for user to choose and customize their desired width of the maximum width of the content layout can go.

Spotlight

Highlight the line where the mouse is currently hovering in the content to optimize for users who may have reading and focusing difficulties.

ONOn
Turn on Spotlight.
OFFOff
Turn off Spotlight.