这是通过程序设计基础的链表实现顺序表的代码
数据结构上机测试1:顺序表的应用
Time Limit: 1000 ms Memory Limit: 65536 KiB
Submit Statistic Discuss
Problem Description
在长度为n(n<1000)的顺序表中可能存在着一些值相同的“多余”数据元素(类型为整型),编写一个程序将“多余”的数据元素从顺序表中删除,使该表由一个“非纯表”(值相同的元素在表中可能有多个)变成一个“纯表”(值相同的元素在表中只能有一个)。
Input
第一行输入表的长度n;
第二行依次输入顺序表初始存放的n个元素值。
Output
第一行输出完成多余元素删除以后顺序表的元素个数;
第二行依次输出完成删除后的顺序表元素。
Sample Input
12
5 2 5 3 3 4 2 5 7 5 4 3
Sample Output
5
5 2 3 4 7
Hint
用尽可能少的时间和辅助存储空间。
Source
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data ;
struct node *next;
};
int main ()
{
int n , i ;
struct node *head , * p , *q , *x;
scanf("%d", &n);
head = (struct node *)malloc(sizeof(struct node));
head-> next = NULL;
q = head;
for (i = 0 ; i < n ; i++)
{
p = (struct node *) malloc(sizeof(struct node ));
p-> next = NULL;
scanf("%d" , &p->data);
q->next = p;
q = p;
}
p = head->next;
while(p-> next != NULL)
{
x = p;
q = p-> next;
while(q-> next != NULL)
{
if(q-> data == p-> data)
{
x-> next = q-> next;
q = q-> next;
n--;
}
else
{
x = x-> next;
q = q-> next;
}
}
if(q-> next == NULL)
{
if(q->data == p-> data)
{
x-> next = NULL;
n--;
}
}
if(p-> next != NULL)
{
p = p-> next;
}
}
printf("%d\n" , n);
p = head ->next ;
while(p != NULL)
{
if ( p ->next == NULL)
{
printf("%d\n" , p->data);
}
else
{
printf("%d " , p ->data);
}
p = p-> next;
}
return 0;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)