数据结构——图解单链表的初始化、赋值和遍历输出C语言

一、单链表存储结构解释说明

typedef struct node{
	int data;
	struct node *next;
}Node,*Link;

1.typedef:是将结构体struct node重新定义了新的名称Node和*Link。
2.struct node *next:是让成员next指向struct node这样一个类型的指针。如下图
在这里插入图片描述
3.Node:例如Node p=struct node p
4.*Link:例如*Link p=struct node *p,这里的p同上面的next。

二、单链表初始化解释说明

在主函数中先为head申请一个结点的空间,并将其next设置为NULL,表示这是一个空表。

Link head;
	head=(Link)malloc(sizeof(Node));
	//创建了一个单链表,设置头结点head 
	head->next=NULL;

malloc:是动态内存分配函数,malloc(sizeof(Node))是为Node类型申请一块连续的内存块区域,这个的返回值是void*类型(就是这片连续区域的地址),但是我们前面将这片连续的区域指定成了*Link类型,所以此时需要将malloc(sizeof(Node))做强制类型转换,于是就写成了(Link)malloc(sizeof(Node)),并将其赋值给head。

三、单链表的赋值操作

while(ch!='\n')
	{
		p=(Link)malloc(sizeof(Node));
		scanf("%d",&c);
		p->data=c;
		q->next=p;
		p->next=NULL;
		q=p;
		ch=getchar();
	}

解释说明如下图:
在这里插入图片描述

四、单链表的遍历输出操作

while(p!=NULL){
		printf("%d,",p->data);
		p=p->next;
	}

解释说明:
在这里插入图片描述

五、完整代码

#include "stdio.h"
#include "stdlib.h" 
typedef struct node{
	int data;
	struct node *next;
}Node,*Link;
int createLinklist(Link head){
	int c;
	char ch;
	Link p,q;
	q=head;
	while(ch!='\n')
	{
		p=(Link)malloc(sizeof(Node));
		scanf("%d",&c);
		p->data=c;
		q->next=p;
		p->next=NULL;
		q=p;
		ch=getchar();
	}
	q->next=NULL;
	return 1;
}
show(Link head){
	Link p;
	p=(Link)malloc(sizeof(Node));
	p=head->next;
	while(p!=NULL){
		printf("%d,",p->data);
		p=p->next;
	}
}
int main(){
	int n;
	Link head;
	head=(Link)malloc(sizeof(Node));
	//创建了一个单链表,设置头结点head 
	head->next=NULL; 
	printf("创建一个链表\n");
	printf("请输入单链表的元素(用逗号隔开,回车结束):\n");
	createLinklist(head);
	printf("遍历单链表并输出:");
	show(head);
	return 0;
}
  • 26
    点赞
  • 89
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
#include<stdio h> #include<stdlib h> #include<malloc h> #include<string h> struct Node { char name[20]; int score; struct Node next; }; 定义一个结构体 此结构体存储学生姓名 成绩以及指向该结构体的指针 typedef struct Node ListNode; ListNode CreateList int n ; 创建链表函数 返回创建的链表的头指针 void InsertList ListNode h int i char name[] int score ; 插入结点函数 void DeleteList ListNode h int i int n ; 删除结点函数 void PrintList ListNode h ; 打印链表函数 int main { ListNode h; int i 1 n score; char name[20]; while i { printf "1 Create a new list n" ; printf "2 Add a new student"s information n" ; printf "3 Delete a student"s information n" ; printf "4 All student"s information n" ; printf "0 Quit n" ; scanf "%d" &i ; switch i { case 1: printf "Enter the number of students n n " ; scanf "%d" &n ; h CreateList n ; 创建链表 h是头指针 printf "List elements is: n" ; PrintList h ; break; case 2: printf "input the position of insert element:" ; scanf "%d" &i ; if i<1||i>n+1 控制条件 { printf "Error input n" ; break; } else { printf "input name of the student:" ; scanf "%s" name ; printf "input score of the student:" ; scanf "%d" &score ; InsertList h i name score ; printf "List elements is: n" ; PrintList h ; break; } case 3: printf "input the position of delete element:" ; scanf "%d" &i ; DeleteList h i n ; 调用删除结点函数 printf "list element is: n" ; PrintList h ; break; case 4: printf "list element is: n" ; PrintList h ; break; case 0: return; break; default: printf "illegal input " ; } } return 0; } ListNode CreateList int n { ListNode head; 定义头指针 ListNode p pre; int i; head ListNode malloc sizeof ListNode ; 给头结点分配空间 head >next NULL; 头结点的指针域为空 pre head; for i 1;i< n;i++ { printf "input name of the %d student:" i ; p ListNode malloc sizeof ListNode ; 创建一个新的结点 指针p指向它 scanf "%s" &p >name ; 给数据域赋值 printf "input score of the %d student:" i ; scanf "%d" &p >score ; 给数据域赋值 pre >next p; pre p; } p >next NULL; 最后一个结点指针域为空 return head; } void PrintList ListNode h { ListNode p; p h >next; p指向首元结点 while p { printf "%s %d" p >name p >score ; p p >next; 依次遍历 每访问完一个结点p指向下一个结点 printf " n" ; } } void InsertList ListNode h int i char name[] int e { ListNode q p; int j 0; p h; for j 0;j<i 1;j++ p指向第i 1个结点 { p p >next; } q ListNode malloc sizeof ListNode ; 创建个新结点 q指向它 strcpy q >name name ; q >score e; q >next p >next; q指向的结点的指针域指向第i个结点 p >next q; 第i 1个结点指向第i个结点 } void DeleteList ListNode h int i int n { ListNode p q; int j; char name[10]; int score; if i<1||i>n printf "illegal input n" ; else { j 0; p h; for j 0;j<i 1;j++ p指向第i 1个结点 { p p >next; } q p >next; q指向第i个结点 p >next q >next; 第i 1个结点的指针域指向第i+1个 strcpy name q >name ; score q >score; free q ; printf "name %s score %d n" name score ; 把删除的结点的数据域输出 } }">#include<stdio h> #include<stdlib h> #include<malloc h> #include<string h> struct Node { char name[20]; int score; struct Node next; }; 定义一个结构体 此结构体存储学生姓名 成绩以及指向该结构体的指针 typedef struct Node ListNode; ListN [更多]
二叉树的层次遍历可以使用队列来实现。具体步骤如下: 1. 将根节点入队。 2. 当队列不为空时,执行以下操作: - 将队首节点出队,并访问该节点。 - 如果该节点有左子节点,则将左子节点入队。 - 如果该节点有右子节点,则将右子节点入队。 以下是C语言代码实现: ```c #include <stdio.h> #include <stdlib.h> // 二叉树结点的定义 typedef struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; } TreeNode; // 队列结点的定义 typedef struct QueueNode { TreeNode *data; struct QueueNode *next; } QueueNode; // 队列的定义 typedef struct Queue { QueueNode *front; QueueNode *rear; } Queue; // 初始化队列 void initQueue(Queue *q) { q->front = q->rear = NULL; } // 判断队列是否为空 int isEmpty(Queue *q) { return q->front == NULL; } // 入队 void enqueue(Queue *q, TreeNode *data) { QueueNode *newNode = (QueueNode *)malloc(sizeof(QueueNode)); newNode->data = data; newNode->next = NULL; if (isEmpty(q)) { q->front = q->rear = newNode; } else { q->rear->next = newNode; q->rear = newNode; } } // 出队 TreeNode *dequeue(Queue *q) { if (isEmpty(q)) { return NULL; } TreeNode *data = q->front->data; QueueNode *temp = q->front; q->front = q->front->next; free(temp); return data; } // 层次遍历 void levelOrder(TreeNode *root) { if (root == NULL) { return; } Queue q; initQueue(&q); enqueue(&q, root); while (!isEmpty(&q)) { TreeNode *node = dequeue(&q); printf("%d ", node->val); if (node->left != NULL) { enqueue(&q, node->left); } if (node->right != NULL) { enqueue(&q, node->right); } } } // 创建二叉树 TreeNode *createTree() { int val; scanf("%d", &val); if (val == -1) { return NULL; } TreeNode *root = (TreeNode *)malloc(sizeof(TreeNode)); root->val = val; root->left = createTree(); root->right = createTree(); return root; } int main() { TreeNode *root = createTree(); levelOrder(root); return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值