Doubly Linked List #2
Time limit: 5000ms
Memory limit: 256mb
Description:
Complete the following program.
/*
I, <Your Full Name>, am submitting the assignment for
an individual project.
I declare that the assignment here submitted is original except for
source material explicitly acknowledged, the piece of work, or a part
of the piece of work has not been submitted for more than one purpose
(i.e. to satisfy the requirements in two different courses) without
declaration. I also acknowledge that I am aware of University policy
and regulations on honesty in academic work, and of the disciplinary
guidelines and procedures applicable to breaches of such policy and
regulations, as contained in the University website
http://www.cuhk.edu.hk/policy/academichonesty/.
It is also understood that assignments without a properly signed
declaration by the student concerned will not be graded by the
teacher(s).
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
typedef int element;
typedef struct node *nodePointer;
struct node {
nodePointer llink;
element data;
nodePointer rlink;
};
nodePointer dcreate(element data) {
nodePointer node = malloc(sizeof(struct node));
node->llink = node;
node->data = data;
node->rlink = node;
return node;
}
void dinsert(nodePointer node, nodePointer newnode) {
/* insert newnode to the right of node */
newnode->llink = node;
newnode->rlink = node->rlink;
node->rlink->llink = newnode;
node->rlink = newnode;
}
void ddelete(nodePointer node, nodePointer deleted) {
/* delete from the doubly linked list */
if (node == deleted)
printf("Deletion of header node not permitted.\n");
else {
deleted->llink->rlink = deleted->rlink;
deleted->rlink->llink = deleted->llink;
free(deleted);
}
}
void print(nodePointer node) {
for (nodePointer it = node->rlink; it != node; it = it->rlink) {
printf("%d\n", it->data);
}
}
void delete_duplicate_elements(nodePointer node);
int main() {
int n;
scanf("%d", &n);
nodePointer list = dcreate(-1); // This is the header node
for (int i = 0; i < n; i++) {
int data;
scanf("%d", &data);
dinsert(list, dcreate(data)); // Insert a new node before the head of the list
}
print(list);
puts("===");
delete_duplicate_elements(list);
print(list);
}
/* Please keep the code above unchanged.
* You can only edit the following code.
* The textbook can be found at
* https://library-books24x7-com.easyaccess2.lib.cuhk.edu.hk/assetviewer.aspx?bookid=21597
*/
// For consecutive element with the same value, delete all the duplicates and
// keep only one of them.
void delete_duplicate_elements(nodePointer node) {
// WRITE YOUR OWN CODE HERE
}
Sample input:
8
1 1 2 2 1 3 3 3
Sample output:
3
3
3
1
2
2
1
1
===
3
1
2
1
Submit