#include<stdio.h>
#include<stdlib.h>
typedef struct lnode
{
int data;
struct lnode* next;
}lnode,*link;
int n = 4;
int a[4] = { 1,2,3,4 };
void build(link* L)//建立单链表
{
*L = (lnode*)malloc(sizeof(lnode));
(*L)->next = NULL;
int i = 0;
lnode* s = *L, * r = *L;
for (i = 0; i < n; i++)
{
s = (lnode*)malloc(sizeof(lnode));
s->data = a[i];
s->next = r->next;
r->next = s;
r = s;
}
r->next = NULL;
}
void delete(link* L, int x)//删除x
{
lnode* p = (*L)->next, * pre = *L;
while (p != NULL)
{
if (p->data == x)
{
lnode*q = p;
pre->next= p->next;
p = p->next;
free(q);
}
else
{
p = p->next;
pre = pre->next;
}
}
}
void print(link *L)//输出单链表
{
lnode* p = (*L)->next;
while (p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
int main()
{
link L;
build(&L);
print(&L);
delete(&L,3);
print(&L);
}
06-13
5112
08-09
2760
01-29
696
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交