一.温故知新
栈式的存储结构
先进后出
队列式的存储结构
先进先出
无论是顺序表还是链表都可以实现
栈式
struct node
{
int stack[6];
int ind;
};
队列
struct node
{
int queue[6];
int front;
int rear;
};
静态库和动态库
早测
1.链表中倒数第k个结点
给定一个头结点为handler的单向不循环链表
输出该链表中倒数第k个结点
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| struct llist_node *func(struct llist_node *l, int k)
{
struct llist_node *s = l;//慢指针
struct llist_node *f = l;//快指针
while(k--)
{
if(f == NULL)//判断链表是否提前结束
return NULL;
f = f->next;
}
while(f != NULL)
{
s = s->next;
f = f->next;
}
}
|
2.合并两个有序链表
将两个升序的无头单向不循环链表合并为一个新的升序链表并返回
新链表是通过拼接给定的两个链表的所有节点组成的
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
| struct llist_node *func(struct llist_node *l1, struct llist_node *l2)
{
struct llist_node *l = NULL;//指向合并之后的链表
struct llist_node *cur = NULL;
if(l1 == NULL)
return l2;
if(l2 == NULL)
return l1;
if(l1->val < l2->val)
{
l = cur = l1;
l1 = l1->next;
}
else
{
l = cur = l2;
l2 = l2->next;
}
while(l1 != NULL && l2 != NULL)
{
if(l1->val < l2->val)
{
cur->next = l1;
l1 = l1->next;
}
else
{
cur->next = l2;
l2 = l2->next;
}
cur = cur->next;
}
if(l1 != NULL)
cur->next = l1;
if(l2 != NULL)
cur->next = l2;
return l;
}
|
3.简答题
给定一个有头单向不循环链表的头结点handler和要删除的结点p