【C语言编程】写一个函数del,删除动态链表中指定的结点

输入时首先输入两个个数n,k,然后输入n个数表示链表的每一个元素。k表示删除值为k的节点。最后将删除操作结束后的链表输出
输入示例:
5 2
1 2 3 4 5
输出示例
1 3 4 5

  #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;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2018-03-21
int main(int argc, char** argv)
{
int dest[1024]; // 存储输入的数据
int result[1024]; // 存储最后的结果 这里只是简单的用数组,你完全可以自己写个list

int ntotal = getc(); // getc函数不知道写的对不对,没有查看api
int ndel = getc();

/*获取用户输入的数据*/

int i = -1;

while (++i < ntotal)
dest[i] = getc();

/*判断是否等于ndel,如果不相等则放在result数组中*/

i = -1;

int j = -1;

while (i < ntotal)
if (dest[i] != ndel)

result[++j] = dest[i];

/*打印result数组,你自己打印吧*/

return 0;

}本回答被网友采纳