一.温故知新
栈式的存储结构(受限制的存储结构)
特点 : 先进后出
线性表中的特殊存储形式
通过顺序表来实现栈式存储结构
通过链表来实现栈式存储结构
#define STACKSIZE 6
struct node
{
int stack_buf[STACKSIZE];
int stack_ind;
};
队列式的存储结构(受限制的存储结构)
特点 : 先进先出
线性表中的特殊存储形式
通过顺序表来实现队列式的存储结构
通过链表来实现队列式的存储结构
#define QUEUESIZE 6
struct node
{
int queue[QUEUESIZE];
int front;
int rear;
};
早测
快慢指针
struct llist_node
{
int val;
struct llist_node *next;
};
1.反转链表
给你一个无头单向不循环链表的首结点l ,请你反转链表,
并返回反转后的链表首结点
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| struct llist_node *func(struct llist_node *l)
{
struct llist_node *cur = l;//指向当前要反转的结点
struct llist_node *p = NULL;//指向原链表cur之后的位置
struct llist_node *next = NULL;//指向反转链表之后cur之后的位置
while(cur != NULL)//遍历所有结点
{
p = cur->next;
cur->next = next;
next = cur;
cur = p;
}
return next;
}
|
2.链表的中间结点
给定一个头结点为handler的单向不循环链表,返回链表的中间结点
如果有两个中间结点,则返回第二个中间结点。
1
2
3
4
5
6
7
8
9
10
11
12
13
| struct llist_node *fund(struct llist_node *handler)
{
struct llist_node *s = handler;//慢指针
struct llist_node *f = handler;//快指针
while(f != NULL && f->next != NULL)//判断只有一个结点
{
s = s->next;
f = f->next->next;
}
return s;
}
|
===================================
作业
1.把今天讲过的内容完完整整的复习一遍
2.把队列式的存储结构的实现方法再写一遍
3.写个链表,把早测的功能函数写程序实现
===================================