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

Using pointers and structures, create a linked list of integers. Put integers in

ID: 3546516 • Letter: U

Question

Using pointers and structures, create a linked list of integers. Put integers into this list and have a pointer to the head of the list and the last element in the linked list have a next pointer of null.

1) Build a routine to create the list (it need only have 5 values to keep things simple).

2) Write a simple routine that receives a pointer to the first element of the list and prints all of the elements in the list.

3) Write a routine that finds the largest element in the list and returns that integer value.

In #C

Explanation / Answer

#include<stdio.h>

#include<malloc.h>

struct node

{

int info;

struct node * next;

};

typedef struct node node;

node *list=NULL;


//Insertion at the beginning

node *insert(int item)

{

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

ptr->info=item;

if(list==NULL)

{

ptr->next=NULL;

list=ptr;

}

else

{

ptr->next=list;

list=ptr;

}

return list;

}


//Display function to display the list

void display(node *list)

{

if(list==NULL)

{

printf(" List is empty ");

exit(0);

}

else

{

node *ptr=list;

while(ptr->next!=NULL)

{

printf(" %d -> ",ptr->info);

ptr=ptr->next;

}

printf(" %d",ptr->info);

}

}


int largest(node * list,)

{

int count=1;

node *ptr=list;

int max=ptr->info;

while(ptr->next!=NULL)

{

if(max<ptr->info)

max=ptr->info;

ptr=ptr->next;

}

  

return max;

}


main()

{

int num,x,i,k;

printf("Enter the number of elements in the linked list : ");

scanf("%d",&num);

for(i=0; i<num; i++)

{

printf(" Enter the element %d : ",i+1);

scanf("%d",&x);

list=insert(x);

}

display(list);

int large=largest(list);

printf("%d",large);

return 0;

}

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