C語言中字符串實現(xiàn)正序與逆序?qū)嵗斀?/h1>
更新時間:2017年07月07日 11:41:55 投稿:lqh
這篇文章主要介紹了C語言中字符串實現(xiàn)倒敘實例詳解的相關資料,需要的朋友可以參考下
C語言中字符串實現(xiàn)逆序?qū)嵗斀?/strong>
字符串逆序和正序的實現(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;
}
實現(xiàn)效果圖:

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關文章
-
ON_COMMAND_RANGE多個按鈕響應一個函數(shù)的解決方法
這篇文章主要介紹了ON_COMMAND_RANGE多個按鈕響應一個函數(shù)的解決方法,需要的朋友可以參考下 2014-07-07
-
Window10下安裝VS2022社區(qū)版的實現(xiàn)步驟(圖文教程)
很多和同學們在接觸c語言的時候都是使用VS,本文主要介紹了Window10下如何安裝VS2022社區(qū)版的實現(xiàn)步驟,具有一定的參考價值,感興趣的可以了解一下 2024-02-02
最新評論
C語言中字符串實現(xiàn)逆序?qū)嵗斀?/strong>
字符串逆序和正序的實現(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;
}
實現(xiàn)效果圖:

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關文章
ON_COMMAND_RANGE多個按鈕響應一個函數(shù)的解決方法
這篇文章主要介紹了ON_COMMAND_RANGE多個按鈕響應一個函數(shù)的解決方法,需要的朋友可以參考下2014-07-07
Window10下安裝VS2022社區(qū)版的實現(xiàn)步驟(圖文教程)
很多和同學們在接觸c語言的時候都是使用VS,本文主要介紹了Window10下如何安裝VS2022社區(qū)版的實現(xiàn)步驟,具有一定的參考價值,感興趣的可以了解一下2024-02-02

