Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Your assignment for next week is to use the solution posted from the previous pr

ID: 3890823 • Letter: Y

Question

Your assignment for next week is to use the solution posted from the previous pre-lab and add the following functions:

int removeItem(int index, List userList) – This function removes the item at location index from userList. If there isn’t an item at that location the function just returns an error code. Note that you are free to decide what error code is returned but you must provide appropriate documentation (comments) for the user.

List appendList(List uList1, List uList2) – This function creates a new list consisting of the items on list uList1 followed by the items on list uList2. Note that this function can’t inform the user if malloc fails, so your documentation needs to warn about that.

List mergeList(List uList1, List uList2) – This function creates a new list consisting of the items on list uList1 followed by the items on list uList2 but with duplicates removed. Note that this function can’t inform the user if malloc fails, so your documentation needs to warn about that

(PREVIOUS WEEK SOLUTION)

int main (void)

{

List myList;

createList(&myList, 100);

addItem (0.13, &myList);

printf(“Item zero: %f ”, getItem(0, mylist));

printf(“List size: %d ”, sizeOfList(myList));

deleteList(&myList);

}

1. int createList(List *, int) – This function creates and initializes a List variable. The first argument takes the address of a List variable so that its element values can be initialized by the function. The second argument is an integer giving the maximum possible size of the list.

int createList(List *listPtr, int listSize){

listPtr->maxSizeOfList = listSize;

listPtr->tail = 0;

listPtr->array = malloc(sizeof(float)*listSize);

if (listPtr->array == NULL)return 0;

return 1;

}

2.int addItem(float, List *) – This function takes the first argument and makes it the last item, i.e., the value at the end of the List specified by the second argument. Return 0 if there is an error, else 1.

int addItem(float number, List *listPtr){

if (listPtr->tail == listPtr->maxSizeOfList)return(0);

listPtr->array[listPtr->tail] = number;

listPtr->tail++;

return 1;

}

3. float getItem(int, List) – This function returns the value located at the index given by the first argument.

float getItem(int index, List userList){

return userList.array[index];

}

4. int sizeOfList(List) – This function returns the number of items on the list.

int sizeOfList(List userList){

return userList.tail;

}

5. void deleteList(List *) – This function deletes the memory allocated for the array.

void deleteList(List *userList)

{ userList->tail = 0;

free (userList->array);

userList->array = NULL;

}

Explanation / Answer

linkedlist.c

#include <stdio.h>
#include <malloc.h>
#define EMPTYLIST printf(" EMPTY LIST:");

struct nde
{
int values;
struct nde *nxt;
};

sn* createNode(int);
void ins_nodeAtFirst();
void ins_nodeAtLast();
void ins_nodeAtPos();
void sort_asc();
void del_pos();
void searching();
void update();
void view();
void rev(sn *);

typedef struct node sn;
sn *newNode, *pntr, *previous, *temporary;
sn *fir = NULL, *lst = NULL;

int main()
{
int choice;
char answer = 'Y';

while (answer == 'Y'||answer == 'y')
{
printf(" --------------------------------- ");
printf(" Operations on singly linked list ");
printf(" --------------------------------- ");
printf(" 1.Insert nde at fir");
printf(" 2.Insert nde at lst");
printf(" 3.Insert nde at position");
printf(" 4.Sorted Linked List in Ascending Order");
printf(" 5.Delete Node from any Position");
printf(" 6.Update Node Value");
printf(" 7.Search Element in the linked list");
printf(" 8.Display List from Beginning to end");
printf(" 9.Display List from end using Recursion");
printf(" 10.Exit ");
printf(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ");
printf(" Enter your choice");
scanf("%d", &choice);

switch (choice)
{
case 1:
printf(" ...Inserting nde at fir... ");
ins_nodeAtFirst();
break;
case 2:
printf(" ...Inserting nde at lst... ");
ins_nodeAtLast();
break;
case 3:
printf(" ...Inserting nde at position... ");
ins_nodeAtPos();
break;
case 4:
printf(" ...Sorted Linked List in Ascending Order... ");
sort_asc();
break;
case 5:
printf(" ...Deleting Node from any Position... ");
del_pos();
break;
case 6:
printf(" ...Updating Node Value... ");
update();
break;
case 7:
printf(" ...Searching Element in the List... ");
searching();
break;
case 8:
printf(" ...Displaying List From Beginning to End... ");
view();
break;
case 9:
printf(" ...Displaying List From End using Recursion... ");
rev(fir);
break;
case 10:
printf(" ...Exiting... ");
return 0;
break;
default:
printf(" ...Invalid Choice... ");
break;
}
printf(" YOU WANT TO CONTINUE (Y/N)");
scanf(" %c", &answer);
}
return 0;
}

/*
* Creating Node
*/
sn* createNode(int values)
{
newNode = (sn *)malloc(sizeof(sn));
if (newNode == NULL)
{
printf(" Memory was not allocated");
return 0;
}
else
{
newNode->values = values;
newNode->nxt = NULL;
return newNode;
}
}

/*
* Inserting Node at First
*/
void ins_nodeAtFirst()
{
int values;

printf(" Enter the values for the nde:");
scanf("%d", &values);
newNode = createNode(values);
if (fir == lst && fir == NULL)
{
fir = lst = newNode;
fir->nxt = NULL;
lst->nxt = NULL;
}
else
{
temporary = fir;
fir = newNode;
fir->nxt = temporary;
}
printf(" ----INSERTED----");
}

/*
* Inserting Node at Last
*/
void ins_nodeAtLast()
{
int values;

printf(" Enter the values for the Node:");
scanf("%d", &values);
newNode = createNode(values);
if (fir == lst && lst == NULL)
{
fir = lst = newNode;
fir->nxt = NULL;
lst->nxt = NULL;
}
else
{
lst->nxt = newNode;
lst = newNode;
lst->nxt = NULL;
}
printf(" ----INSERTED----");
}

/*
* Inserting Node at position
*/
void ins_nodeAtPos()
{
int posit, values, cntr = 0, uu;

printf(" Enter the values for the Node:");
scanf("%d", &values);
newNode = createNode(values);
printf(" Enter the position ");
scanf("%d", &posit);
pntr = fir;
while (pntr != NULL)
{
pntr = pntr->nxt;
cntr++;
}
if (posit == 1)
{
if (fir == lst && fir == NULL)
{
fir = lst = newNode;
fir->nxt = NULL;
lst->nxt = NULL;
}
else
{
temporary = fir;
fir = newNode;
fir->nxt = temporary;
}
printf(" Inserted");
}
else if (posit>1 && posit<=cntr)
{
pntr = fir;
for (uu = 1;uu < posit;uu++)
{
previous = pntr;
pntr = pntr->nxt;
}
previous->nxt = newNode;
newNode->nxt = pntr;
printf(" ----INSERTED----");
}
else
{
printf("Position is out of range");
}
}

/*
* Sorted Linked List
*/
void sort_asc()
{
sn *nxt;
int tem;

if (fir == NULL)
{
EMPTYLIST;
printf(":No elements to sort ");
}
else
{
for (pntr = fir;pntr != NULL;pntr = pntr->nxt)
{
for (nxt = pntr->nxt;nxt != NULL;nxt = nxt->nxt)
{
if (pntr->values > nxt->values)
{
tem = pntr->values;
pntr->values = nxt->values;
nxt->values = tem;
}
}
}
printf(" ---Sorted List---");
for (pntr = fir;pntr != NULL;pntr = pntr->nxt)
{
printf("%d em", pntr->values);
}
}
}

/*
* Delete Node from specified position in a non-empty list
*/
void del_pos()
{
int posit, cntr = 0, uu;

if (fir == NULL)
{
EMPTYLIST;
printf(":No nde to delete ");
}
else
{
printf(" Enter the position of values to be deleted:");
scanf(" %d", &posit);
pntr = fir;
if (posit == 1)
{
fir = pntr->nxt;
printf(" Element deleted");
}
else
{
while (pntr != NULL)
{
pntr = pntr->nxt;
cntr = cntr + 1;
}
if (posit > 0 && posit <= cntr)
{
pntr = fir;
for (uu = 1;uu < posit;uu++)
{
previous = pntr;
pntr = pntr->nxt;
}
previous->nxt = pntr->nxt;
}
else
{
printf("Position is out of range");
}
free(pntr);
printf(" Element deleted");
}
}
}
/*
* Updating Node values in a non-empty list
*/
void update()
{
int old, new, change = 0;

if (fir == NULL)
{
EMPTYLIST;
printf(":No nodes in the list to update ");
}
else
{
printf(" Enter the values to be updated:");
scanf("%d", &old);
printf(" Enter the newvalue:");
scanf("%d", &new);
for (pntr = fir;pntr != NULL;pntr = pntr->nxt)
{
if (pntr->values == old)
{
pntr->values = new;
change = 1;
break;
}
}
if (change == 1)
{
printf(" Updated Successfully");
}
else
{
printf(" Value not found in List");
}
}
}

/*
* searching an element in a non-empty list
*/
void searching()
{
int change = 0, keys, posit = 0;

if (fir == NULL)
{
EMPTYLIST;
printf(":No nodes in the list ");
}
else
{
printf(" Enter the values to searching");
scanf("%d", &keys);
for (pntr = fir;pntr != NULL;pntr = pntr->nxt)
{
posit = posit + 1;
if (pntr->values == keys)
{
change = 1;
break;
}
}
if (change == 1)
{
printf(" Element %d found at %d position ", keys, posit);
}
else
{
printf(" Element %d not found in list ", keys);
}
}
}
/*
* Displays non-empty List from Beginning to End
*/
void view()
{
if (fir == NULL)
{
EMPTYLIST;
printf(":No nodes in the list to view ");
}
else
{
for (pntr = fir;pntr != NULL;pntr = pntr->nxt)
{
printf("%d em", pntr->values);
}
}
}

void rev(sn *pntr)
{
int values;

if (pntr == NULL)
{
EMPTYLIST;
printf(":No nodes to view ");
}
else
{
if (pntr != NULL)
{
values = pntr->values;
rev(pntr->nxt);
printf("%d em", values);
}

}
}

Rate an upvote.......Thankyou

Hope this helps.....

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote