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

Problem 1: Create a dynamic list of integers. Your list should be initialized by

ID: 3701150 • Letter: P

Question

Problem 1: Create a dynamic list of integers. Your list should be initialized by the user who enters a set of integers from the keyboard Your program should work as follows. Enteran integer: 6 Do you want to enter another integer(y/n)? y Enter an integer: 3 Do you want to enter another integer(y/n)? y Enter an integer: 7 Do you want to enter another integer (y/n)? n The integers entered are: 63 7 Use the following structure in your code typedef struct node st int x; struct node_s listp; node; Problem 2: Shuffle the list of ints entered by the user Your program should Ask the user to specify the number of element swaps swapCount that will be executed to randomize the list of ints Randomly swaps (the contents of) two nodes of the list swapCount times a) b) To obtain a random number in [0, count] (count is the length of the list) use the following function #include #inc lude #inc lude«nath.h» #include int rand gen(int count) double frac;

Explanation / Answer

program1.c

#include <stdio.h>

#include <stdlib.h>

#include <math.h>

#include <time.h>

typedef struct node_s{

int x;

struct node_s *listp;

} node;

node *head =NULL;

void addList(int number);

void printList();

void addList(int number){

node * temp = head;

node * newnode = (node *)malloc(sizeof(node));

newnode->x = number;

newnode->listp = NULL;

if (head == NULL){

head = newnode;

}

else{

while (temp->listp != NULL){

temp = temp->listp;

}

temp->listp = newnode;

}

}

// This function prints contents of linked list starting from head

void printList()

{

node * temp = head;

while (temp != NULL)

{

printf(" %d ", temp->x);

temp = temp->listp;

}

}

int main(){

int number;

char choice;

do{

printf("Enter an integer ");

scanf("%d", &number);

addList( number);

printf("Do you want to enter an integer(y )");

scanf(" %c", &choice);

}while(choice != 'n');

printList();

return 0;

}

program2.c

#include <stdio.h>

#include <stdlib.h>

#include <math.h>

#include <time.h>

typedef struct node_s{

int x;

struct node_s *listp;

} node;

node *head =NULL;

void addList(int number);

void printList();

int getCount();

int rand_gen(int count);

void swapMain();

int getCount();

void swap(node *pt, int i, int j);

void addList(int number){

node * temp = head;

node * newnode = (node *)malloc(sizeof(node));

newnode->x = number;

newnode->listp = NULL;

if (head == NULL){

head = newnode;

}

else{

while (temp->listp != NULL){

temp = temp->listp;

}

temp->listp = newnode;

}

}

// This function prints contents of linked list starting from head

void printList()

{

node * temp = head;

while (temp != NULL)

{

printf(" %d ", temp->x);

temp = temp->listp;

}

}

int rand_gen(int count){

double frac;

frac = (double)rand()/((double)RAND_MAX+1);

return floor(count * frac); // ranodom number in [0, ccount]

}

void swapMain(){

printf("Enter number of times of swap");

int number;

scanf("%d", &number);

int i =0;

for(i =0; i < number; i++){

int first_index = rand_gen(getCount());

int second_index = rand_gen(getCount());

swap(head,first_index,second_index );

}

}

int getCount()

{

int count = 0; // Initialize count

node* current = head; // Initialize current

while (current != NULL)

{

count++;

current = current->listp;

}

return count;

}

void swap(node *pt, int i, int j){

pt = head;

node *ptr1 = head;

node *ptr2 = head;

int a;

int b;

for( a = 0; a < i; a++){

ptr1 = ptr2->listp;

}

for( a = 0; a < j; a++){

ptr2 = ptr2->listp;

}

int temp = ptr1->x;

ptr1->x = ptr2->x;

ptr2->x = temp;

}

int main(){

int number;

srand(time(NULL));

char choice;

do{

printf("Enter an integer ");

scanf("%d", &number);

addList( number);

printf("Do you want to enter an integer(y )");

scanf(" %c", &choice);

}while(choice != 'n');

printList();

swapMain();

printList();

return 0;

}