• 链表的各种


    Description

    这个链表共有五种操作

    Input

    loc 为正数,x为一整型数, 
    "insert loc x"表示将整数x插入链表作为链表第loc个数据 
    "erase loc"表示删除链表的第loc个数据(测试数据保证loc小于链表的结点总数) 
    "output"表示输出链表所有的数据,数据间用空格隔开 
    "find x"表示在链表中查找值为x的数据,若查找到则输出"hava found",没找到输出"not found"
    "end"表示程序结束

    Output

    按要求输出

    Sample Input

    insert 1 2 insert 2 6 insert 3 5 find 6 output erase 2 find 6 output insert 3 10 output insert 2 11 output end

    Sample Output

    have found
    2 6 5
    not found
    2 5
    2 5 10
    2 11 5 10
     
     
    View Code
      1 #include<stdio.h>
    2 #include<string.h>
    3 #include<stdlib.h>
    4 typedef struct node
    5 {
    6 int data;
    7 struct node *next;
    8 }pao;
    9 pao *put_linklist( pao *H )
    10 {
    11 pao *r;
    12 r = H;
    13 while( r -> next -> next != NULL )
    14 {
    15 printf( "%d ",r -> next -> data );
    16 r = r -> next;
    17 }
    18 printf( "%d ", r -> next -> data );
    19 puts( "" );
    20
    21 }
    22 pao *get_linklist( pao *H, int i )
    23 {
    24 pao *p = H;
    25 int j = 0;
    26 while( p -> next != NULL&& j<i )
    27 {
    28 p = p->next;
    29 j++;
    30 }
    31 if( j == i )
    32 return p;
    33 else
    34 return NULL;
    35 }
    36 pao *insert( pao *H )
    37 {
    38 pao *p, *s;
    39 int i, x;
    40 scanf( "%d%d", &i, &x );
    41 p = get_linklist( H, i-1 );
    42 if( p == NULL )
    43 {
    44 H -> data = x;
    45 }
    46 else
    47 {
    48 s = (pao *)malloc(sizeof(pao));
    49 s->data = x;
    50 s -> next = p -> next;
    51 p -> next = s;
    52 }
    53 }
    54 pao *find( pao *H )
    55 {
    56 int x;
    57 scanf( "%d", &x );
    58 pao *p = H-> next;
    59 while( p!=NULL && p -> data != x )
    60 {
    61 p = p -> next;
    62 }
    63 if( p )
    64 printf( "have found \n" );
    65 else
    66 printf( "not found \n" );
    67 }
    68 pao *del( pao *H )
    69 {
    70 int i;
    71 scanf( "%d", &i );
    72 pao *p, *s;
    73 p = get_linklist( H, i-1 );
    74 if( p != NULL )
    75 {
    76 if( p -> next != NULL )
    77 {
    78 s = p->next;
    79 p->next = s -> next;
    80 free(s);
    81 }
    82 }
    83
    84 }
    85 int main()
    86 {
    87 char str[8];
    88 pao *H;
    89 H = (pao *)malloc(sizeof(pao));
    90 H->next = NULL;
    91 while( scanf( "%s", str ) == 1 )
    92 {
    93 if( strcmp( "insert", str ) == 0 )
    94 insert( H );
    95 else if( strcmp( "output", str )== 0 )
    96 put_linklist( H );
    97 else if( strcmp( "find", str) == 0 )
    98 find( H );
    99 else if( strcmp( "erase", str) == 0 )
    100 del( H );
    101 else if( strcmp( "end", str ) == 0 )
    102 exit(1);
    103 }
    104 }

     
  • 相关阅读:
    JS倒计时执行操作
    美化radio和checkbox样式
    ajax 多个表单值问题,表单序列化加其它表单值
    .net中的路径问题
    Response.Redirect 打开新窗口的两种方法
    GRIDVIEW FINDCONTROL的使用
    如何验证gridview控件的编辑行?如何获得gridview模板列<ItemTemplate/>中Label值?
    在Repeater的HeaderTemplate和FooterTemplate模板中寻找控件FindControl
    在GridView中使用FindControl(2)
    构造函数(C# 编程指南)
  • 原文地址:https://www.cnblogs.com/zsj576637357/p/2382563.html
Copyright © 2020-2023  润新知