数据结构上机测试2-1:单链表操作A
2016-09-20 12:34
218 查看
数据结构上机测试2-1:单链表操作A
Time Limit: 1000MS Memory Limit: 4096KBSubmit Statistic Discuss
Problem Description
输入n个整数,先按照数据输入的顺序建立一个带头结点的单链表,再输入一个数据m,将单链表中的值为m的结点全部删除。分别输出建立的初始单链表和完成删除后的单链表。
Input
第一行输入数据个数n;第二行依次输入n个整数;
第三行输入欲删除数据m。
Output
第一行输出原始单链表的长度;第二行依次输出原始单链表的数据;
第三行输出完成删除后的单链表长度;
第四行依次输出完成删除后的单链表数据。
Example Input
10 56 25 12 33 66 54 7 12 33 12 12
Example Output
10 56 25 12 33 66 54 7 12 33 12 7 56 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;
}
相关文章推荐
- 数据结构上机测试2-2:单链表操作B
- 【1139】数据结构上机测试2-2:单链表操作B
- 数据结构上机测试2-1:单链表操作A
- 数据结构上机测试2-2:单链表操作B
- 数据结构上机测试2-1:单链表操作A
- 1138 数据结构上机测试2-1:单链表操作A
- 数据结构上机测试2-1:单链表操作A
- 数据结构上机测试2-1:单链表操作A
- 数据结构上机测试2-2:单链表操作B
- 寒假第二天--线性表--数据结构上机测试2-2:单链表操作B
- 【1138】数据结构上机测试2-1:单链表操作A(顺序建表+删除节点)(SDUT)
- 数据结构上机测试2-2:单链表操作B
- 暑假集训第1天链表-数据结构上机测试2-1:单链表操作A
- 数据结构上机测试2-1:单链表操作A
- 【1139】数据结构上机测试2-2:单链表操作B (逆序建表+重复元素删除)
- 【1138】数据结构上机测试2-1:单链表操作A
- 数据结构上机测试2-2:单链表操作B
- 数据结构上机测试2-1:单链表操作A
- 数据结构上机测试2-2:单链表操作B
- 【1139】数据结构上机测试2-2:单链表操作B (逆序建表+重复元素删除)