先将26个字母输入链表,再从链表中取出,取出指针怎么指向?
#include "stdio.h"
#include "malloc.h"
struct node
{
int date;
struct node *next;
};
main()
{
struct node *p;
char c;
for(c=122;c>=98;c--)
{
p = (struct node *)malloc(sizeof(struct node));
printf("%d\n",sizeof(struct node));
p->date=c;
p=p->next;
}
p = (struct node *)malloc(sizeof(struct node));
p->date='a';
p->next=NULL;
for(c=122;c>=97;c--)
{
printf("%c",p->date);
p--;
}
}
----------------解决方案--------------------------------------------------------
#include <stdio.h>
#include <malloc.h>
struct node
{
int date;
struct node *next;
};
int main()
{
struct node *pHead, *pCur;
char c;
pHead = (struct node *)malloc(sizeof(struct node));
pCur = pHead;
for(c = 'z'; c >= 'b'; c--)
{
printf("%d\n",sizeof(struct node));
pCur->date = c;
pCur->next = (struct node *)malloc(sizeof(struct node));
pCur = pCur->next;
}
pCur->date = 'a';
pCur->next = NULL;
for(pCur = pHead; pCur != NULL; pCur = pCur->next)
printf("%c",pCur->date);
return 0;
}
给你改了一下代码,自己琢磨一下,看来你根本就没理解链表,在纸上画画吧
另外,
1. 对于库头文件,应该是尖括号形式,如<stdio.h>,只有自己定义的头文件才是双引号形式,如"myheader.h"
2. main函数的返回值类型是int
----------------解决方案--------------------------------------------------------
感谢tyc611!
我又学到的一招。
----------------解决方案--------------------------------------------------------
请问,可以让链表逆序输出吗?
----------------解决方案--------------------------------------------------------