Question Solved1 Answer Consider the following linked list node structure: struct node{ int data; struct node *next; }; Implement the following basic functions of the linear linked list structure: Check empty list int is_empty( struct node *header) { if(header==NULL) return 1; //TRUE else return 0; //FALSE } Create a node struct node * newnode(int d) { struct node *temp; temp=(struct node *) malloc(sizeof(node)); temp->data =d; temp->next=NULL; return temp; } Insert a node into front/back/middle of LL struct node * insertFront(struct node *header, int d) { struct node *temp; temp=newnode(d); temp->next =header; header=temp; return header; } struct node * insertBack(struct node *header, int d) { struct node *temp, *headertemp; temp=newnode(d); if(header==NULL) { header=temp; return header; } headertemp=header; while(headertemp->next!=NULL) headertemp =headertemp->next; headertemp->next=temp; return header; } void insertAfter(struct node *afterNode, int d) { struct node *temp; temp=newnode(d); temp->next=afterNode->next; afterNode->next=temp; } Delete a node from front/back/middle of LL struct node * deleteFront(struct node *header) { struct node *temp; if(header==NULL) return header; temp=header; header= header->next; free(temp); return header; } struct node * deleteBack(struct node *header) { struct node *temp, *headertemp; if(header==NULL) return header; headertemp=header; while(headertemp->next->next!=NULL) headertemp =headertemp->next; temp=headertemp->next; headertemp->next=NULL; free(temp); return header; } void deleteAfter(struct node *afterNode) { struct node *temp; if(afterNode->next==NULL || afterNode==NULL) return; temp =afterNode->next; afterNode->next=temp->next; free(temp); } Task 1: Write down a complete C/C++ program to test your linear linked list implementation. Additionally, write another function which will be used to list the linked list content. Complete your implementation using the following code: header = insertBack(header,2); header = insertBack(header,4); header = insertBack(header,6); DisplayList(header); header = insertFront(header,1); DisplayList(header); insertAfter(header->next->next,5); DisplayList(header); header = deleteFront(header); DisplayList(header); header = deleteBack(header); DisplayList(header); deleteAfter(header->next); DisplayList(header); Task 2: Write a function that inserts a node to nth place of a Linear Linked List. Consider the following prototype: struct node * insertn (struct node * p, int n); Task 3: Write a function that deletes all the nodes in the given linked list. void deletelist (struct node *); Task 4: Write a function that copies a linked list. Source and destination lists are the input parameters to the function. void copylist (struct node *,struct node * );

BDSX1U The Asker · Computer Science

Consider the following linked list node structure:

struct node{

int data;

struct node *next;

};

Implement the following basic functions of the linear linked list structure:

  1. Check empty list

int is_empty( struct node *header)

{

if(header==NULL)

return 1; //TRUE

else

return 0; //FALSE

}

  1. Create a node

struct node * newnode(int d)

{

struct node *temp;

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

temp->data =d;

temp->next=NULL;

return temp;

}

  1. Insert a node into front/back/middle of LL

struct node * insertFront(struct node *header, int d)

{

struct node *temp;

temp=newnode(d);

temp->next =header;

header=temp;

return header;

}

struct node * insertBack(struct node *header, int d)

{

struct node *temp, *headertemp;

temp=newnode(d);

if(header==NULL)

{

header=temp;

return header;

}

headertemp=header;

while(headertemp->next!=NULL)

headertemp =headertemp->next;

headertemp->next=temp;

return header;

}

void insertAfter(struct node *afterNode, int d)

{

struct node *temp;

temp=newnode(d);

temp->next=afterNode->next;

afterNode->next=temp;

}

  1. Delete a node from front/back/middle of LL

struct node * deleteFront(struct node *header)

{

struct node *temp;

if(header==NULL)

return header;

temp=header;

header= header->next;

free(temp);

return header;

}

struct node * deleteBack(struct node *header)

{

struct node *temp, *headertemp;

if(header==NULL)

return header;

headertemp=header;

while(headertemp->next->next!=NULL)

headertemp =headertemp->next;

temp=headertemp->next;

headertemp->next=NULL;

free(temp);

return header;

}

void deleteAfter(struct node *afterNode)

{

struct node *temp;

if(afterNode->next==NULL || afterNode==NULL)

return;

temp =afterNode->next;

afterNode->next=temp->next;

free(temp); }

Task 1: Write down a complete C/C++ program to test your linear linked list implementation. Additionally, write another function which will be used to list the linked list content. Complete your implementation using the following code:

header = insertBack(header,2);

header = insertBack(header,4);

header = insertBack(header,6);

DisplayList(header);

header = insertFront(header,1);

DisplayList(header);

insertAfter(header->next->next,5);

DisplayList(header);

header = deleteFront(header);

DisplayList(header);

header = deleteBack(header);

DisplayList(header);

deleteAfter(header->next);

DisplayList(header);

Task 2: Write a function that inserts a node to nth place of a Linear Linked List. Consider the following prototype:

struct node * insertn (struct node * p, int n);

Task 3: Write a function that deletes all the nodes in the given linked list.

void deletelist (struct node *);

Task 4: Write a function that copies a linked list. Source and destination lists are the input parameters to the function.

void copylist (struct node *,struct node * );

More
See Answer
Add Answer +20 Points
Community Answer
PL7HHS The First Answerer
See all the answers with 1 Unlock
Get 4 Free Unlocks by registration

Task 1 Program Screenshot Sample Output {:[2,4,6,,],[1,2,4,6,],[1,2,4,5,6],[2,4,5,6,],[2,4,5,,],[2,4,,,]:} Program to Copy #include <stdio.h> #include <stdlib.h> struct node { int data; struct node *next; }; //Check empty list int is_empty (struct node *header) { if (header == NULL) return 1;ttt//TRUE else return 0;ttt//FALSE } //Create a node struct node *newnode (int d) { struct node *temp; temp = (struct node *) malloc (sizeof (struct node)); temp->data = d; temp->next = NULL; return temp; } //Insert a node into front/back/middle of LL struct node *insertFront (struct node *header, int d) { struct node *temp; temp = newnode (d); temp->next = header; header = temp; return header; } struct node *insertBack (struct node *header, int d) { struct node *temp, *headertemp; temp = newnode (d); if (header == NULL) { header = temp; return header; } headertemp = header; while (headertemp->next != NULL) headertemp = headertemp->next; headertemp->next = temp; return header; } void insertAfter (struct node *afterNode, int d) { struct node *temp; temp = newnode (d); temp->next = afterNode->next; afterNode->next = temp; } //Delete a node from front/back/middle of LL struct node *deleteFront (struct node *header) { struct node *temp; if (header == NULL) return header; temp = header; header = header->next; free (temp); return header; } struct node *deleteBack (struct node *header) { struct node *temp, *headertemp; if (header == NULL) return header; headertemp = header; while (headertemp->next->next != NULL) headertemp = headertemp->next; temp = headertemp->next; headertemp->next = NULL; free (temp); return header; } void deleteAfter (struct node *afterNode) { struct node *temp; if (afterNode->next == NULL || afterNode == NULL) return; temp = afterNode->next; afterNode->next = temp->next; free (temp); } //Display the content of linked list void DisplayList(struct node *header) { struct node *temp=header; if(is_empty(header)) printf("Linked list is emptyn"); else { while(temp!=NULL) { printf("%d ",temp->data); temp=temp->next; } printf("n"); } } struct node *header=NULL; int main () { header = insertBack(header,2); header = insertBack(header,4); header = insertBack(header,6); DisplayList(header); header = insertFront(header,1); DisplayList(header); insertAfter(header->next->next,5); DisplayList(header); header = deleteFront(header); DisplayList(header); header = deleteBack(header); DisplayList(header); deleteAfter(header->next); DisplayList(header); return 0; } Task 2 //Insert node at n th position struct node * insertn (struct node * head, int n) { struct node *temp1,*temp; tint count,d; tif (n<=0) ttprintf("position is invalidn"); telse t{ ttprintf("Enter key:?"); ttscanf("%d",&d); tttemp=newnode(d); tt ttif (temp==NULL) tttprintf("Node is not creatednInsertion is not possiblen"); ttelse tt{ ttt tttif (n==1) ttt{ tttttemp->next=head; ttttheader=temp; ttt} tttelse ttt{ tttttemp1=head; ttttcount=1; ttttwhile (count<n-1 && temp1!=NULL) tttt{ ttttttemp1=temp1->next; tttttcount++; tttt} ttttif(temp1==NULL) tttttprintf("Linked list is out of rangen"); ttttelse tttt{ ttttttemp->next=temp1->next; ttttttemp1->next=temp; tttt} ttt} tt} t} } //Delete all node in the linked list void deletelist (struct node *head) { struct node *temp=head,*temp1; while(temp!=NULL) { temp1=temp->next; free(temp); temp=temp1; } header=NULL; } Task 3 //Delete all node in the linked list void deletelist (struct node *head) { struct node *temp=head,*temp1; while(temp!=NULL) { temp1=temp->next; free(temp); temp=temp1; } header=NULL; } Task 4 //Copy a linked list from sorce list to destination list void copylist (struct node *src,struct node *des ) { struct node *ptr=src; while(ptr!=NULL) { dest=insertBack(dest,ptr->data); ptr=ptr->next; } } Complete program for Task 2, Task 3, and Task 4 #include <stdio.h> #include <stdlib.h> struct node { int data; struct node *next; }; struct node *header=NULL; struct node *source=NULL; struct node *dest=NULL; //Check empty list int is_empty (struct node *header) { if (header == NULL) return 1;ttt//TRUE else return 0;ttt//FALSE } //Create a node struct n ... See the full answer