How to compare eg. Linked list stores : 1 2 3. Then user input 2. How could I compare the user input '2' with the linked list items? So I could possibly slot '2' before '3' in the linked list to make the linked list have 4 items? I tried by comparing the linked list item to the user input but could not successfully do so. Could you teach me how to do it? This time round, I did it myself.
Code:
#include <stdio.h>
#include <stdlib.h>
typedef struct _listnode{
int item;
struct _listnode *next;
} ListNode;
typedef struct _linkedlist{
int size;
ListNode *head;
ListNode *tail;
} LinkedList;
void printList(LinkedList *ll);
ListNode * findNode(LinkedList *ll, int index);
int insertNode(LinkedList *ll, int index, int value);
int removeNode(LinkedList *ll, int index);
int main(){
LinkedList l;
int i=0,j=0,counter=0;
int choice;
LinkedList *ll; int index; int value;
l.head = 0;
l.size = 0;
printf("1. createLinkedList()\n");
printf("2. insertSorted()\n");
printf("3. removeDuplicates()\n");
do {
printf("\nChoose an option : ");
scanf("%d", &choice);
fflush(stdin);
switch (choice) {
case 1:
printf("Enter list : ");
scanf("\n%d", &i);
while(i!=-1) {
insertNode(&l, j, i);
j++;
counter++;
scanf("\n%d", &i);
}
printf("Printing list: \n");
printList(&l);
printf("\ncounter : %d",counter);
printf("\n");
break;
case 2:
printf("Enter number to insert : ");
scanf("\n%d", &i);
insertNode(&l, j, i);
j++;
counter++;
printf("Printing list: \n");
printList(&l);
printf("\ncounter : %d",counter);
printf("\n");
break;
case 3:
removeNode(ll, index);
break;
}
} while (choice < 4);
return 0;
}
void printList(LinkedList *ll){
ListNode *temp = ll->head;
if (temp == NULL)
return;
while (temp != NULL){
printf("%d ", temp->item);
temp = temp->next;
}
}
ListNode * findNode(LinkedList *ll, int index){
ListNode *temp;
if (ll == NULL || index < 0 || index >= ll->size)
return NULL;
temp = ll->head;
if (temp == NULL || index < 0)
return NULL;
while (index > 0){
temp = temp->next;
if (temp == NULL)
return NULL;
index--;
}
return temp;
}
int insertNode(LinkedList *ll, int index, int value){
ListNode *pre, *cur ;
if (ll == NULL || index < 0 || index > ll->size + 1)
return -1;
// If empty list or inserting first node, need to update head pointer
if (ll->head == NULL || index == 0){
cur = ll->head;
ll->head = (ListNode *)malloc(sizeof(ListNode));
if (ll->size == 0)
ll->tail = ll->head;
ll->head->item = value;
ll->head->next = cur;
ll->size++;
return 0;
}
// Inserting as new last node
if (index == ll->size){
pre = ll->tail;
cur = pre->next;
pre->next = (ListNode *)malloc(sizeof(ListNode));
ll->tail = pre->next;
pre->next->item = value;
pre->next->next = cur;
ll->size++;
return 0;
}
// Find the nodes before and at the target position
// Create a new node and reconnect the links
if ((pre = findNode(ll, index-1)) != NULL){
ll->head->item = value;
ll->head->next = cur;
if (ll->head->item) > (value)) {
ll->head->item = value;
ll->head->next = ll->head;
}
/*cur = pre->next;
pre->next = (ListNode *)malloc(sizeof(ListNode));
pre->next->item = value;
pre->next->next = cur;
ll->size++; */
//return 0;
// }
return -1;
}
int removeNode(LinkedList *ll, int index){
ListNode *pre, *cur;
// Highest index we can remove is size-1
if (ll == NULL || index < 0 || index >= ll->size)
return -1;
// If removing first node, need to update head pointer
if (index == 0){
cur = ll->head->next;
free(ll->head);
ll->head = cur;
ll->size--;
if (ll->size == 0)
ll->tail = 0;
return 0;
}
// Find the nodes before and after the target position
// Free the target node and reconnect the links
if ((pre = findNode(ll, index-1)) != NULL){
// Removing the last node, update the tail pointer
if (index == ll->size - 1){
ll->tail = pre;
free(pre->next);
pre->next = NULL;
}
else{
cur = pre->next->next;
free(pre->next);
pre->next = cur;
}
ll->size--;
return 0;
}
return -1;
}