c语言编程 数据结构题

创建线性顺序表其最大长度为20 并在顺序表中节点的值为{1,4,6,7,10},然后在下标为2的位置插入5打印输出,然后删除值为7的元素并打印输出。
拜托了!

参考代码如下:(手打不易,望采纳!)

/*
创建线性顺序表其最大长度为20 ï¼Œ
在顺序表中节点的值为{1,4,6,7,10},
然后在下标为2的位置插入5打印输出,
然后删除值为7的元素并打印输出。
*/

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

typedef struct node
{
    int a;
    struct node *next;
} NODE;

// åˆ›å»ºé¡ºåºé“¾è¡¨ï¼Œé•¿åº¦listSize,并初始化
NODE *createlist(NODE *head, int listSize, int arr[]);
// æ’入一个节点
int insertnode(NODE *head, int index, int val);
// åˆ é™¤ä¸€ä¸ªèŠ‚点
int delnode(NODE *head, int val);
// æ‰“印链表信息
void printlist(NODE *head);

// ä¸»å‡½æ•°
int main(int argc, char const *argv[])
{
    int arr[5] = {1, 4, 6, 7, 10};
    int arrlen;
    arrlen = (int)sizeof(arr) / (int)sizeof(int);

    NODE *head = NULL;
    head = createlist(head, arrlen, arr);
    if (NULL == head)
    {
        printf("Failed to create list . Program Eixt . \n");
        return 0;
    }

    printf("\n");
    printlist(head);

    if (1 == insertnode(head, 2, 5))
    {
        printf("\nInserted a node before index 2,\n");
        printlist(head);
    }
    else
    {
        printf("\nFailed to Insert a node .\n");
    }

    if (1 == delnode(head, 7))
    {
        printf("\nDeleted a node with value of 7,\n");
        printlist(head);
    }
    else
    {
        printf("\nNo node deleted : value is not exist.\n");
    }
    return 0;
}

// åˆ›å»ºé¡ºåºé“¾è¡¨ï¼Œé•¿åº¦listSize,并初始化
NODE *createlist(NODE *head, int listSize, int arr[])
{
    head = (NODE *)malloc(sizeof(NODE));
    if (NULL == head)
    {
        printf("Oops ! Malloc Failed ! Program Exit !\n");
        return NULL;
    }
    NODE *p, *q;
    p = head;
    for (int i = 0; i < listSize; i++)
    {
        q = (NODE *)malloc(sizeof(NODE));
        if (NULL == q)
        {
            printf("Oops ! Malloc Failed ! Program Exit !\n");
            return NULL;
        }
        q->next = NULL;

        if (i < 5)
        {
            q->a = arr[i];
        }

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

// æ’入一个节点
int insertnode(NODE *head, int index, int val)
{
    NODE *p = head;
    NODE *q = NULL;
    int i, count = 0;
    while (NULL != p->next)
    {
        count++;
        p = p->next;
    }
    if (index > count || index < 0)
    {
        printf("Length of list is %d , please enter proper index to add node. [0,%d]\n", count, count);
        printf("Nothing has been added.\n");
        return 0;
    }

    p = head->next;
    q = head;

    for (i = 0; i <= index; i++)
    {
        if (index == i )
        {
            NODE *n = (NODE *)malloc(sizeof(NODE));
            if (NULL == n)
            {
                printf("Oops ! Malloc Failed ! Program Exit !\n");
                return 0;
            }
            n->a = val;

            n->next = p;
            q->next = n;

            return 1;
        }
        p = p->next;
        q = q->next;
    }
    return 0;
}

// åˆ é™¤ä¸€ä¸ªèŠ‚点
int delnode(NODE *head, int val)
{
    NODE *p, *q;
    p = head->next;
    q = head;
    while (p != NULL)
    {
        if (val == p->a)
        {
            p = p->next;
            q->next = p;
            return 1;
        }
        else
        {
            p = p->next;
            q = q->next;
        }
    }
    return 0;
}

// æ‰“印链表信息
void printlist(NODE *head)
{
    NODE *p;
    p = head->next;
    int i = 0;
    if (NULL != p)
    {
        printf("Print List info as below:\n");
        while (NULL != p)
        {
            printf("Node at Index %d , value is %d .\n", i, p->a);
            i++;
            p = p->next;
        }
    }
    else
    {
        printf("List is empty.\n");
    }
}

Linux环境运行截图:

以上,如有帮助,烦请点采纳,谢谢!

温馨提示:答案为网友推荐,仅供参考
第1个回答  2019-03-23
搜索创建列表的函数方法,再创建插入结点,和删除结点的方法。再在主函数调用
望采纳,谢谢
相似回答