C语言中字符串实现正序与逆序实例详解
发布时间:2020-12-16 05:07:45 所属栏目:百科 来源:网络整理
导读:C语言中字符串实现逆序实例详解 字符串逆序和正序的实现代码: #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(voi
C语言中字符串实现逆序实例详解 字符串逆序和正序的实现代码: #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("不能分配内存空间!"); 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("不能分配内存空间!"); 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); } /*主函数*/ int main() { stud *head=NULL; head=creat(); print1(head); print2(head); free_stud(head); return 0; } 实现效果图: 感谢阅读,希望能帮助到大家,谢谢大家对本站的支持! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- c# – 如何使用EventHandler动态创建/删除ASP.NET页面的控件
- Unity3D如何使用Sqlite数据库
- objective-c – 类型而不是变量之间的插入字符,用括号括起来
- c# – 使用Linq将数据分区为数组
- Cocos2d-x开发---改变父节点颜色、透明度影响子节点
- iphone – 通过仪器跟踪“发送到解除分配的实例的消息”
- objective-c – 如何在CocoaLibSpotify注销上清除用户名和密
- 数组 – Swift 3.0:在Array或Dictionary扩展中调用global
- Oracle 的 ORA-01653 错误解决办法
- 如何使用swift在iOS 8中插入图像内联UILabel