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

1-Write a method that inserts a node at the beginning of the linked list and a m

ID: 3620192 • Letter: 1

Question

1-Write a method that inserts a node at the beginning of the linked list and a method that deletes the first node of the linked list

2-Repeat the above , but assume that the list has a tail reference as well as a head reference

Explanation / Answer

#include struct node{ int value; struct node *next; }*head; void insert(){ int no; struct node *temp; printf(" Enter a number: "); scanf("%d",&no); temp = (struct node *)malloc(sizeof(struct node)); temp->value=no; temp->next=NULL; if(head==NULL){ head=temp; } else{ temp->next=head; head=temp; } } void delete1(){ struct node * temp; if(head!=NULL){ temp=head; head=head->next; free(temp); } } void print(struct node *temp){ while(temp!=NULL){ printf("-->%d",temp->value); temp=temp->next; } } int main(){ int ch=1; while(1){ printf(" 1.Insert 2.Delete 3.Print 4.Exit Enter choice:"); scanf("%d",&ch); switch(ch){ case 1: insert();break; case 2: delete1();break; case 3: print(head);break; case 4: exit(0); } } }