Linked Lists Questions
Linked Lists Questions
Linked Lists Questions
Section]
Information: Program templates for questions are available in APAS system. You must use
them to implement your functions.
1. (insertSortedLL) Write a C function insertSortedLL() that asks the user to input an integer,
then inserts it into the linked list in ascending order. The function, insertSortedLL(), should
not allow inserting an integer if it already exists in the current linked list. The function should return
the index position where the new item was added; if the function could not complete successfully, it
should return a value of -1. You can assume that the linked list is either a sorted linked list or an
empty list. ListNode *temp; ᭭ܲկฎcurindex <= ll->index҅ᩳکବፗള
temp = ll->head; ल҅ࣁ᭔Ӿྯӻྲํӣᐿఘ̶٭
The function prototype is given as follows:
int insertSortedLL(LinkedList *ll, int item);
The function should return the index position where the new item was added as follows:
The value 8 was added at index 4
The function does not complete successfully (does not insert the value of 7 to the linked list)
hence it should return a value of -1:
The value 7 was added at index -1
For an example, assume that given two linked lists are LinkedList1 and LinkedList2:
LinkedList1: 1, 2, 3
LinkedList2: 4, 5, 6, 7
The resulting linked lists are:
LinkedList1: 1, 4, 2, 5, 3, 6
LinkedList2: 7
The second list should become empty when the first list is larger than the second list. For an
example, assume that given two linked lists are LinkedList1 and LinkedList2:
LinkedList1: 1, 5, 7, 3, 9, 11
LinkedList2: 6, 10, 2, 4
A sample input and output session is given below (if the current linked lists are Linked list
1: 1, 2, 3 and Linked list 2: 4 , 5, 6, 7):
Linked list 1: 1 2 3
Linked list 2: 4 5 6 7
For example, if the linked list is (1, 2, 3, 4, 5), the resulting linked list will be (5, 4, 3, 2, 1).
ᙧ, ӷӻListnode҅*first҅*rest҅
A sample input and output session is given below: ইํNULLፗളreturnҔ
1: Insert an integer to the linked list: recursiveҁ&rest҂;
2: Reversed Linked List: first-next-next = first;
0: Quit: first-next = NULL;
*ptrHead = rest;
Please input your choice(1/2/0): 1
Input an integer that you want to add to the linked list: 1
The resulting Linked List is: 1
Please input your choice(1/2/0): 1
Input an integer that you want to add to the linked list: 2
The resulting Linked List is: 1 2
Please input your choice(1/2/0): 1
Input an integer that you want to add to the linked list: 3
The resulting Linked List is: 1 2 3
Please input your choice(1/2/0): 1
Input an integer that you want to add to the linked list: 4
The resulting Linked List is: 1 2 3 4
Please input your choice(1/2/0): 1
Input an integer that you want to add to the linked list: 5
The resulting Linked List is: 1 2 3 4 5