关于链表的问题!

时间一粒 2010-05-07 12:21:48
题目:A、B为元素递增有序排列的单链表,编写算法另建一单链表C,保存两个表的公共元素,要求C的元素值各不相同且递增有序。
我的算法还没有考虑元素递增问题,而是把相同的元素保存到C中。但是保存有问题~
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>

#define OK 1
#define OVERFLOW -2
#define ERROR 0

typedef int ElemType;
typedef int Status;

typedef struct LNode{
ElemType data;
struct LNode *next;
}LNode,*LinkList;

////创建链表
Status CreateList(LinkList &L,int n)
{
int i;
L=(LinkList)malloc(sizeof(LNode));
if(!L) exit(ERROR);
L->data=n;
L->next=NULL;
printf("请输入元素:\n");
for(i=0;i<n;i++)
{
LinkList p=(LinkList)malloc(sizeof(LNode));
scanf("%d",&p->data);
p->next=L->next;
L->next=p;
}
return OK;
}


///保存元素
Status CommonList(LinkList La,LinkList Lb,LinkList &Lc)
{
Lc=(LinkList)malloc(sizeof(LNode));
if(!Lc) exit(ERROR);
Lc->next=NULL;
while(La->next!=NULL)
{
La=La->next;
// for(i=0;i<num;i++)
while(Lb->next!=NULL)
{
Lb=Lb->next;
if(La->next==Lb->next)
Lc->next=La->next;
Lc=Lc->next;
}
}
Lc->next=NULL;
return OK;
}
////输出元素
Status PrintList(LinkList L)
{
printf("链表的元素为:");
while(L->next)
{
L=L->next;
printf("%d",L->data);
}
printf("\n");
return OK;
}
////主函数
int main()
{
LinkList LA,LB,LC;
int na,nb,nc;
printf("请输入链表LA的个数:");
scanf("%d",&na);
CreateList(LA,na);
PrintList(LA);

printf("请输入链表LB的个数:");
scanf("%d",&nb);
CreateList(LB,nb);
PrintList(LB);

CommonList(LA,LB,LC);
printf("相同元素为:\n");
PrintList(LC);
system("pause");


}
...全文
110 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
liate1 2010-05-07
  • 打赏
  • 举报
回复
先站个位置
时间一粒 2010-05-07
  • 打赏
  • 举报
回复
主要是保存那个函数有问题,希望高手能帮忙指正!谢谢~
aleyn 2010-05-07
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 huanmie_09 的回复:]
C/C++ code

#include<stdio.h>
#include<stdlib.h>

#define OK 1
#define OVERFLOW -2
#define ERROR 0

typedef int ElemType;
typedef int Status;

typedef struct LNode{
ElemType data;
……
[/Quote]
支持楼上的
blackkey2008 2010-05-07
  • 打赏
  • 举报
回复
占位

马上上代码
huanmie_09 2010-05-07
  • 打赏
  • 举报
回复

#include<stdio.h>
#include<stdlib.h>

#define OK 1
#define OVERFLOW -2
#define ERROR 0

typedef int ElemType;
typedef int Status;

typedef struct LNode{
ElemType data;
struct LNode *next;
} LNode, *LinkList;

////创建链表
//插头法创建
Status CreateList(LinkList &L, int n)
{
int i;
L=(LinkList)malloc(sizeof(LNode));
if(!L) {
exit(ERROR);
}
L->data=n;
L->next=NULL;
printf("请输入元素:\n");
for(i=0;i<n;i++)
{
LinkList p=(LinkList)malloc(sizeof(LNode));
scanf("%d",&p->data);
p->next=L->next;
L->next=p;
}
return OK;
}


///保存元素
Status CommonList(LinkList La,LinkList Lb,LinkList &Lc)
{
Lc=(LinkList)malloc(sizeof(LNode));
if(!Lc) {
exit(ERROR);
}
Lc->next=NULL;
LinkList p = La->next, q = Lb->next, t = Lc;

while(p != NULL && q != NULL) {
if(p->data > q->data) {
q = q->next;
}
else if(p->data < q->data) {
p = p->next;
}
else {
t->next = p;
t = p;
p = p->next;
q = q->next;
}
}
t->next = NULL;
return OK;
}

////输出元素
Status PrintList(LinkList L)
{
if(L == NULL) {
printf("链表未创建!\n");
exit(1);
}
printf("链表的元素为:");

while(L->next)
{
L=L->next;
printf("%d ",L->data);
}
printf("\n");
return OK;
}

////对单链表进行排序
void sort(LinkList &L)
{
LinkList pre = L, q, t, endptr = L;
int len = 0;
int i, flag = 1, j = 0;
while(endptr->next != NULL) {
endptr = endptr->next;
len++;
}
for(i = 0; i < len - 1 && flag; i++) {
flag = 0;
t = pre->next;
q = t->next;
for(; q != endptr; q = t->next) {
if(t->data > q->data) { //交换p, q
pre->next = q;
t->next = q->next;
q->next = t;
flag = 1;
}
pre = pre->next;
t = pre->next;
}
if(t->data > q->data) { //交换p, q
pre->next = q;
t->next = q->next;
q->next = t;
endptr = q;
}
else {
endptr = t;
}
pre = L;
}
}


////主函数
int main()
{
LinkList LA,LB,LC;
int na,nb,nc;
printf("请输入链表LA的个数:");
scanf("%d",&na);
CreateList(LA,na);
sort(LA);
PrintList(LA);

printf("请输入链表LB的个数:");
scanf("%d",&nb);
CreateList(LB,nb);
sort(LB);
PrintList(LB);

CommonList(LA,LB,LC);
printf("相同元素为:\n");
PrintList(LC);
system("pause");

return 0;
}
yangguang0907 2010-05-07
  • 打赏
  • 举报
回复
你的保存函数有一些问题:
1.传入的参数La,Lb不应该变。
2.你看相不相等应该是看data你怎么这样比较了啊,La->next==Lb->next
3.你的程序一直没有释放内存。

我将保存函数改了一下啊
///保存元素
Status CommonList(LinkList La,LinkList Lb,LinkList &Lc)
{
LinkList tempA,tempB,tempC;
tempB = Lb->next;

Lc=(LinkList)malloc(sizeof(LNode));
if(!Lc) return ERROR;
Lc->next=NULL;
tempC = Lc;

for(tempA = La ->next; tempA ->next != NULL;tempA = tempA ->next)
{
for(tempB = Lb ->next; tempB ->next != NULL;tempB = tempB ->next)
if(tempA ->data == tempB ->data)
{
tempC ->next = tempA;
tempC = tempC ->next;
}
}
return OK;
}
赵4老师 2010-05-07
  • 打赏
  • 举报
回复
sort
template<class RanIt>
void sort(RanIt first, RanIt last);
template<class RanIt, class Pred>
void sort(RanIt first, RanIt last, Pred pr);
The first template function reorders the sequence designated by iterators in the range [first, last) to form a sequence ordered by operator<. Thus, the elements are sorted in ascending order.

The function evaluates the ordering predicate X < Y at most ceil((last - first) * log(last - first)) times.

The second template function behaves the same, except that it replaces operator<(X, Y) with pr(X, Y).
赵4老师 2010-05-07
  • 打赏
  • 举报
回复
set_intersection
template<class InIt1, class InIt2, class OutIt>
OutIt set_intersection(InIt1 first1, InIt1 last1,
InIt2 first2, InIt2 last2, OutIt x);
template<class InIt1, class InIt2, class OutIt, class Pred>
OutIt set_intersection(InIt1 first1, InIt1 last1,
InIt2 first2, InIt2 last2, OutIt x, Pred pr);
The first template function alternately copies values from two sequences designated by iterators in the ranges [first1, last1) and [first2, last2), both ordered by operator<, to form a merged sequence of length K beginning at x, also ordered by operator<. The function then returns x + K.

The merge occurs without altering the relative order of elements within either sequence. Moreover, for two elements from different sequences that have equivalent ordering that would otherwise be copied to adjacent elements, the function copies only the element from the ordered range [first1, last1) and skips the other. An element from one sequence that has equivalent ordering with no element from the other sequence is also skipped. Thus, the function merges two ordered sequences to form another ordered sequence that is effectively the intersection of two sets.

If x and first1 designate regions of storage, the range [x, x + K) must not overlap the range [first1, last1). If x and first2 designate regions of storage, the range [x, x + K) must not overlap the range [first2, last2). The function evaluates the ordering predicate X < Y at most 2 * ((last1 - first1) + (last2 - first2)) - 1 times.

The second template function behaves the same, except that it replaces operator<(X, Y) with pr(X, Y).

69,379

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧