习题11-7 奇数值结点链表
题目要求
本题要求实现两个函数,分别将读入的数据存储为单链表、将链表中奇数值的结点重新组成一个新的链表。链表结点定义如下:
struct ListNode {
int data;
ListNode *next;
};
函数接口定义
struct ListNode *readlist();
struct ListNode *getodd( struct ListNode **L );
函数readlist()从标准输入读入一系列正整数,按照读入顺序建立单链表。当读到−1时表示输入结束,函数应返回指向单链表头结点的指针。
函数getodd()将单链表L中奇数值的结点分离出来,重新组成一个新的链表。返回指向新链表头结点的指针,同时将L中存储的地址改为删除了奇数值结点后的链表的头结点地址(所以要传入L的指针)。
裁判测试程序样例
#include <stdio.h>
#include <stdlib.h>
struct ListNode {
int data;
struct ListNode *next;
};
struct ListNode *readlist();
struct ListNode *getodd( struct ListNode **L );
void printlist( struct ListNode *L )
{
struct ListNode *p = L;
while (p) {
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
int main()
{
struct ListNode *L, *Odd;
L = readlist();
Odd = getodd(&L);
printlist(Odd);
printlist(L);
return 0;
}
/* 你的代码将被嵌在这里 */
输入样例
1 2 2 3 4 5 6 7 -1
输出样例
1 3 5 7
2 2 4 6
嵌入代码
struct ListNode* CreatListNode(int x)
{
struct ListNode* NewNode = (struct ListNode*)malloc(sizeof(struct ListNode));
if (NewNode == NULL)
{
printf("开辟空间失败\n");
exit(-1);
}
NewNode->data = x;
NewNode->next = NULL;
return NewNode;
}
struct ListNode *readlist()
{
int x = 0;
scanf("%d", &x);
struct ListNode* ListHead = CreatListNode(x);
struct ListNode* tail = ListHead;
while (tail)
{
scanf("%d", &x);
if (x == -1)
{
tail->next = NULL;
}
else
{
tail->next = CreatListNode(x);
}
tail = tail->next;
}
return ListHead;
}
struct ListNode* getodd(struct ListNode** L)
{
struct ListNode* prev = NULL;
struct ListNode* cur = *L;
struct ListNode* next = cur->next;
struct ListNode* OddHead = CreatListNode(0);
struct ListNode* OddCur = OddHead;
while (cur)
{
if ((cur->data) % 2 == 0)
{
prev = cur;
cur = next;
}
else
{
while (next && (next->data) % 2 != 0)
{
next = next->next;
}
if (prev)
prev->next = next;
else
*L = next;
while (cur != next)
{
OddCur->next = cur;
OddCur = cur;
cur = cur->next;
OddCur->next = NULL;
}
}
if (next)
{
next = next->next;
}
}
return (OddHead->next);
}
##################################################################################
习题11-8 单链表结点删除
题目要求
本题要求实现两个函数,分别将读入的数据存储为单链表、将链表中所有存储了某给定值的结点删除。链表结点定义如下:
struct ListNode {
int data;
ListNode *next;
};
函数接口定义
struct ListNode *readlist();
struct ListNode *deletem( struct ListNode *L, int m );
函数readlist()从标准输入读入一系列正整数,按照读入顺序建立单链表。当读到−1时表示输入结束,函数应返回指向单链表头结点的指针。
函数deletem()将单链表L中所有存储了m的结点删除。返回指向结果链表头结点的指针。
裁判测试程序样例
#include <stdio.h>
#include <stdlib.h>
struct ListNode {
int data;
struct ListNode *next;
};
struct ListNode *readlist();
struct ListNode *deletem( struct ListNode *L, int m );
void printlist( struct ListNode *L )
{
struct ListNode *p = L;
while (p) {
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
int main()
{
int m;
struct ListNode *L = readlist();
scanf("%d", &m);
L = deletem(L, m);
printlist(L);
return 0;
}
/* 你的代码将被嵌在这里 */
输入样例
10 11 10 12 10 -1
10
输出样例
11 12
嵌入代码
// 申请一个struct ListNode*的结点
struct ListNode* CreatSListNode(int x)
{
struct ListNode* NewNode = (struct ListNode*)malloc(sizeof(struct ListNode));
if (NewNode == NULL)
{
printf("申请节点失败\n");
exit(-1);
}
NewNode->data = x;
NewNode->next = NULL;
return NewNode;
}
//建立单链表
struct ListNode* readlist()
{
int x = 0;
scanf("%d", &x);
struct ListNode* Head = CreatSListNode(x);
struct ListNode* tail = Head;
while (tail)
{
scanf("%d", &x);
if (x == -1)
{
tail->next = NULL;
}
else
{
tail->next = CreatSListNode(x);
}
tail = tail->next;
}
return Head;
}
//3、删除存储了m的结点
struct ListNode *deletem(struct ListNode *L, int m)
{
struct ListNode* prev = NULL;
struct ListNode* cur = L;
struct ListNode* next = cur->next;
while (cur)
{
if (cur->data != m)
{
prev = cur;
cur = next;
}
else
{
while ((next) && (next->data) == m)
{
next = next->next;
}
if (prev)
prev->next = next;
else
L = next;
while (cur != next)
{
struct ListNode* del = cur;
cur = cur->next;
free(del);
}
}
if (next)
next = cur->next;
}
return L;
}
总结
这两道题目的思想有一定的相似之处,因此我将这两道题放到一起去写一篇博客
思想
第一步:建立一个单链表
每建立一个节点,都要去动态开辟一个struct ListNode*大小的结构体指针,将从键盘输入的第一个值x作为头元素的data值存储,此后设置一个tail指向head指针的地址,向后进行数据的依次插入,直至遍历到输入的值x为-1时,令tail->next = NULL;
具体细节看代码理解
第二步
在进行这一步时,要考虑到头部,尾部以及中间结点的变化。这是解这两道题的关键。
详情请看我的另一篇文章
牛客网:编程题-删除链表中重复的结点
习题11-7 遍历链表内的元素,将其中结点指向data为奇数的放到另一个数组之中存放
与本道题目不同的是,这个题目的结束条件不是next,而是cur,因为咱们是从头要遍历到尾,将其中结点指向的data为奇数按顺序放到另一个链表之中。
其中放到另一个链表的步骤为
while (cur != next)
{
OddCur->next = cur;
OddCur = cur;
cur = cur->next;
OddCur->next = NULL;
}
习题11-8 根据所输入的值m,遍历链表删除结点指向的data与m相等的结点
与本道题目不同的是,这个题目的结束条件不是next,而是cur,因为咱们是知道要删除什么数据的结点,我们就要从头结点一直遍历到尾结点,直至将对应数据的结点删除完毕后,这道题目才算真正完成。