#include <stdio.h>
#include <malloc.h>
#include <conio.h>
typedef struct node //定义节点
{
int value;
struct node* next;
}note;
note* head = NULL;
void del (note** head, int k)//删除链表
{
note* pp;
note* pt;
note* pq;
pp = *head;
if ((*head)->value == k)//如果头结点的值等于k,删除头结点
{
*head = (*head)->next;
return;
}
while(pp->value != k)
{
pt = pp;
pq = pp->next;
pp = pq;
}
pt->next = pp->next;//删除结点
}
void insert(note** head, int q)//建立链表
{
note* pp;
note* pt;
note* p = (note*)malloc(sizeof(note));
p->value = q;
p->next = NULL;
pp = *head;
if (*head==NULL)
{
*head=p;
return;
}
while(pp->next!=NULL)
{
pt = pp->next;
pp = pt;
}
pp->next = p;
}
void print(note* head)//打印链表
{
note* pp;
while(head!=NULL)
{
printf("%d ", head->value);
pp = head->next;
head = pp;
}
}
int main()
{
int i;
int n,k,value;
scanf("%d %d",&n, &k);
for(i=0; i<n; i++)
{
scanf("%d", &value);
insert(&head, value); //把head的地址传过去
}
del(&head, k);
print(head);
getch();//随意按个键退出界面
return 0;
}
温馨提示:答案为网友推荐,仅供参考