c语言链表 注释的地方要怎么实现

typedef struct node{
double data;
struct node* next;
} Node;

bool IsEmpty(Node* head);
/*
function:
tests whether a list is empty
input:
head - pointer to the first node
output:
true if the list is empty and false otherwise
/
Node
InsertNode(Node** phead, int index, double x);
/*
function:
inserts a new node with certain data after a certain position
input:
phead - pointer to the pointer to the first node
index - the new node is inerted after position index
insert a new node as the head if index=0
x - data of the new node
output:
a pointer to the new node if insertion is successful and NULL otherwise
/
int FindNode(Node
head, double x);
/*
function:
finds node with certain data
input:
head - pointer to the first node
x - the first node whose data=x is returned
output:
returns the position of the first node whose data=x
returns 0 if no such node exists
/
int DeleteNode(Node
* phead, double x);
/*
function:
deletes a node with certain data
input:
phead - pointer to the pointer to the first node
x - the first node whose data=x is deleted
output:
returns the position of the deleted node
returns 0 if no such node exists
/
void DisplayList(Node
head);
/*
function:
prints all the nodes in the list
input:
head - pointer to the first node
/
void DestroyList(Node
* phead);
/*
function:
deletes all the nodes in the list and frees the memory occupied by them
input:
phead - pointer to the head
*/

typedef struct node{
double data;
struct node* next;
} Node;

bool IsEmpty(Node* head)
{
    if(head == NULL)
        return true;
    return false;
}

Node* InsertNode(Node** phead, int index, double x)
{
    Node *p = *phead;
    Node *q = (Node*)malloc(sizeof(Node));
    q->data = x;
    q->next = NULL;
    if(index == 0 || p == NULL)
    {
        q->next = p;
        *phead = q;
    }
    else
    {
        while(p->next != NULL && index > 0)
        {
            p = p->next;
            index--;
        }
        if(index == 0)
        {
            q->next = p->next->next;
            p->next = q;
        }
        else
        {
            p->next = q;
        }
    }
    return q;
}

int FindNode(Node* head, double x)
{
    int idx = 1;
    while(head != NULL)
    {
        if(head->data == x)
            return idx;
        head = head->next;
        idx++;
    }
    return 0;
}

int DeleteNode(Node** phead, double x)
{
    int idx = 1;
    Node *p = *phead;
    if(p->data == x)
    {
        *phead = p->next;
        return 1;
    }
    while(p->next != NULL)
    {
        if(p->next->data == x)
        {
            Node *k = p->next;
            p->next = p->next->next;
            free(k);
            return idx;
        }
        p = p->next;
        idx++;
    }
    return 0;
}

void DisplayList(Node* head)
{
    while(head != NULL)
    {
        printf("%lf\n",head->data);
        head = head->next;
    }
}

void DestroyList(Node** phead)
{
    Node *p = *phead;
    Node *q = p;
    while(p != NULL)
    {
        q = p;
        p = p->next;
        free(q);
    }
    *phead = NULL;
}

img


重新贴一下代码

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>

typedef struct node {
  double data;
  struct node *next;
} Node;

/*
function:
tests whether a list is empty
input:
head - pointer to the first node
output:
true if the list is empty and false otherwise
*/
int IsEmpty(Node *head) { return head == NULL; }

/*
function:
inserts a new node with certain data after a certain position
input:
phead - pointer to the pointer to the first node
index - the new node is inerted after position index
insert a new node as the head if index=0
x - data of the new node
output:
a pointer to the new node if insertion is successful and NULL otherwise
*/
Node *InsertNode(Node **phead, int index, double x) {
  assert(index >= 0);

  Node *p = (Node *)malloc(sizeof(Node));
  p->data = x;
  p->next = NULL;

  if (index == 0) {
    p->next = *phead;
    *phead = p;
  } else {
    int i = 1;
    Node *prev = *phead;
    while (i < index) {
      prev = prev->next;
      i++;
    }
    p->next = prev->next;
    prev->next = p;
  }

  return p;
}

/*
function:
finds node with certain data
input:
head - pointer to the first node
x - the first node whose data=x is returned
output:
returns the position of the first node whose data=x
returns 0 if no such node exists
*/
int FindNode(Node *head, double x) {
  int pos = 1;
  while (head && head->data != x) {
    pos++;
    head = head->next;
  }
  return head == NULL ? 0 : pos;
}

/*
function:
deletes a node with certain data
input:
phead - pointer to the pointer to the first node
x - the first node whose data=x is deleted
output:
returns the position of the deleted node
returns 0 if no such node exists
*/
int DeleteNode(Node **phead, double x) {
  int pos = 1;
  Node *prev = NULL;
  Node *p = *phead;
  while (p && p->data != x) {
    prev = p;
    p = p->next;
    pos++;
  }
  if (p) {
    if (p == *phead)
      *phead = p->next;
    else
      prev->next = p->next;
    free(p);
  } else {
    pos = 0;
  }
  return pos;
}

/*
function:
prints all the nodes in the list
input:
head - pointer to the first node
*/
void DisplayList(Node *head) {
  while (head) {
    printf("%lf ", head->data);
    head = head->next;
  }
  printf("\n");
}

/*
function:
deletes all the nodes in the list and frees the memory occupied by them
input:
phead - pointer to the head
*/
void DestroyList(Node *phead) {
  while (phead) {
    Node *p = phead;
    phead = phead->next;
    free(p);
  }
}

int main() {
  Node *head = NULL;
  printf("Is Empty: %d\n", IsEmpty(head));
  for (int i = 0; i < 5; i++)
    InsertNode(&head, i, i);
  DisplayList(head);
  printf("Find 2: %d\n", FindNode(head, 2));
  DeleteNode(&head, 2);
  DisplayList(head);
  printf("Find 2: %d\n", FindNode(head, 2));
  DestroyList(head);
  return 0;
}

运行结果:

img

代码如下:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>

typedef struct node {
    double data;
    struct node* next;
} Node;

bool IsEmpty(Node* head)
{
    if (head == 0)
        return 1;
    else
        return 0;
}
/*
 function:
    tests whether a list is empty
 input:
    head - pointer to the first node
 output:
    true if the list is empty and false otherwise
*/
Node* InsertNode(Node** phead, int index, double x)
{
    int i = 0;
    Node* cur=0;
    Node* p = (Node*)malloc(sizeof(Node));
    if (p == NULL) //判断申请内存是否成功
        return NULL;

    p->data = x;

    

    if (x == 0 || *phead == NULL)
    {
        p->next = *phead; //p的next指向原来的头节点
        *phead = p; //p成为新的头节点
        return p;
    }
    else
    {
        i = 1;
        cur = *phead;
        while (i < x && cur != NULL)
        {
            i++;
            cur = cur->next;
        }
        if (cur == NULL) //
            return NULL;
        else
        {
            p->next = cur->next;
            cur->next = p;
            return p;
        }
    }
}
/*
 function:
    inserts a new node with certain data after a certain position
 input:
    phead - pointer to the pointer to the first node
    index - the new node is inerted after position index
            insert a new node as the head if index=0
    x - data of the new node
 output:
    a pointer to the new node if insertion is successful and NULL otherwise
*/
int FindNode(Node* head, double x)
{
    Node* p = head;
    int index = 1;
    while (p)
    {
        if (p->data == x)
            return index;
        else
        {
            index++;
            p = p->next;
        }
    }
    return 0;
}
/*
 function:
    finds node with certain data
 input:
    head - pointer to the first node
    x - the first node whose data=x is returned
 output:
    returns the position of the first node whose data=x
    returns 0 if no such node exists
*/
int DeleteNode(Node** phead, double x)
{
    int index = 1;
    Node* pre = *phead;
    Node* cur = 0;

    if (pre->data == x)//如果头节点满足条件
    {
        cur = pre->next;
        free(pre);
        *phead = cur; //将原来的第二个节点作为新的头节点
        return index;
    }
    else
    {
        index = 2;//从第二个节点开始判断
        cur = pre->next;
        while (cur != NULL)
        {
            if (cur->data == x)
            {
                pre->next = cur->next;
                free(cur);
                return index;
            }
            else
            {
                index++;
                pre = pre->next;
                cur = pre->next;
            }
        }
        return 0; //没有找到返回0
    }
}
/*
 function:
    deletes a node with certain data
 input:
    phead - pointer to the pointer to the first node
    x - the first node whose data=x is deleted
 output:
    returns the position of the deleted node
    returns 0 if no such node exists
*/
void DisplayList(Node* head)
{
    Node* p = head;
    while (p != NULL)
    {
        printf("%g ", p->data);
        p = p->next;
    }
    printf("\n");
}
/*
 function:
    prints all the nodes in the list
 input:
    head - pointer to the first node
*/
void DestroyList(Node** phead)
{
    Node* p = *phead;
    Node* t;
    while (p != NULL)
    {
        t = p->next;
        free(p);
        p = t;
    }
    *phead = NULL; //将指针置0
}
/*
 function:
    deletes all the nodes in the list and frees the memory occupied by them
 input:
    phead - pointer to the head
*/



int main()
{
    Node* head = 0;
    for (int i = 0; i < 5; i++)
        InsertNode(&head, i, i);
    DisplayList(head);

    for (int i = 0; i < 5; i++)
        InsertNode(&head, 0, i);
    DisplayList(head);

    InsertNode(&head, 12, 7);
    DisplayList(head);


    for (int i = 0; i < 7; i += 2) {
        int idx = FindNode(head, i);
        if (idx > 0)
            printf("%d is at position %d.\n", i, idx);
        else
            printf("%d is not in the list.\n", i);
    }

    DeleteNode(&head, 0);
    DisplayList(head);

    DeleteNode(&head, 4);
    DisplayList(head);

    DeleteNode(&head, 7);
    DisplayList(head);

    DestroyList(&head);

}