bool isEmpty() const ; //is list empty int getLength() const ; //lenth of list v
ID: 3654208 • Letter: B
Question
bool isEmpty() const; //is list empty
int getLength() const; //lenth of list
void insertFront(ListItemType newItem); //insert a new node at the front
void insertBack(ListItemType newItem); //insert a new node at the back
void removeFront( ListItemType& dataItem);//remove a node from the front and return node value
void removeBack( ListItemType& dataItem); //remove a node from the end and return node value
void printList(); //print linked list in a form of list 10 5
List operator=(const List& aList); //assignment operator overloading for list1=list2
private:
Explanation / Answer
struct Node { int Data; struct Node *Next; }*Head; void addBeg(int num) { struct Node *temp; temp=(struct Node *)malloc(sizeof(struct Node)); temp->Data = num; if (Head == NULL) { //List is Empty Head=temp; Head->Next=NULL; } else { temp->Next=Head; Head=temp; } } //Adding a Node at the end of the list void addEnd(int num) { struct Node *temp1, *temp2; temp1=(struct Node *)malloc(sizeof(struct Node)); temp1->Data=num; // Copying the Head location into another node. temp2=Head; if(Head == NULL) { // If List is empty we create First Node. Head=temp1; Head->Next=NULL; } else { // Traverse down to end of the list. while(temp2->Next != NULL) temp2=temp2->Next; // Append at the end of the list. temp1->Next=NULL; temp2->Next=temp1; } } // Adding a new Node at specified position void addAt(int num, int loc) { int i; struct Node *temp, *prev_ptr, *cur_ptr; cur_ptr=Head; if(loc > (length()+1) || loc Data=num; prev_ptr->Next=temp; temp->Next=cur_ptr; } } } // Counting number of elements in the List int length() { struct Node *cur_ptr; int count=0; cur_ptr=Head; while(cur_ptr != NULL) { cur_ptr=cur_ptr->Next; count++; } return(count); } // Deleting a node from List depending upon the data in the node. int delNodeData(int num) { struct Node *prev_ptr, *cur_ptr; cur_ptr=Head; while(cur_ptr != NULL) { if(cur_ptr->Data == num) { if(cur_ptr==Head) { Head=cur_ptr->Next; free(cur_ptr); return 0; } else { prev_ptr->Next=cur_ptr->Next; free(cur_ptr); return 0; } } else { prev_ptr=cur_ptr; cur_ptr=cur_ptr->Next; } } printf(" Element %d is not found in the List", num); return 1; } // Deleting a node from List depending upon the location in the list. int delNodeLoc(int loc) { struct Node *prev_ptr, *cur_ptr; int i; cur_ptr=Head; if(loc > (length()) || loc Next; free(cur_ptr); return 0; } else { for(i=1;iNext; } prev_ptr->Next=cur_ptr->Next; free(cur_ptr); } } return 1; } // Displaying list contents void display() { struct Node *cur_ptr; cur_ptr=Head; if(cur_ptr==NULL) { printf(" List is Empty"); } else { printf("Elements in the List: "); //traverse the entire linked list while(cur_ptr!=NULL) { printf(" -> %d ",cur_ptr->Data); cur_ptr=cur_ptr->Next; } printf(" "); } } //Reversesing a Linked List void reverse() { struct Node *prev_ptr, *cur_ptr, *temp; cur_ptr=Head; prev_ptr=NULL; while(cur_ptr != NULL) { temp=prev_ptr; prev_ptr=cur_ptr; cur_ptr=cur_ptr->Next; prev_ptr->Next=temp; } Head=prev_ptr; }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.