Must be Written in C programming! * TODO * func: lst_are_equal * desc: returns 1
ID: 3865307 • Letter: M
Question
Must be Written in C programming! * TODO * func: lst_are_equal * desc: returns 1 if the two given lists are equal: * * they are the same length and * contain the same sequence of values * * returns 0 otherwise * * DIFFICULTY LEVEL: 1 */ extern int lst_are_equal(LIST *lst1, LIST *lst2); /* * function: lst_push_front * desc: self explanatory */ extern void lst_push_front(LIST *l, ElemType val); /* * function: lst_push_back * desc: self explanatory */ extern void lst_push_back(LIST *l, ElemType val); /* * function: lst_len * desc: returns length of list */ extern int lst_len(LIST *l); /* * function: lst_is_empty * desc: returns 1 if list is empty; 0 otherwise */ extern int lst_is_empty(LIST *l); /** [done as part of previous lab] * function: lst_count * description: Counts number of occurrences * of x in the list and returns * that value. */ extern int lst_count(LIST *l, ElemType x); // sanity checker functions - DONE extern int lst_sanity1(LIST *l); extern int lst_sanity2(LIST *l); extern int lst_sanity3(LIST *l); /* * function: lst_pop_front * desc: removes front element from list and returns * its value. * If list is empty, DEFAULT is returned (however, * this value has no real meaning -- caller should * not in general call lst_pop_front on an empty list). */ extern ElemType lst_pop_front(LIST *l);
Explanation / Answer
Sample code:
/*
* C Program to Check whether 2 Lists are Same
*/
#include <stdio.h>
#include <stdlib.h>
struct node
{
int num;
struct node *next;
};
int lst_are_equal (struct node *, struct node *);
void lst_push_front (struct node **, int);
void lst_push_back (struct node **, int);
int lst_len (struct node *);
int lst_is_empty(struct node *);
int lst_count(struct node *, int);
void lst_pop_front(struct node **);
void display(struct node *);
int main()
{
struct node *fList = NULL;
struct node *sList = NULL;
fList->next = NULL;
int result;
//-- Insert element to first list
lst_push_front(&fList, 12);
lst_push_front(&fList, 45);
lst_push_front(&fList, 96);
lst_push_front(&fList, 25);
//-- Display first list element
display(fList);
printf(" ");
//-- Insert element to second list
lst_push_front(&sList, 12);
lst_push_front(&sList, 45);
lst_push_front(&sList, 96);
lst_push_front(&sList, 25);
//-- Display second list element
display(sList);
/*Check whether first and second lists are equal, if equal then display lists are equal otherwise display not equal*/
result = lst_are_equal(fList, sList);
if (result == 1)
{
printf("Lists are equal. ");
}
else
{
printf("Lists are not equal. ");
}
//-- Insert element to back of the first list
lst_push_back(&fList, 12);
//-- Insert element to back of the second list
lst_push_back(&sList, 55);
printf("The first list length %d ", lst_len (fList));
printf("The second list length %d ", lst_len (sList));
printf(" The list after modified: ");
display(fList);
printf(" ");
display(sList);
/*Check whether first and second lists are equal, if equal then display lists are equal otherwise display not equal*/
result = lst_are_equal(fList, sList);
if (result == 1)
{
printf("Lists are equal. ");
}
else
{
printf("Lists are not equal. ");
}
// printf(" 12 occurrences count %d ", lst_count (fList, 12));
lst_pop_front (&fList);
lst_pop_front (&sList);
return 0;
}
int lst_are_equal (struct node *fList, struct node *sList)
{
while (fList != NULL && sList != NULL)
{
if (fList->num != sList-> num)
{
return 0;
}
else
{
fList = fList->next;
sList = sList->next;
}
}
if (fList != NULL || sList != NULL)
{
return 0;
}
else
{
return 1;
}
}
void lst_push_back (struct node **head, int val)
{
struct node *temp;
temp = (struct node *)malloc(sizeof(struct node));
struct node *ptr = *head;
while(ptr->next != NULL)
{
ptr = ptr->next;
}
temp->num = val;
temp->next = ptr->next;
ptr->next = temp;
}
void lst_push_front (struct node **head, int val)
{
struct node *temp;
temp = (struct node *)malloc(sizeof(struct node));
temp->num = val;
temp->next = *head;
*head = temp;
}
int lst_len(struct node * head)
{
int count = 0; // Initialize count
struct node* curList = head; // Initialize current
while (curList != NULL)
{
count++;
curList = curList->next;
}
return count;
}
int lst_is_empty(struct node * head)
{
int lenList = lst_len(head);
if(lenList > 0)
return 0; //-- if not an empty list
else
return 1; //-- If list is empty
}
int lst_count (struct node *head, int x)
{
int occurrencesCount = 0;
while (head != NULL)
{
if (head->num == x)
{
occurrencesCount++;
}
}
return occurrencesCount;
}
void display(struct node *head)
{
struct node *ptr = head;
while(ptr != NULL)
{
printf("%d ", ptr->num);
ptr = ptr->next;
}
}
void lst_pop_front(struct node **head)
{
struct node *temp = *head;
while ((*head) != NULL)
{
(*head) = (*head)->next;
free(temp);
temp = *head;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.