链表

简单链表的运用

struct st{
int n;
struct st *next;
};
int main(void){
struct st a[3],*p;
a[0].n=5;
a[0].next=&a[1];
a[1].n=7;
a[1].next=&a[2];
a[2].n=9;
a[2].next='\0';
p=&a[0];
printf("%d",p->n);
printf("%d",p->n++);
printf("%d",p->next->n);
printf("%d",++p->n);
return 0;
}

链表的进阶用法

#define NULL 0
#define LEN sizeof(struct stu)
struct stu
{
	int num;
	char name[10];
	struct stu *next;
};
struct stu *create()
{
	struct stu *head = NULL,*new,*tail,*p;
	int count = 0;
	while(count<5)
	{
		new = (struct stu * ) malloc(LEN);
		printf("input Number and Name \n");
		scanf("%d%s",&new->num,new->name);
		if(new->num==0)
		{
			free(new);
			break;
		}
		else
		if(count==0)
		{
			head = new; tail=new;
		}
		else
		{
			tail->next=new;   //自左向右结合
			tail=new;
		}
		count++;
	}
	tail->next=NULL;
	return(head);
}

void print(struct stu *head)
{
	struct stu *p;
	p=head;
	if(head==NULL)
	printf("list is empty\n");
	else
	while(p!=NULL)
	{
		printf("%d %s\n",p->num,p->name);
		p=p->next;
	}
}

void main()
{
	struct stu *head;
	head = create();
	print(head);
}
赞赏