`

Review_singlelinkedlist

阅读更多

时间长了,不写手生,复习一下单链表。

 

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

typedef struct _node
{
	int mydata;
	struct _node *next;
}node;

node *Create()
{
	int i,len;
	node *h,*p,*q;

	printf("定制链表长度:");
	scanf("%d",&len);
	
	if(len<0)
	{
		printf("输入有误!\n");
		exit(1);
	}
	
	h=(node*)malloc(sizeof(node));
	if(h!=NULL)
	{
		p=h;
	}

	for(i=0;i<len;i++)
	{
		q=(node*)malloc(sizeof(node));
		if(q!=NULL)
		{
			printf("输入节点数据:");
			scanf("%d",&q->mydata);
			p->next=q;
			q->next=NULL;
			p=q;
		}
	}

	return h;
}

void Print(node *head)
{
	node *pointer=head->next;
	while(pointer!=NULL)
	{
		printf("%d\n",pointer->mydata);
		pointer=pointer->next;
	}
}

int main()
{
	node *s=Create();
	Print(s);

	exit(0);
}
 

 

随意复制,随意粘贴。

 

0
0
分享到:
评论
发表评论

文章已被作者锁定,不允许评论。

相关推荐

Global site tag (gtag.js) - Google Analytics