帮帮我~~还在学习中

jkasus 2007-10-15 01:15:56
1,给出删除单链表中值为K的结点的前驱结点的算法.

2,试给出实现删除单链表中值的相同的多余接点的算法.

3,试给出依次输入单链表中所有数据元素的算法.
...全文
96 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
wushichao1987 2007-10-15
  • 打赏
  • 举报
回复
第一个问题:
设两个指针变量front,behind.front始终表示当前节点的前驱节点的前驱,当behind->data == k 时,front = behind
程序开始时可先用语句处理第一和第二个节点,然后用循环处理即可.很简单就不给程序了.
第二个问题:
将单链表中数据放到数组中,排序,遍历链表,对每个数据在数组中查找,若有,则删除.
这个问题应该还有更好的算法,期待高人指点.
第三个问题:
Node *Input(Node *p){
while( p != NULL )
scanf( "%d" , &p->data );
}
这是数据类型为整型时的程序.
星羽 2007-10-15
  • 打赏
  • 举报
回复

最后记得删除连表,不给你写了
星羽 2007-10-15
  • 打赏
  • 举报
回复



#include "stdlib.h"
#include "stdio.h"

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

struct node* build_link(int size) {

struct node* head = 0;
struct node* temp = 0;
struct node* prev = 0;

while (size--) {

temp = (struct node*)malloc(sizeof(struct node));
if (!head)
head = temp;

if (prev)
prev->next = temp;
prev = temp;
}

temp->next = 0;

return head;
}

/*1,给出删除单链表中值为K的结点的前驱结点的算法.*/

int delete_prev_node(struct node* h, int K) {

struct node* t = h;
struct node* p = 0;

if (h == 0)
return 0;

while (t != 0) {

if (t->data == K) {

if (p == 0)
return 0;

p->data = t->data;
p->next = t->next;

free(t);

t = p->next;
continue;
}

p = t;
t = t->next;
}

return 0;
}

/*2,试给出实现删除单链表中值的相同的多余接点的算法.*/

void delete_same(struct node* h)
{
struct node* t = h;

if (h == 0)
return;

while (t != 0)
{
struct node* a = t->next;
struct node* p = t;

while (a != 0)
{
if (a->data == t->data)
{
p->next = a->next;
free(a);
a = p->next;
continue;
}
p = a;
a = a->next;
}

t = t->next;
}
}

/*3,试给出依次输入单链表中所有数据元素的算法.*/

void input_link(struct node* h)
{
struct node* t = h;
int i = 0;

while (t)
{
printf("input node-%d 's data : ", i++);
scanf("%d", &(t->data));
t = t->next;
}

}

void print_link(struct node* h)
{
struct node* t = h;

printf("the link is : ");

while (t)
{
printf("%d ", t->data);
t = t->next;
}

printf("\n");
}

int main()
{
struct node* h = 0;
int s = 0;
int t = 0;

printf("please input the size of the link you want to build : ");
scanf("%d", &s);

if ((h = build_link(s)) == 0)
{
printf("build link error!!\n");
return 1;
}

input_link(h);
print_link(h);

delete_same(h);
printf("after delete the same node\n");
print_link(h);

printf("please input the value K to remove it's prev node : K = ");
scanf("%d", &t);

delete_prev_node(h, t);
print_link(h);


return 0;
}



运行范例 :

please input the size of the link you want to build : 8
input node-0 's data : 1
input node-1 's data : 2
input node-2 's data : 3
input node-3 's data : 3
input node-4 's data : 4
input node-5 's data : 5
input node-6 's data : 5
input node-7 's data : 8
the link is : 1 2 3 3 4 5 5 8
after delete the same node
the link is : 1 2 3 4 5 8
please input the value K to remove it's prev node : K = 3
the link is : 1 3 4 5 8
请按任意键继续. . .



huangxw000 2007-10-15
  • 打赏
  • 举报
回复
同上,单链表还没搞懂,等待高人指点.

69,381

社区成员

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

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