单链表的创建与遍历--C

基本结构声明

struct node{
    int data;       //数据域
    struct node *next;//指针域
};

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

struct node{//链表结点 
	int data;//数据域 
	struct node *next;//指针域 
};
typedef struct node Node;
int main(void){
	Node *head,*p,*q;//a
	p=(Node*)malloc(sizeof(Node));//第一个结点 
	p->data=20;
	head=p;//头指针指向第一个结点 
	
	for(int i=1;i<=3;i++){
		q=(Node*)malloc(sizeof(Node));//q指向新申请的结点 
		q->data=(i+2)*10;//新结点数据域赋值 
		p->next=q;//新节点链接到表尾 
		p=q;//p指向尾结点 
	}
	
	p->next=NULL;//最后的尾结点的指针域要为空 
	
	printf("%#x\n",head);
	
	for(Node *p=head;p!=NULL;p=p->next){//遍历链表,然后就可以输出每个结点的地址 
		printf("%#x\t",p);
	}
	printf("\n");
	for(Node *p=head;p!=NULL;p=p->next){
		printf("%d  %#x   ",p->data,p->next);
	}
}

结果

相关推荐

  1. c 追加,查询,,删除

    2024-07-18 10:52:03       29 阅读
  2. 循环创建

    2024-07-18 10:52:03       57 阅读
  3. 数据结构-邻接矩阵创建

    2024-07-18 10:52:03       50 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-18 10:52:03       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-18 10:52:03       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-18 10:52:03       58 阅读
  4. Python语言-面向对象

    2024-07-18 10:52:03       69 阅读

热门阅读

  1. Spring Boot集成ShardingSphere详解

    2024-07-18 10:52:03       21 阅读
  2. 石油与化工行业的工业互联网平台革新之路

    2024-07-18 10:52:03       23 阅读
  3. 10 个c++ cuda 编程例子

    2024-07-18 10:52:03       23 阅读
  4. centos 在线方式安装Node.js 20.15.1 版本(2024最新)

    2024-07-18 10:52:03       23 阅读
  5. flutter app 技术需求规划 设计

    2024-07-18 10:52:03       25 阅读
  6. 库卡机器人示教器 KPC2 00107-264 KPC200.107-264

    2024-07-18 10:52:03       23 阅读
  7. Redis数据结构-跳跃表 skiplist

    2024-07-18 10:52:03       18 阅读
  8. flutter 版本自动更新调研

    2024-07-18 10:52:03       20 阅读
  9. python 结合mitmproxy 抓取指定接口数据

    2024-07-18 10:52:03       23 阅读