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

Refer to the lecture slides about linked lists and work on the following tasks.

ID: 3749576 • Letter: R

Question

Refer to the lecture slides about linked lists and work on the following tasks. a) Write a C program to implement the basic operations of Linked List data structure Declare and initialize the Linked List to add integer 22 as first data in the list: initNode(Node tmpHead, int n) Guides for declaration and initialization struct Node ( int data; Node *next; Node head new Node; head->data22; head->next -NULL b) Implement the following operations as functions i) Display all nodes in the entire list - displayNodes(Node tmpHead) Add two new nodes (33 and 44) to existing list (at the end) addNode(Node *tmpHead, int r) Guides for displaying all nodes and adding new node Node *cur = NULL; cur-tmpHead while (cur) ( Node *newNode-new Node; newNode->data33; cout next == NULL) cur->next-newNode break; { cur = cur->next; c) Implement the following operations as functions i) Add a new node (11) at the front of the list- addFront(Node **tmpHead, int n) Remove a node from the list (at front)-removeFront (Node **tmpHead) Guides for adding new node at the front and removing the first node Node oldHeadtmpHead; if (oldHead->next != NULL) Node *newNode-new Node; newNode->data = n; newNode->nexttmpHead tmpHead -newNode tmpHead oldHead->next; delete oldHead; else cout

Explanation / Answer

//C program

#include<stdio.h>

#include <malloc.h>

typedef struct node{

int data;

struct node * next;

}node;

node * initialise(int n){

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

head->data =n;

head->next =NULL;

return head;

}

void display(node *tmphead){

node *cur =NULL;

cur=tmphead;

while(cur){

printf("%d ",cur->data);

cur=cur->next;

}

printf(" ");

}

void addnode(node *tmphead ,int n){

node *cur =NULL;

cur=tmphead;

while(cur->next)

cur=cur->next;

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

newnode->data=n;

newnode->next=NULL;

cur->next=newnode;

}

void addFront (node **tmphead ,int n){

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

newnode->data=n;

newnode->next=*tmphead;

*tmphead=newnode;

}

void removeFront(node **tmphead){

node *oldhead=*tmphead;

if(oldhead->next!=NULL){

*tmphead=oldhead->next;

free(oldhead);

}

else printf("Removefront() aborted ");

}

int main(){

node *tmphead;

tmphead=initialise(22);

display(tmphead);

addnode(tmphead,33);

addnode(tmphead,44);

display(tmphead);

addFront(&tmphead,11);

display(tmphead);

removeFront(&tmphead);

display(tmphead);

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