您的位置:首页 > 理论基础 > 数据结构算法

数据结构上机测试2-1:单链表操作A

2014-11-04 20:39 253 查看

传送门

代码:
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;

struct node{
	int data;
	struct node *next;
};

int main()
{
    int n, i, cnt, m;
    struct node *head,  *tail, *p, *q;
    head = (struct node *)malloc(sizeof(struct node));
    head->next = NULL;
    tail = head;
    while(~scanf("%d", &n))
	{
		cnt = 0;
		for(i=0; i<n; i++)
		{
			p = new node;
            p->next = NULL;
            scanf("%d", &p->data);
            tail->next = p;
            tail = p;
		}
		scanf("%d", &m);

		printf("%d\n", n);
		tail = head->next;
		while(tail!=NULL)
		{
			if(tail->next==NULL)
				printf("%d\n", tail->data);
			else printf("%d ", tail->data);
			tail= tail->next;
		}
		p = head;
		q = head->next;
		while(q!=NULL)
		{
			if(q->data==m)
			{
			    p->next = q->next;
			    free(q);
			    cnt++;
			}
			else
				p = p->next;
			q = p->next;
		}
		printf("%d\n", n-cnt);
        tail = head->next;
		while(tail!=NULL)
		{
			if(tail->next==NULL)
				printf("%d\n", tail->data);
			else printf("%d ", tail->data);
			tail = tail->next;
		}
	}
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: