1. 程式人生 > >線性連結串列的建立與顯示

線性連結串列的建立與顯示

#include<stdio.h>
#include<malloc.h>
typedef int ElemType;
typedef struct Node
{
	ElemType data;
	struct Node*next;
}LNode;
void createLinkList(LNode*head);
void displayLinkList(LNode*head);
int main()
{
	LNode*head;
	head=(LNode*)malloc(sizeof(LNode));
	head->next=NULL;
	createLinkList(head);
	displayLinkList(head);
}
void createLinkList(LNode*head)
{
	LNode*p,*rear=head;
	ElemType x;
	printf("輸入資料元素x,當x=0時退出:");
	scanf("%d",&x);
	while(x!=0)
	{
		p=(LNode*)malloc(sizeof(LNode));
		p->data=x;
		p->next=NULL;
		rear->next=p;
		rear=p;

		scanf("%d",&x);
	}
}
void displayLinkList(LNode*head)
{
	LNode*p=head->next;
	printf("線性連結串列:");
	while(p!=NULL)
	{
		printf("%d ",p->data);
		p=p->next;
	}
	printf("\n");
}