郁闷啊,怎么才能学好链表

you_meng 2005-05-03 11:32:18
怎样用字符串代替字符呢?就是把他改成字符串型
一直做不出来~郁闷ing
#include<stdio.h>
#include<stdlib.h>
#include "1.h"
void createstack(struct intncode **headp)
{
char n;
struct intncode *p;
printf("input numbers end of 0:\n");
scanf("%c",&n);
while(n!='0')
{
p=(struct intncode *)malloc(sizeof(struct intncode));
p->data=n;
p->next=*headp;
*headp=p;
scanf("%c",&n);
}
}
void sort(void)
{
int d=0;
struct intncode *head,*p;
head=NULL;
createstack(&head);
p=head;
while(p!=NULL)
{
printf("%c",p->data);
p=p->next;
d++;
}
printf("\n");
printf("%d\n",d-1);
}
void main(void)
{
sort();
}
...全文
105 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhousqy 2005-05-03
  • 打赏
  • 举报
回复
上面贴错了哈
--------------
#include<stdio.h>
#include<stdlib.h>

struct intncode {
int data;
struct intncode *next;
};
void createstack(struct intncode **headp)
{
char n;
struct intncode *p;
printf("input numbers end of 0:\n");
scanf("%c",&n);
while(n!='0')
{
p=(struct intncode *)malloc(sizeof(struct intncode));
p->data=n;
p->next=*headp;
*headp=p;
scanf("%c",&n);
}
}
void sort(void)
{
int d=0;
struct intncode *head,*p;
head=NULL;
createstack(&head);
p=head;
while(p!=NULL)
{
printf("%c",p->data);
p=p->next;
d++;
}
printf("\n");
printf("%d\n",d);
}
int main(void)
{
sort();

return 0;
}
zhousqy 2005-05-03
  • 打赏
  • 举报
回复
你的没什么问题啊

#include<stdio.h>
#include<stdlib.h>
#include "1.h"
void createstack(struct intncode **headp)
{
char n;
struct intncode *p;
printf("input numbers end of 0:\n");
scanf("%c",&n);
while(n!='0')
{
p=(struct intncode *)malloc(sizeof(struct intncode));
p->data=n;
p->next=*headp;
*headp=p;
scanf("%c",&n);
}
}
void sort(void)
{
int d=0;
struct intncode *head,*p;
head=NULL;
createstack(&head);
p=head;
while(p!=NULL)
{
printf("%c",p->data);
p=p->next;
d++;
}
printf("\n");
printf("%d\n",d-1);
}
void main(void)
{
sort();
}
astrophor 2005-05-03
  • 打赏
  • 举报
回复
最好还释放内存:

#include<stdio.h>
#include<stdlib.h>
#include <string.h>
//#include "1.h"
const int MAX=20;
const char end[]={"0"};
struct intncode
{
char * data;
intncode * next;
};
void createstack(struct intncode **headp)
{
char n[20];
struct intncode *p;
printf("input numbers end of 0:\n");
scanf("%s",n);
while(strcmp(n,end))
{
p=(struct intncode *)malloc(sizeof(struct intncode));
p->data=new char [MAX];
strcpy(p->data,n);
p->next=*headp;
*headp=p;
scanf("%s",n);
}
}

void del(struct intncode **head)
{
struct intncode *p;
p=*head;
while(p)
{
*head=(*head)->next;
delete [] p->data;
delete p;
p=*head;
}
}

void sort(void)
{
int d=0;
struct intncode *head,*p;
head=NULL;
createstack(&head);
p=head;
while(p!=NULL)
{
printf(" %s ",p->data);
p=p->next;
d++;
}
printf("\n");
printf("%d\n",d-1);
del(&head);
}

void main(void)
{
sort();
}
astrophor 2005-05-03
  • 打赏
  • 举报
回复
#include<stdio.h>
#include<stdlib.h>
#include <string.h>
//#include "1.h"
const int MAX=20;
const char end[]={"0"};
struct intncode
{
char * data;
intncode * next;
};
void createstack(struct intncode **headp)
{
char n[20];
struct intncode *p;
printf("input numbers end of 0:\n");
scanf("%s",n);
while(strcmp(n,end))
{
p=(struct intncode *)malloc(sizeof(struct intncode));
p->data=new char [MAX];
strcpy(p->data,n);
p->next=*headp;
*headp=p;
scanf("%s",n);
}
}
void sort(void)
{
int d=0;
struct intncode *head,*p;
head=NULL;
createstack(&head);
p=head;
while(p!=NULL)
{
printf(" %s ",p->data);
p=p->next;
d++;
}
printf("\n");
printf("%d\n",d-1);
}

void main(void)
{
sort();
}
看合不合你意
you_meng 2005-05-03
  • 打赏
  • 举报
回复
谢谢阿!各位的都很好!我们自己老师的该死的要求,没办法,就非要让我用字符串
xuzheng318 2005-05-03
  • 打赏
  • 举报
回复
#include<stdio.h>
#include<stdlib.h>
#include <string.h>
//#include "1.h"
const int MAX=20;
const char end[]={"0"};
struct intncode
{
char * data;
intncode * next;
};
void createstack(struct intncode **headp)
{
char n[20];
struct intncode *p;
printf("input numbers end of 0:\n");
scanf("%s",n);
while(strcmp(n,end))
{
p=(struct intncode *)malloc(sizeof(struct intncode));
p->data=new char [MAX];
strcpy(p->data,n);
p->next=*headp;
*headp=p;
scanf("%s",n);
}
}

void del(struct intncode **head)
{
struct intncode *p;
p=*head;
while(p)
{
*head=(*head)->next;
delete [] p->data;
delete p;
p=*head;
}
}

void sort(void)
{
int d=0;
struct intncode *head,*p;
head=NULL;
createstack(&head);
p=head;
while(p!=NULL)
{
printf(" %s ",p->data);
p=p->next;
d++;
}
printf("\n");
printf("%d\n",d-1);
del(&head);
}

void main(void)
{
sort();
}

70,032

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧