数据结构上机测试2-1:单链表操作A
来源:互联网 发布:阿里云100gb空间多少钱 编辑:程序博客网 时间:2024/04/28 06:00
数据结构上机测试2-1:单链表操作A
Problem Description
Input
第二行依次输入n个整数;
第三行输入欲删除数据m。
Output
第二行依次输出原始单链表的数据;
第三行输出完成删除后的单链表长度;
第四行依次输出完成删除后的单链表数据。
Example Input
1056 25 12 33 66 54 7 12 33 1212
Example Output
1056 25 12 33 66 54 7 12 33 12756 25 33 66 54 7 33
#include<stdio.h>
#include<malloc.h>
struct LinkList{
int data;
struct LinkList *next;
};
LinkList * CreateList(LinkList *head,int n){
LinkList *tail,*r;
head->next=NULL;
tail = head;
for(int i=0;i<n;i++){
r=(LinkList *)malloc(sizeof(LinkList));
scanf("%d",&r->data);
tail->next=r;
tail=r;
}
tail->next = NULL;
return head;
}
LinkList *DeleList(LinkList *&head,int &n,int key)
{
LinkList *p,*q,*tail,*temp2;
p = head;
while(p->next)
{
if(p->next->data==key) //p的下一个元素与key对比
{
n--;
tail = p->next; //tail是与key相同的元素的指针地址
p->next = tail->next; //将tail的后继赋值给tail的后继
free(tail); //释放tail
}
else
p = p->next;
}
printf("%d\n",n);
return head;
}
void DispList(LinkList *head){
LinkList *p=head->next;
while(p->next!=NULL){
printf("%d ",p->data);
p=p->next;
}
printf("%d\n",p->data);
}
int main(){
int n;
scanf("%d",&n);
LinkList *head;
head=(LinkList *)malloc(sizeof(LinkList));
CreateList(head,n);
int key;
scanf("%d",&key);
printf("%d\n",n);
DispList(head);
DeleList(head,n,key);
DispList(head);
return 0;
}
- 1138 数据结构上机测试2-1:单链表操作A
- 数据结构上机测试2-1:单链表操作A
- 数据结构上机测试2-1:单链表操作A
- 数据结构上机测试2-1:单链表操作A
- 数据结构上机测试2-1:单链表操作A
- 数据结构上机测试2-1:单链表操作A
- 数据结构上机测试2-1:单链表操作A
- 数据结构上机测试2-1:单链表操作A
- 数据结构上机测试2-1:单链表操作A
- 数据结构上机测试2-1:单链表操作A
- SDUToj1138数据结构上机测试2-1:单链表操作A
- 数据结构上机测试2-1:单链表操作A
- 数据结构上机测试2-1:单链表操作A
- 数据结构上机测试2-1:单链表操作A
- 【1138】数据结构上机测试2-1:单链表操作A
- SDUT1138数据结构上机测试2-1:单链表操作A
- 数据结构上机测试2-1:单链表操作A
- 数据结构上机测试2-1:单链表操作A
- 美丽的油画 codeforces 651B
- 387. First Unique Character in a String
- 解决yoeman yo命令的权限问题 Error: EACCES, permission denied
- java中的String类常量池
- 第二代增强cmod
- 数据结构上机测试2-1:单链表操作A
- 不用iframe实现在一个页面内加载另一个页面
- css3实现文本指定行数,超出部分省略号代替
- 数组两端取数
- 【JZOJ junior 2059】 稀有逛超市
- AndroidStudio 2.2使用 Java 8 功能和 Jack 工具链的问题
- 半角・全角切り替え
- 使用 netmap 提升 ixgbe 性能
- 基于手工打造的卷积CNN的性别识别