|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 小jy2333 于 2020-11-6 23:11 编辑
这是题目要求:编程序判断一个字符序列是否是回文,要求采用链式队列和链式堆栈。
算法提示:设字符数组str中存放了要判断的字符串。把字符数组中的字符逐个分别存入队列和堆栈,然后逐个出队列和退栈并比较出队列的字符和退栈的字符是否相等,若全部相等则该字符序列是回文,否则就不是回文。
下面是我写的未完成的代码,到了insertq函数关于队列插入的地方卡主了
- #include<stdio.h>
- #include<stdlib.h>
- #include<malloc.h>
- typedef char datatype;
- typedef struct node1 {
- datatype data;
- struct node1 *next;
- } linkstack;
- linkstack *hs;
- typedef struct node2 {
- datatype data;
- struct node2 *next;
- } linkqueue;
- typedef struct {
- linkqueue *front,*rear;
- } lqueue;
- lqueue *hq;
- void push(datatype x) {
- linkstack *s;
- s=malloc(sizeof(linkstack));
- s->data=x;
- s->next=hs;
- hs=s;
- }
- void pop() {
- linkstack *p;
- if(hs==NULL)
- printf("栈空");
- else {
- p=hs;
- hs=hs->next;
- }
- }
- void insertq(datatype x) {
- linkqueue *p;
- p=malloc(sizeof(linkqueue));
- p->data=x;
- p->next=NULL;
- if(hq->rear==NULL) {
- hq->front=p;
- hq->rear=p;
- } else {
- hq->rear->next=p;
- hq->rear=p;
- }
- }
- void deleteq() {
- linkqueue *p;
- if(hq->front==NULL)
- printf("队空");
- else {
- p=hq->front;
- hq->front=p->next;
- if(hq->front==NULL)
- hq->rear=hq->front;
- }
- }
- void print1(head)
- linkstack *head;
- {
- linkstack *p;
- p=head;
- while(p!=NULL) {
- printf("%c ",p->data);
- p=p->next;
- }
- printf("\n");
- }
- int main() {
- char str[]="abcdefrx";
- char m='a';
- int i=0;
- while(*(str+i)) {
- push(str[i]);
- i++;
- }
- print1(hs);
- pop(hs);
- print1(hs);
- insertq(m);
- }
复制代码
运行结果经过测试发现可能就是错在insertq,不明白错在哪里,求好心人帮忙解答一下
|
|