链表问题

C语言 码拜 8年前 (2016-04-25) 936次浏览
倒序输出,只要写函数就可以啦
链表问题
#include <stdio.h>
#include <stdlib.h>
struct node       //结点数据类型
{
char data;
struct node *next;
};
void traverse(struct node* head);
void destroy(struct node* head);
struct node* headinsert(struct node* head);//该函数按照头插法,创建4个结点的动态链表,并返回第1个结点的地址
int main()
{
struct node *head=NULL;
head=headinsert(NULL);   //用头插法创建链表
//遍历链表
traverse(head);
//释放资源,销毁链表中全部结点
destroy(head);
}
解决方案

40

#include <stdio.h>
#include <stdlib.h>
struct node       //结点数据类型
{
	char data;
	struct node *next; 
};
void traverse(struct node* head);
void destroy(struct node* head);
struct node* headinsert();//该函数按照头插法,创建4个结点的动态链表,并返回第1个结点的地址
int main()
{
	struct node *head=NULL;
	head=headinsert();   //用头插法创建链表
	//遍历链表
	traverse(head);
	//释放资源,销毁链表中全部结点
	destroy(head);
}
void traverse(struct node* head)
{
	struct node *p;
	p= head;
	while(p)
	{
		printf("%c ", p->data);
		p = p->next;
	}
} 
void destroy(struct node* head)
{
	struct node *p;
	while(head)
	{
		p = head;
		head = head ->next;
		free(p);
	}
}
struct node* headinsert()
{
	struct node *p, *head = NULL;
	int n=0;
	char buffer[64] = {0};
	scanf("%s", buffer);
	while(buffer[n] != "\0")
	{
		p = (struct node *) malloc (sizeof(struct node));
		p->data = buffer[n];
		p->next = head;
		head = p;
		n++;
	}
	return head;
}

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明链表问题
喜欢 (0)
[1034331897@qq.com]
分享 (0)