-
2021-12-23 15:21:56
本题要求实现两个函数,分别将读入的数据存储为单链表、将链表中偶数值的结点删除。链表结点定义如下:
struct ListNode { int data; struct ListNode *next; };
函数接口定义:
struct ListNode *createlist(); struct ListNode *deleteeven( struct ListNode *head );
函数
createlist
从标准输入读入一系列正整数,按照读入顺序建立单链表。当读到−1时表示输入结束,函数应返回指向单链表头结点的指针。函数
deleteeven
将单链表head
中偶数值的结点删除,返回结果链表的头指针。裁判测试程序样例:
#include <stdio.h> #include <stdlib.h> struct ListNode { int data; struct ListNode *next; }; struct ListNode *createlist(); struct ListNode *deleteeven( struct ListNode *head ); void printlist( struct ListNode *head ) { struct ListNode *p = head; while (p) { printf("%d ", p->data); p = p->next; } printf("\n"); } int main() { struct ListNode *head; head = createlist(); head = deleteeven(head); printlist(head); return 0; } /* 你的代码将被嵌在这里 */
输入样例:
1 2 2 3 4 5 6 7 -1
结尾无空行
输出样例:
1 3 5 7
结尾无空行
struct ListNode *createlist() { struct ListNode *head,*p1,*tail; int x; head=(struct ListNode *)malloc(sizeof(struct ListNode)); p1=tail=(struct ListNode *)malloc(sizeof(struct ListNode)); p1->next=tail->next=head->next=NULL; scanf("%d",&x); while(x!=-1) { p1->data=x; if(head->next==NULL) head->next=p1; else { tail->next=p1; tail=p1; } p1=(struct ListNode *)malloc(sizeof(struct ListNode)); p1->next=NULL; scanf("%d",&x); } return head; } struct ListNode *deleteeven(struct ListNode *head) { struct ListNode *p1,*p2; p2=head; p1=head->next; while(p1!=NULL) { if(p1->data%2==0) { p2->next=p1->next; } else p2=p2->next; p1=p1->next; } return head->next; }
更多相关内容 -
删除单链表偶数节点
2020-05-22 09:17:02=NULL && head->data % 2 == 0){ //不为空且是偶数 q=head; head=head->next; free(q); } if(head==NULL) return NULL; //要删除的结点不是表头结点 p=head; q=head->next; while(q){ if(q->...#include <stdio.h> #include <stdlib.h> struct ListNode { int data; struct ListNode *next; }; struct ListNode *createlist(); struct ListNode *deleteeven( struct ListNode *head ); void printlist( struct ListNode *head ) { struct ListNode *p = head; while (p) { printf("%d ", p->data); p = p->next; } printf("\n"); } int main() { struct ListNode *head; head = createlist(); head = deleteeven(head); printlist(head); return 0; } /* 你的代码将被嵌在这里 */
/* 你的代码将被嵌在这里 */ struct ListNode *createlist() { struct ListNode *head,*tail,*p; head=tail=NULL; int num; while(scanf("%d",&num) && num!=-1){ p=(struct ListNode *)malloc(sizeof(struct ListNode)); p->data=num; p->next=NULL; if(head==NULL && tail==NULL){ head=tail=p; } else{ tail->next=p; tail=p; } } return head; } struct ListNode *deleteeven( struct ListNode *head ) { struct ListNode *p,*q; //要删除的结点是表头结点 while(head!=NULL && head->data % 2 == 0){ //不为空且是偶数 q=head; head=head->next; free(q); } if(head==NULL) return NULL; //要删除的结点不是表头结点 p=head; q=head->next; while(q){ if(q->data%2==0){ //满足条件 删除 p->next=q->next; free(q); } else{ //不满足条件 p结点后移 p=q; } q=p->next; //保持使 q指向 p 的下一个结点 } return head; }
-
PTA 删除单链表偶数节点 (10 分)
2021-12-12 10:22:39本题要求实现两个函数,分别将读入的数据存储为单链表、将链表中偶数值的结点删除。链表结点定义如下: struct ListNode { int data; struct ListNode *next; }; 函数接口定义: struct ListNode *createlist(); ...- 本题要求实现两个函数,分别将读入的数据存储为单链表、将链表中偶数值的结点删除。链表结点定义如下:
struct ListNode { int data; struct ListNode *next; };
- 函数接口定义:
struct ListNode *createlist(); struct ListNode *deleteeven( struct ListNode *head );
-
函数
createlist
从标准输入读入一系列正整数,按照读入顺序建立单链表。当读到−1
时表示输入结束,函数应返回指向单链表头结点的指针。 -
函数deleteeven将单链表head中偶数值的结点删除,返回结果链表的头指针。
- 裁判测试程序样例:
#include <stdio.h> #include <stdlib.h> struct ListNode { int data; struct ListNode *next; }; struct ListNode *createlist(); struct ListNode *deleteeven( struct ListNode *head ); void printlist( struct ListNode *head ) { struct ListNode *p = head; while (p) { printf("%d ", p->data); p = p->next; } printf("\n"); } int main() { struct ListNode *head; head = createlist(); head = deleteeven(head); printlist(head); return 0; } /* 你的代码将被嵌在这里 */
- 输入样例:
1 2 2 3 4 5 6 7 -1 结尾无空行
- 输出样例:
1 3 5 7 结尾无空行
- AC:
typedef struct ListNode *List; struct ListNode *createlist() { List head = NULL, tail = NULL; int n; scanf("%d", &n); while(n != -1) { List temp = (List)malloc(sizeof(List)); temp->data = n; temp->next = NULL; if(tail == NULL) head = tail = temp; else { tail->next = temp; tail = temp; } scanf("%d", &n); } return head; } struct ListNode *deleteeven(struct ListNode *head) { List newhead = NULL, p = head, tail; while(p) { if((p->data) % 2 != 0) { List temp = (List)malloc(sizeof(List)); temp->data = p->data; temp->next = NULL; if(newhead == NULL) newhead = tail = temp; else { tail->next = temp; tail = temp; } } p = p->next; } return newhead; }
-
[PTA]实验11-2-4 删除单链表偶数节点
2021-05-29 16:40:46本题要求实现两个函数,分别将读入的数据存储为单链表、将链表中偶数值的结点删除。链表结点定义如下: struct ListNode { int data; struct ListNode *next; }; 函数接口定义: struct ListNode *createlist(); ...本题要求实现两个函数,分别将读入的数据存储为单链表、将链表中偶数值的结点删除。链表结点定义如下:
struct ListNode { int data; struct ListNode *next; };
函数接口定义:
struct ListNode *createlist(); struct ListNode *deleteeven( struct ListNode *head );
函数createlist从标准输入读入一系列正整数,按照读入顺序建立单链表。当读到−1时表示输入结束,函数应返回指向单链表头结点的指针。
函数deleteeven将单链表head中偶数值的结点删除,返回结果链表的头指针。
裁判测试程序样例:
#include <stdio.h> #include <stdlib.h> struct ListNode { int data; struct ListNode *next; }; struct ListNode *createlist(); struct ListNode *deleteeven( struct ListNode *head ); void printlist( struct ListNode *head ) { struct ListNode *p = head; while (p) { printf("%d ", p->data); p = p->next; } printf("\n"); } int main() { struct ListNode *head; head = createlist(); head = deleteeven(head); printlist(head); return 0; } /* 你的代码将被嵌在这里 */
输入样例:
1 2 2 3 4 5 6 7 -1
输出样例:
1 3 5 7
- 提交结果:
- 源码:
#include <stdio.h> #include <stdlib.h> struct ListNode { int data; struct ListNode* next; }; struct ListNode* createlist(); struct ListNode* deleteeven(struct ListNode* head); void printlist(struct ListNode* head) { struct ListNode* p = head; while (p) { printf("%d ", p->data); p = p->next; } printf("\n"); } int main() { struct ListNode* head; head = createlist(); head = deleteeven(head); printlist(head); return 0; } /* 你的代码将被嵌在这里 */ struct ListNode* createlist() { struct ListNode* head, * tail, * temp; // 头节点、尾节点、临时节点 // 为头节点分配内存,头节点不存储信息 head = (struct ListNode*)malloc(sizeof(struct ListNode)); // 头节点指向空 head->next = NULL; // 此时头、尾节点相同 tail = head; int data; scanf("%d", &data); while (data != -1) { // 为临时节点分配内存 temp = (struct ListNode*)malloc(sizeof(struct ListNode)); // 临时节点指向空 temp->next = NULL; // 存入数据 temp->data = data; // 将临时节点连接到链表尾 tail->next = temp; // 更新尾节点 tail = temp; scanf("%d", &data); } return head; } struct ListNode* deleteeven(struct ListNode* head) { struct ListNode* current = head; // 当前节点 // 链表为空 if (!head) { return NULL; } // 由于头节点不存储数据,故需从head->next开始遍历链表 while (current->next) { // 偶数节点,需要删除 if (current->next->data % 2 == 0) { // 临时节点,保存当前节点(current->next) struct ListNode* temp = current->next; // 当前节点的下个节点的地址覆盖当前节点的地址 current->next = temp->next; // 释放当前节点 free(temp); } else { current = current->next; } } // 头节点不存储值 head = head->next; return head; }
-
6-2 删除单链表偶数节点 (10 分)
2022-01-24 18:17:41本题要求实现两个函数,分别将读入的数据存储为单链表、将链表中偶数值的结点删除。链表结点定义如下: struct ListNode { int data; struct ListNode *next; }; 函数接口定义: struct ListNode *createlist(); ... -
6-6 删除单链表偶数节点 (10 分)
2021-09-28 11:47:086-6 删除单链表偶数节点 (10 分) 本题要求实现两个函数,分别将读入的数据存储为单链表、将链表中偶数值的结点删除。链表结点定义如下: struct ListNode { int data; struct ListNode *next; }; 函数接口定义: ... -
6-9 删除单链表偶数节点 (14分)
2020-08-30 15:39:16本题要求实现两个函数,分别将读入的数据存储为单链表、将链表中偶数值的结点删除。链表结点定义如下: struct ListNode { int data; struct ListNode *next; }; 函数接口定义: struct ListNode *createlist(); ... -
【PTA数据结构】6-3 删除单链表偶数节点 (20 分)
2021-09-29 13:12:04本题要求实现两个函数,分别将读入的数据存储为单链表、将链表中偶数值的结点删除。链表结点定义如下: struct ListNode { int data; struct ListNode *next; }; 函数接口定义: struct ListNode *createlist(); ... -
数据结构· 删除单链表偶数节点
2020-05-02 21:24:20本题要求实现两个函数,分别将读入的数据存储为单链表、将链表中偶数值的结点删除。链表结点定义如下: struct ListNode { int data; struct ListNode *next; }; 函数接口定义: struct ListNode *createlist();... -
8-6-4 删除单链表偶数节点 (20 分)
2021-05-04 00:18:058-6-4 删除单链表偶数节点 (20 分) 本题要求实现两个函数,分别将读入的数据存储为单链表、将链表中偶数值的结点删除。链表结点定义如下: struct ListNode { int data; struct ListNode *next; }; 函数接口定义... -
删除单链表偶数节点 (20分)
2019-12-21 14:38:16删除单链表偶数节点 (20分) 本题要求实现两个函数,分别将读入的数据存储为单链表、将链表中偶数值的结点删除。链表结点定义如下: struct ListNode { int data; struct ListNode *next; }; 函数接口定义: ... -
6-6 删除单链表偶数节点 (20分)
2020-05-14 09:16:346-6 删除单链表偶数节点 (20分) -
6-3 删除单链表偶数节点 (20分)
2020-04-17 18:16:53本题要求实现两个函数,分别将读入的数据存储为单链表、将链表中偶数值的结点删除。链表结点定义如下: struct ListNode { int data; struct ListNode *next; }; 函数接口定义: struct ListNode *createlist(); ... -
删除单链表偶数节点PTA
2019-05-02 10:39:29本题要求实现两个函数,分别将读入的数据存储为单链表、将链表中偶数值的结点删除。链表结点定义如下: struct ListNode { int data; struct ListNode *next; }; 函数接口定义: struct ListNode *createlist(); ... -
6-24 删除单链表偶数节点 (20分)
2020-04-20 00:52:33本题要求实现两个函数,分别将读入的数据存储为单链表、将链表中偶数值的结点删除。链表结点定义如下: struct ListNode { int data; struct ListNode *next; }; 函数接口定义: struct ListNode *create... -
删除单链表偶数节点 (10 分)
2019-05-19 22:16:18删除单链表偶数节点 (10 分) 本题要求实现两个函数,分别将读入的数据存储为单链表、将链表中偶数值的结点删除。链表结点定义如下: struct ListNode { int data; struct ListNode *next; }; 函数接口定义: struct ... -
[C语言][PTA][2019Fall] 6-24 删除单链表偶数节点 (20 point(s))
2021-05-22 14:10:15Post Views:162最后更新时间: 2021-04-02 19:51:30()声明这是 拼题A(PTA)《中M2019秋C入门和进阶练习集》的习题。原题在 ...题目描述6-24 删除... -
删除单链表偶数节点 (15 分)
2018-11-30 15:34:196-5 删除单链表偶数节点 (15 分) 本题要求实现两个函数,分别将读入的数据存储为单链表、将链表中偶数值的结点删除。链表结点定义如下: struct ListNode { int data; struct ListNode *next; }; 函数... -
PTA: 6-5 删除单链表偶数节点 (20 分)
2019-03-31 10:07:53删除单链表偶数节点 本题要求实现两个函数,分别将读入的数据存储为单链表、将链表中偶数值的结点删除。 链表结点定义如下: struct ListNode { int data; struct ListNode *next; }; 函数接口定义: struct ... -
PTA 6-2 删除单链表偶数节点 (20分)
2020-03-08 20:37:26全部代码 #include <stdio.h> #include <stdlib.h> struct ListNode { int data; struct ListNode *next; }; struct ListNode *createlist();...struct ListNode *deleteeven( struct ListNode *head... -
6-4 删除单链表偶数节点 (10 分)
2019-03-13 20:57:20本题要求实现两个函数,分别将读入的数据存储为单链表、将链表中偶数值的结点删除。链表结点定义如下: struct ListNode { int data; struct ListNode *next; }; 函数接口定义: struct ListNode *create... -
6-2 删除单链表偶数节点 (20 分)
2019-01-04 00:38:04本题要求实现两个函数,分别将读入的数据存储为单链表、将链表中偶数值的结点删除。链表结点定义如下: struct ListNode { int data; struct ListNode *next; }; 函数接口定义: struct ListNode *createlist... -
6-21 删除单链表偶数节点 (20分)
2020-01-04 18:29:40本题要求实现两个函数,分别将读入的数据存储为单链表、将链表中偶数值的结点删除。链表结点定义如下: struct ListNode { int data; struct ListNode *next; }; 函数接口定义: struct ListNode *createlist(); ... -
实验11-2-4 删除单链表偶数节点 (20分)
2020-03-22 11:27:14本题要求实现两个函数,分别将读入的数据存储为单链表、将链表中偶数值的结点删除。 函数接口定义: struct ListNode *createlist(); struct ListNode *deleteeven( struct ListNode *head ); 函数createlist从标准... -
PTA: 删除单链表偶数节点(C语言)
2020-04-21 17:31:15本题要求实现两个函数,分别将读入的数据存储为单链表、将链表中偶数值的结点删除。链表结点定义如下: struct ListNode { int data; struct ListNode *next; }; 函数接口定义: struct ListNode *createlist(); ... -
删除单链表偶数节点,/* 你的代码将被嵌在这里 */这里的代码如何写
2021-05-22 14:10:17删除单链表偶数节点,/*你的代码将被嵌在这里*/这里的代码如何写0rxianok2018.04.11浏览39次分享举报删除单链表偶数节点本题要求实现两个函数,分别将读入的数据存储为单链表、将链表中偶数值的结点删除。... -
实验11-2-4 删除单链表偶数节点 (20 分)
2021-12-06 15:24:41本题要求实现两个函数,分别将读入的数据存储为单链表、将链表中偶数值的结点删除。链表结点定义如下: struct ListNode { int data; struct ListNode *next; }; 函数接口定义: struct ListNode *createlist... -
PTA删除单链表偶数节点
2021-03-15 16:12:47创建链表,返回头节点。创建头指针,并分配内存空间,头...删除函数,将头节点单拿出来考虑。for遍历链表节点,while判断删除。#include #include #includeusing namespace std;struct ListNode {int data;struct Li...