当前位置: 移动技术网 > 网络运营>网络>协议 > 单链表

单链表

2020年07月29日  | 移动技术网网络运营  | 我要评论
今天学了学单链表,算是弄懂了个大概有三个文件如下lianbiao.h#ifndef _LIANBIAO_H_#define _LIANBIAO_H_typedef int eletype; struct node{eletype data;struct node *next;};struct node *creat_linklist();void print_list(struct node* p);#endif lianbiao1.c#include"lianbiao

今天学了学单链表,算是弄懂了个大概
有三个文件如下
lianbiao.h

#ifndef _LIANBIAO_H_
#define _LIANBIAO_H_
typedef int eletype; 
struct node
{
	eletype data;
	struct node *next;
};
struct node *creat_linklist();
void print_list(struct node* p);
#endif 

lianbiao1.c

#include"lianbiao1.h"
#include<stdio.h>
#include<stdlib.h>
/*
creat_linklist:创建一个单链表
*/
struct node *creat_linklist(void)
{
	struct node *first=NULL;      
	struct node *last=NULL;
	struct node *p=NULL;
	int d,count=0;       
	while(1)
	{
		scanf("%d",&d);
		if(d==0)              //输入0结束输入
			break;
		p=(struct node*)malloc(sizeof(*p));  //节点分配
		count++;
		if(NULL==first)			//如果是空表(既没有节点)
		{
			first=p;		
			last=p;
			
			
		}
		else					
		{
			
			last->next=p;
			//p->pre=last;
			last=p;
		}
		
		p->data=d;				//赋值
	}
	printf("%d个节点\n",count);
	return first;				//返回第一个节点的地址
	
	
}
/*
	printf_list:逐个输出链表里的值
	@p:传入的链表的地址
*/
void print_list(struct node* p)		
{
	while(p)
	{
		printf("%d ",p->data);
		p=p->next;
	}
	printf("\n");
}

main.c

#include"lianbiao1.h"
int main(int argc,char *argv[])
{
	struct node *k;
	k=creat_linklist();
	print_list(k);
}

本文地址:https://blog.csdn.net/qq_43701013/article/details/107645016

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网