phpstorm使用视频教程:有关C语言(数据结构)程序设计题

来源:百度文库 编辑:高考问答 时间:2024/05/09 10:38:30
问题:请设计一个算法,在一个单链表中值为Y的结点前面插入一个值为X的结点,即使值为X的结点成为值为Y的结点的前驱结点?

谢谢帮忙啊~~
问题2:设计一个算法,求顺序表中值为X的结点的个数?

两个问题都用完整的程序编写~~谢谢

struct Node;
typedef struct Node *PNode;
struct Node //节点类型;
{
DataType info;
PNode link;
};
typedef struct Node *LinkList; //单链表类型

int insert_link(LinkList llist,DataType y,DataType x)
{
PNode p,q;
p=llist;
while(p!=null&&y!=p->info)
p=p->link;

if(p==null)
{
printf("The value of y is not in the link!");
return (0);
}

q=(PNode)malloc(sizeof(struct Node));
if(q==NULL)
{
printf("out of space!\n");
return 0;
}
else
{
q->info=x;
q->link=p->link;
p->link=q;
return 1;
}
}

第二个问题:
struct SeqList
{
DataType element[100]; //存放线性表中的元素;
int n; //存放线性表中元素的个数;
};

typedef struct SeqList *PSeqList;//指向链表;

int find(PSeqList plist,DataType x)
{
int i;
int r=0;//存放x的个数;
for(i=0;i<plist->n;i++)
if(plist->element[i]==x)r++;
return r;
}

首先你要看看你的Y是不首节点
是的话:X.next=Y;就可以了
如果不是的话在查询Y的时候先设一个节点p作为Y的前驱节点,
X.next = Y;
p.next= X;

That is all!!