I need halp finishing this assignment. This is done in C language. I need helpin
ID: 3670133 • Letter: I
Question
I need halp finishing this assignment. This is done in C language. I need helping with the instructions and printing the link list from start to end. The instrcutions are below and my code is under the instrcutions. Can anyone help me finish all of it? The previous anyswer did not help me finish assignment. Thanks.
CS 2123
Data Structures Assignment 2
Due Friday February 19
1. (100 pts) Write a program to implement the following functions on linked lists. Assume that node structure of a singly linked list is as follows.
struct node {
int info;
and node structure of a doubly linked list is as follows
Implement the below functions whose prototypes are given below
CopytoSinglyLinked function makes a singly linked copy of a doubly linked list that is provided as a parameter and returns a pointer to the singly linked list.
Previous function returns the previous node of current in a singly linked list pointed by head. If current is the first node Previous returns null.
PrintReverse function prints a elements of a singly linked list in reverse order. This should be implemented as an iterative function. Use Previous function in your imple- mentation.
RemoveDuplicates function removes duplicate elements in a singly linked list that is provided as a parameter. The contents of the list need not be sorted. You should remove duplicates in an unsorted list without changing the order of elements in the list.
MY CODE IS BELOW
#include <stdio.h>
#include <stdlib.h>
struct node
{
int info;
struct node *next;
};
typedef struct node node;
//node structure of a doubly linked list is as follows
struct cnode
{
int info;
struct cnode *next;
struct cnode *previous;
};
typedef struct cnode cnode;
node* copytoSinglyLinked(cnode* head){ // Copy a Doubly_linklist to Single_linklist
node* h = new node;
h->info = head->info;
node* temp = h;
cnode* temp_1 = head->next;
while (temp_1 != NULL){ // iterarate each node and add into the list
node* temp_2 = new node();
temp_2->info = temp_1->info;
temp->next = temp_2;
temp = temp_2;
temp_1 = temp_1->next;
}
return h;
}
node* previous(node* head,node* current){
node* prev = NULL;
node* temp = head;
while (temp != NULL || temp != current){
prev = temp;
temp = temp->next;
}
return prev;
}
void PrintReverse(node* head){
if (head == NULL) return;
node* temp = head;
while (temp->next != NULL){
temp = temp->next;
}
while (temp != NULL){
printf("%d ",temp->info);
temp = previous(head, temp);
}
printf(" ");
}
node* RemoveDuplicate(node* head){
map<int,int> mymap;
node* temp = head;
mymap[temp->info] = 0;
while (temp->next != NULL){
if (mymap.find(temp->next->info) == mymap.end())
temp = temp->next;
else{
temp->next = temp->next->next;
temp = temp->next;
}
}
return head;
}
int main(void){
PrintReverse(head);
}
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
struct node
{
int info;
struct node *next;
};
typedef struct node node;
//node structure of a doubly linked list is as follows
struct cnode
{
int info;
struct cnode *next;
struct cnode *prev;
};
typedef struct cnode cnode;
void push(struct cnode** head_ref, int new_data)
{
/* allocate cnode */
struct cnode* new_cnode =
(struct cnode*) malloc(sizeof(struct cnode));
/* put in the data */
new_cnode->info = new_data;
/* since we are adding at the begining,
prev is always NULL */
new_cnode->prev = NULL;
/* link the old list off the new cnode */
new_cnode->next = (*head_ref);
/* change prev of head cnode to new cnode */
if((*head_ref) != NULL)
(*head_ref)->prev = new_cnode ;
/* move the head to point to the new cnode */
(*head_ref) = new_cnode;
}
node* copytoSinglyLinked(cnode* head){ // Copy a Doubly_linklist to Single_linklist
node* h = (struct node*) malloc(sizeof(struct node));
h->info = head->info;
node* temp = h;
cnode* temp_1 = head->next;
while (temp_1 != NULL){ // iterarate each node and add into the list
node* temp_2 = (struct node*) malloc(sizeof(struct node));
temp_2->info = temp_1->info;
temp->next = temp_2;
temp = temp_2;
temp_1 = temp_1->next;
}
return h;
}
node* previous(node* head,node* current){
node* prev = NULL;
node* temp = head;
while (temp != current){
prev = temp;
temp = temp->next;
}
return prev;
}
void printList(struct node *node)
{
while(node!=NULL)
{
printf("%d ", node->info);
node = node->next;
}
}
void PrintReverse(node* head){
if (head == NULL) return;
node* temp = head;
while (temp->next != NULL){
temp = temp->next;
}
while (temp != NULL){
printf("%d ",temp->info);
temp = previous(head, temp);
}
printf(" ");
}
void RemoveDuplicate(struct node *start)
{
struct node *ptr1, *ptr2, *dup;
ptr1 = start;
/* Pick elements one by one */
while(ptr1 != NULL && ptr1->next != NULL)
{
ptr2 = ptr1;
/* Compare the picked element with rest of the elements */
while(ptr2->next != NULL)
{
/* If duplicate then delete it */
if(ptr1->info == ptr2->next->info)
{
/* sequence of steps is important here */
dup = ptr2->next;
ptr2->next = ptr2->next->next;
free(dup);
}
else /* This is tricky */
{
ptr2 = ptr2->next;
}
}
ptr1 = ptr1->next;
}
}
int main(void){
struct cnode* dhead = NULL;
struct node* shead = NULL;
struct node* shead_temp = NULL;
/* Let us create a sorted linked list to test the functions
Created linked list will be 10->8->4->2 */
push(&dhead, 2);
push(&dhead, 2);
push(&dhead, 8);
push(&dhead, 10);
shead = copytoSinglyLinked(dhead);
shead_temp = previous(shead,shead->next);
printList(shead_temp);
PrintReverse(shead);
RemoveDuplicate(shead);
PrintReverse(shead);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.