溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

C語(yǔ)言中字符串實(shí)現(xiàn)正序與逆序?qū)嵗斀?/h1>
發(fā)布時(shí)間:2020-10-25 00:58:40 來(lái)源:腳本之家 閱讀:242 作者:lqh 欄目:編程語(yǔ)言

C語(yǔ)言中字符串實(shí)現(xiàn)逆序?qū)嵗斀?/strong>

字符串逆序和正序的實(shí)現(xiàn)代碼:

#include <stdio.h>

#include <stdlib.h>

#include <conio.h>

#include <malloc.h>

#include <string.h>

/*定義*/

typedef struct node

{

 char c;

 struct node *llink,*rlink;

}stud;

/*建立鏈表*/

stud * creat(void)

{

 stud *p,*h,*s;

 char a;

 if((h=(stud *)malloc(sizeof(stud)))==NULL)

 {

  printf("不能分配內(nèi)存空間!");

  exit(0);

 }

 h->c = 0;

 h->llink=NULL;

 h->rlink=NULL;

 p=h;

 while(1)

 {

a = getchar();

if(a=='\n')

break;

  if((s= (stud *) malloc(sizeof(stud)))==NULL)

  {

   printf("不能分配內(nèi)存空間!");

   exit(0);

  }

  p->rlink=s;

  

  s->c =a;

  s->llink=p;

  s->rlink=NULL;

  p=s;

 }

 h->llink=s;

 p->rlink=h;

 return(h);

}

/*正序*/

void print1(stud *h)

{

 stud *p;

 p=h->rlink;

 printf("字符串(正序):");

 while(p!=h)

 {

  printf("%c",p->c);

  p=p->rlink;

 }

 printf("\n");

}

/*逆序*/

void print2(stud *h)

{

 stud *p;

 p=h->llink;

 printf("字符串(逆序):");

 while(p!=h)

 {

  printf("%c",p->c);

  p=p->llink;

 }

 printf("\n");

}

/*釋放*/

void free_stud(stud *h)

{

 stud *p,*q;

 p=h->llink;

 while(p!=h)

 {

  q=p;

  p=p->llink;

  free(q);

 }

 free(h);

}

/*主函數(shù)*/

int main()

{

 stud *head=NULL;

 head=creat();

 print1(head);

 print2(head);

 free_stud(head);

 return 0;

}

實(shí)現(xiàn)效果圖:

C語(yǔ)言中字符串實(shí)現(xiàn)正序與逆序?qū)嵗斀?></p>
<p>感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!<br />
</p>            </div>
            <div   id=向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI