İn C Language pls help #include #include #include

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

İn C Language pls help

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

typedef struct ArrayList_s
{
    void **list;
    int size;
    int capacity;
    int delta_capacity;
} ArrayList_t, *ArrayList;

ArrayList arraylist_create(int capacity, int delta_capacity)
{
    ArrayList l;
    l = (ArrayList)malloc(sizeof(ArrayList_t));
    if (l != NULL)
    {
        l->list = (void **)malloc(capacity * sizeof(void *));
        if (l->list != NULL)
        {
            l->size = 0;
            l->capacity = capacity;
            l->delta_capacity = delta_capacity;
        }
        else
        {
            free(l);
            l = NULL;
        }
    }
    return l;
}

void arraylist_destroy(ArrayList l)
{
    free(l->list);
    free(l);
}

int arraylist_isempty(ArrayList l)
{
    return l->size == 0;
}

int arraylist_isfull(ArrayList l)
{
    return l->size == l->capacity;
}

int arraylist_set(ArrayList l, void *e, int index, void **replaced)
{
    if (!arraylist_isfull(l))
    {
        if (index < 0 && index > l->size)
        {
            index = l->size;
        }
        else
        {
            if (replaced != NULL)
            {
                *replaced = l->list[index];
            }
        }
        if (index == l->size)
        {
            l->size = l->size + 1;
        }
        l->list[index] = e;
    }
    else
    {
        index = -index;
    }
    return index;
}

int arraylist_insert(ArrayList l, void *e, int index)
{
    void **nlist;
    int ncapacity;
    if (index >= 0 && index <= l->size)
    {
        if (arraylist_isfull(l))
        {
            ncapacity = l->capacity + l->delta_capacity;
            nlist = realloc(l->list, ncapacity * sizeof(void *));
            if (nlist != NULL)
            {
                //shift elements between index and l->size-1 to right by 1
                memmove(&nlist[index] + 1, &nlist[index], (l->size - index) * sizeof(void *));

                l->list = nlist;
                l->capacity = ncapacity;
                l->size++;
                l->list[index] = e;
            }
            else
            {
                nlist = (void **)malloc(ncapacity * sizeof(void *));
                if (nlist != NULL)
                {
                    //copy elements while shifting elements between index and l->size-1 to right by 1
                    memcpy(nlist, l->list, index * sizeof(void *));
                    memcpy(&nlist[index] + 1, &l->list[index], (l->size - index) * sizeof(void *));
                    free(l->list);

                    l->list = nlist;
                    l->capacity = ncapacity;
                    l->size++;
                    l->list[index] = e;
                }
                else
                {
                    index = -index;
                }
            }
        }
        else
        {
            //shift elements between index and l->size-1 to right by 1
            memmove(&l->list[index] + 1, &l->list[index], (l->size - index) * sizeof(void *));

            l->size++;
            l->list[index] = e;
        }
    }
    else
    {
        index = -index;
    }
    return index;
}

void *arraylist_delete(ArrayList l, int index)
{
    /*
    define *e, ncapacity, **nlist
    e <- null
    if l is not empty and index >= 0 and index < size of l then
        e <- l->list[index]
        decrease l->size once
        shift elements between index + 1 to left by 1
        ncapacity <- capacity of l / 2
        if size of l <= ncapacity and ncapacity > 0 then
            nlist <- realloc(list of l, ncapacity * size of (void *))
            if nlist not null then
                list of l <- nlist
                capacity of l <- ncapacity
            else
                nlist <- (void **)malloc(ncapacity * size of (void *))
                if nlist not null then
                    memcpy(nlist, list of l, size of l * size of (void *))
                    free list of l
                    list of l <- nlist
                    capacity of l <- ncapacity
            endif
        endif
    endif
    return e
    */

}

int main()
{

    return 0;
}

Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY