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

#include<stdio.h> #include<stdlib.h> struct node { int val; struct node *next; }

ID: 3693762 • Letter: #

Question

#include<stdio.h>

#include<stdlib.h>

struct node

{

int val;

struct node *next;

};

//initializing

struct node *head = NULL;

struct node *curr = NULL;

struct node* create_list(int val){

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

if(NULL == ptr){

printf(" Node creation failed ");

return NULL;

}

//pointing to different values

ptr->val = val;

ptr->next = NULL;

head = curr = ptr;

return ptr;

}

struct node* add_to_list(int val){

if(NULL == head){

return (create_list(val));

}

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

if(NULL == ptr){

printf(" Node creation failed ");

return NULL;

//to print a statement if the list is empty/NULL

}

ptr->val = val;

ptr->next = NULL;

curr->next = ptr;

curr = ptr;

return ptr;

}

//printing the list in order from 0-20

void print_list(void){

struct node *ptr = head;

//printf(" -------Printing list Start------- ");

int i=0;

while(ptr != NULL){

printf("Node #%d contains %d ",i,ptr->val);

ptr = ptr->next;

i++;

}

//printf(" -------Printing list End------- ");

return;

}

//pointing to the next value to add them all together

void sum_list(void){

struct node *ptr = head;

int sum_total =0;

while(ptr != NULL){

sum_total += ptr->val;

ptr = ptr->next;

}

printf(" The sum of all nodes in this list is %d ",sum_total);

return;

}

int main(void)

{

int i = 0, ret = 0;

struct node *ptr = NULL;

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

add_to_list(i);

print_list();

sum_list();

return 0;

}

Searching through a Linked List Build a linked list of 100 nodes, each containing an integer between 0 and 99. Each node will contain a unique integer and all integers between 0 and 99 will be included in the list. This can be done in a monotonically-increasing manner as was for HW #6 (except it will be 100 nodes rather than 21). Search the list for the node that contains a particular value entered by the user from the keyboard, and returns the location in the linked list where the sought number (the key) was found. Assume the list is unordered (which of course, is not the case here), and search the entire linked list until the node containing the desired number is found. For output, print out a line stating that the value sought was found the nth node. For example: Please enter the number to be sought in the list: >15 The number 15 was found in node #15 Note that the list must be implemented as a linked list and NOT as an array!

Explanation / Answer

#include<stdio.h>

#include<conio.h>

struct list

{

int n;

struct list *link;

};

struct list *p,*q;

struct list *start=NULL;

void construct()

{

int no;

if(start==NULL)

   {

   p=(struct list *)malloc(sizeof(struct list));

   printf(" enter element:");

   scanf("%d",&no);

   start=p;

   p->n=no;

   p->link=NULL;

   }

   else

   {

   q=(struct list *)malloc(sizeof(struct list));

   printf(" enter element:");

   scanf("%d",&no);

   q->n=no;

   q->link=NULL;

   p->link=q;

   p=q;

   }

}

void display()

{

q=p=start;

for(;q!=NULL;)

{

printf(" %d",q->n);

q=p->link;

p=q;

}

}

void insbeg()

{

   int no;

   p=(struct list *)malloc(sizeof(struct list));

   printf(" enter element to insert at the begining:");

   scanf("%d",&no);

   p->n=no;

   p->link=NULL;

   q=start;

   start=p;

   p->link=q;

}

void inspos()

{

   int no,n;

   struct list *m;

   m=(struct list *)malloc(sizeof(struct list));

   printf(" enter element to insert at specified position:");

   scanf("%d",&no);

   m->n=no;

   m->link=NULL;

   printf(" enter after which element where we insert a new element:");

   scanf("%d",&n);

   p=q=start;

   for(;q!=NULL;)

   {

   if(q->n==n)

    break;

   else

   {

   q=p->link;

   p=q;

   }

   }

m->link=p->link;

q->link=m;

}

void delloc()

{

   int n,i;

   printf(" enter location no to delete the node:");

   scanf("%d",&n);

   p=q=start;

   if(n==1)

    start=p->link;

    else

    {

   for(i=1;q->link!=NULL;i++)

   {

   if(i==n-1)

    {

    break;

    }

   else

   {

   q=p->link;

   p=q;

   }

   }

   p=p->link;

   q->link=p->link;

   }

   }

void delitem()

{

   int n;

   struct list *temp;

   printf(" enter element to delete:");

   scanf("%d",&n);

   p=q=start;

   if(p->n==n)

    start=p->link;

   else

   {

   for(;q!=NULL;)

   {

   if(q->n==n)

    break;

   else

   {

   temp=p;

   q=p->link;

   p=q;

   }

   }

temp->link=p->link;

}

}

void menu()

{

printf(" 1.construct a list");

printf(" 2.insert a node at the beginning");

printf(" 3.insert a node at the specified position");

printf(" 4.delete a node from a given position");

printf(" 5.delete a node by specified item");

printf(" 6.display");

printf(" 7.exit");

}

void main()

{

int ch;

clrscr();

menu();

while(1)

{

printf(" enter choice:");

scanf("%d",&ch);

switch(ch)

{

   case 1:construct();break;

   case 2:insbeg();break;

   case 3:inspos();break;

   case 4:delloc();break;

   case 5:delitem();break;

   case 6:display();break;

   case 7:exit(0);

   }

}

}