对单链表l进行删除数据元素x的c语言算法
- 你的回答被采纳后将获得:
- 系统奖励15(财富值+成长值)+难题奖励30(财富值+成长值)
2015-10-12
展开全部
给你个单链表的
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
typedef int ElemType;
typedef struct node{
ElemType data;
struct node *next;
} LinkList;
void creat(LinkList *&L)
{
L = (LinkList *)malloc(sizeof(LinkList));
L->next = NULL;
}
void Insert(LinkList *L, ElemType e) //插入算法1
{
LinkList *s = NULL;
LinkList *p = L;
s = (LinkList*)malloc(sizeof(LinkList));
s->data = e;
s->next = p->next;
p->next = s;
}
void Insert2(LinkList *L, int i, ElemType e) //插入算法2 可以在任意位置插入
{
int j = 1;
LinkList *p, *s;
p = L;
j = 1;
while (p&&j < i)
{
p = p->next;
j++;
}
if (!p || j>i)
{
exit(0);
}
s = (LinkList*)malloc(sizeof(LinkList));
s->data = e;
s->next = p->next;
p->next = s;
}
void display(LinkList *L)
{
LinkList *p = L->next;
while (p != NULL)
{
printf("%d\n", p->data);
p = p->next;
}
}
void Delete(LinkList *l, int i, ElemType e) //任意位置删除
{
int j = 1;
LinkList *p = l;
LinkList *q;
while (p&&j < i)
{
j++;
p = p->next;
}
if (!p || j>i)
{
exit(0);
}
q = p->next;
p->next = q->next;
e = q->data;
free(q);
}
int main()
{
LinkList *L = NULL;
int a, b, c;
int d, e, K;
creat(L);
/*scanf_s("%d%d%d", &a, &b, &c);
Insert(L, a);
Insert(L, b);
Insert(L, c);
display(L)*/;
scanf_s("%d%d%d", &d, &e, &K);
Insert2(L, 1 ,d);
Insert2(L, 2, e);
Insert2(L, 3, K);
display(L);
Delete(L, 3, K);
display(L);
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
typedef int ElemType;
typedef struct node{
ElemType data;
struct node *next;
} LinkList;
void creat(LinkList *&L)
{
L = (LinkList *)malloc(sizeof(LinkList));
L->next = NULL;
}
void Insert(LinkList *L, ElemType e) //插入算法1
{
LinkList *s = NULL;
LinkList *p = L;
s = (LinkList*)malloc(sizeof(LinkList));
s->data = e;
s->next = p->next;
p->next = s;
}
void Insert2(LinkList *L, int i, ElemType e) //插入算法2 可以在任意位置插入
{
int j = 1;
LinkList *p, *s;
p = L;
j = 1;
while (p&&j < i)
{
p = p->next;
j++;
}
if (!p || j>i)
{
exit(0);
}
s = (LinkList*)malloc(sizeof(LinkList));
s->data = e;
s->next = p->next;
p->next = s;
}
void display(LinkList *L)
{
LinkList *p = L->next;
while (p != NULL)
{
printf("%d\n", p->data);
p = p->next;
}
}
void Delete(LinkList *l, int i, ElemType e) //任意位置删除
{
int j = 1;
LinkList *p = l;
LinkList *q;
while (p&&j < i)
{
j++;
p = p->next;
}
if (!p || j>i)
{
exit(0);
}
q = p->next;
p->next = q->next;
e = q->data;
free(q);
}
int main()
{
LinkList *L = NULL;
int a, b, c;
int d, e, K;
creat(L);
/*scanf_s("%d%d%d", &a, &b, &c);
Insert(L, a);
Insert(L, b);
Insert(L, c);
display(L)*/;
scanf_s("%d%d%d", &d, &e, &K);
Insert2(L, 1 ,d);
Insert2(L, 2, e);
Insert2(L, 3, K);
display(L);
Delete(L, 3, K);
display(L);
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你大概看一下下面的程序,思路没问题。
//假设链表的头指针为head
p=head;
while(head!=NULL && head->data == x)
{
head = head -> next;
free(p);
p = head;
}
if(head == NULL) return head;
p = head -> next;
pold = head;
while(p != NULL)
{
if(p -> data == x)
{
pold->next = p->next;
free(p);
p=pold->next;
}
else{
pold = p;
p = p -> next;
}
}
//假设链表的头指针为head
p=head;
while(head!=NULL && head->data == x)
{
head = head -> next;
free(p);
p = head;
}
if(head == NULL) return head;
p = head -> next;
pold = head;
while(p != NULL)
{
if(p -> data == x)
{
pold->next = p->next;
free(p);
p=pold->next;
}
else{
pold = p;
p = p -> next;
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询