zoukankan      html  css  js  c++  java
  • C语言:将字符串中的字符逆序输出,但不改变字符串中的内容。-在main函数中将多次调用fun函数,每调用一次,输出链表尾部结点中的数据,并释放该结点,使链表缩短。

    //将字符串中的字符逆序输出,但不改变字符串中的内容。

     1 #include <stdio.h>
     2 
     3 /************found************/
     4 void fun (char *a)
     5 {  if ( *a )
     6    {  fun(a+1) ;//使用递归进行数组的逆序输出。
     7 /************found************/
     8       printf("%c",*a) ;
     9    }
    10 }
    11 
    12 void main( )
    13 {  char s[10]="abcd";
    14    printf("处理前字符串=%s
    处理后字符串=", s);
    15    fun(s); printf("
    ") ;
    16 }

    //已经建立了一个带头结点的单向链表,在main函数中将多次调用fun函数,每调用一次,输出链表尾部结点中的数据,并释放该结点,使链表缩短。

     1 #include    <stdio.h>
     2 #include    <stdlib.h>
     3 #define    N    8
     4 typedef  struct list//定义一个结构体
     5 {  int  data;
     6    struct list  *next;
     7 } SLIST;
     8 
     9 void fun( SLIST  *p)
    10 {  SLIST  *t, *s;
    11    t=p->next;    s=p;
    12    while(t->next != NULL)
    13    {  s=t;
    14 /**********found**********/
    15       t=t->next;
    16    }
    17 /**********found**********/
    18    printf(" %d ",*t);//或者t->data
    19    s->next=NULL;
    20 /**********found**********/
    21    free(t);//释放
    22 }
    23 SLIST *creatlist(int  *a)
    24 {  SLIST  *h,*p,*q;      int  i;
    25    h=p=(SLIST *)malloc(sizeof(SLIST));
    26    for(i=0; i<N; i++)
    27    {  q=(SLIST *)malloc(sizeof(SLIST));
    28       q->data=a[i];  p->next=q;  p=q;
    29    }
    30    p->next=0;
    31    return  h;
    32 }
    33 void outlist(SLIST  *h)
    34 {  SLIST  *p;
    35    p=h->next;
    36    if (p==NULL)  printf("
    The list is NULL!
    ");
    37    else
    38    {  printf("
    Head");
    39       do { printf("->%d",p->data);  p=p->next;  } while(p!=NULL);
    40       printf("->End
    ");
    41    }
    42 }
    43 void main()
    44 {  SLIST  *head;
    45    int  a[N]={11,12,15,18,19,22,25,29};
    46    head=creatlist(a);//创建头结点
    47    printf("
    Output from head:
    ");  outlist(head);
    48    printf("
    Output from tail: 
    ");
    49    while (head->next != NULL){
    50      fun(head);
    51      printf("
    
    ");
    52      printf("
    Output from head again :
    ");  outlist(head);
    53    }
    54 }
  • 相关阅读:
    小程序与VUE请求后台接口,传数组
    VUE中使用Echarts图表
    封装一个VUE时间线组件
    Git的使用方法及IDEA与Git的集成(二)
    Git中拥有不同的仓库的不同账户
    IDEA无法集成Git问题
    IDEA之debug学习
    MyBatis 学习(三)
    MyBatis 学习(二)
    MyBatis 学习(一)
  • 原文地址:https://www.cnblogs.com/ming-4/p/10511687.html
Copyright © 2011-2022 走看看