40

问答题 40 /50

1)写出程序把一个链表中的接点顺序倒排。
2)  写出程序删除链表中的所有接点。
3) 两个字符串, s,t; t 字符串插入到 s 字符串中, s 字符串有足够的空间存放 t 字符串。

参考答案

1. 写出程序把一个链表中的接点顺序倒排 typedef struct linknode
{
    int data;
    struct linknode *next;
} node;
//将一个链表逆置
node *reverse(node *head)
{
    node *p, *q, *r;
    p = head;
    q = p->next;
    while (q != NULL)
    {
        r = q->next;
        q->next = p;
        p = q;
        q = r;
    }

    head->next = NULL;
    head = p;
    return head;
}

2. 写出程序删除链表中的所有接点。 void del_all(node *head)
{
    node *p;
    while (head != NULL)
    {
        p = head->next;
        free(head);
        head = p;
    }
    cout << "释放空间成功!" << endl;
}

3. 两个字符串,s,t;t字符串插入到s字符串中,s字符串有足够的空间存放t字符串。 void insert(char *s, char *t, int i)
{
    char *q = t;
    char *p = s;
    if (q == NULL)return;
    while (*p != '\0')
    {
        p++;
    }
    while (*q != 0)
    {
        *p = *q;
        p++;
        q++;
    }
    *p = '\0';
}