double linked list with c

1549 views c
1

ok, now i have to make double linked list on c. there are 7 functions which act on main.

append. insertAt. deleteAt. print. print_revers. newnode. newDLL.

i can amend only 5 functions append, insertAt, deleteAt, print, print_reverse

finally i can make append,print,print_reverse however, i can't make insertAt,deleteAt, because of index.

1. i can't understand why the code

else {
    while (index-- >= 0) {
      temp = temp->next;
    }

make memory collide. for using index, i need to move node to collect position and connect to newnode. but it doesn't work...

2. also what's return; 's role? i have not seen such type of return.

3. how can i make deleteAt using index? i think deleteAt and insertAt have quiet similar algoritum. so i try to make insertAt first and deleteAt last. but what i write doesn't work well..

i can find a lot of data of doublelinkedlist on internet. but i can't find using index.... i have been listening c language lecture on only two month, so sorry about the spagettii code...

#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
    int val;
    struct Node *prev;
    struct Node *next;
} Node;

typedef struct {
    Node *head;
    int size;
} DLL;

Node *newnode(int n)
{
    Node *temp = (Node *)malloc(sizeof(Node));
    temp->val = n;
    temp->prev = NULL;
    temp->next = NULL;
    return temp;
}

DLL *newDLL() {
    DLL *temp = (DLL *)malloc(sizeof(DLL));
    temp->head = NULL;
    temp->size = 0;
    return temp;
}

void append(DLL *list, Node *newnode) {
    struct Node* temp = list->head;
    struct Node* newNode = newnode;
    if (list->head == NULL) {
        list->head = newNode;
        list->size++;
        return;
    }
    while (temp->next != NULL) {
        temp = temp->next;
    }
    list->size++;
    temp->next = newNode;
    newNode->prev = temp;
}

void insertAt(DLL *list, int index, Node *newnode) {

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

    if (index < 0 || index >= list->size + 1) {
        printf("out of range\n");
    }
    else if (index == 0) {
        newnode->next = list->head;
        list->head->prev = newnode;
        list->head = newnode;
    }
    else {
        while (index-- >= 0) {
          temp = temp->next;
        }

    temp->val = newnode->val;
    temp->next = list->head->next;
    list->head->next = temp;
    temp->prev = list->head;

    if (temp->next != NULL)
        temp->next->prev = temp;
    }
}

void deleteAt(DLL *list, int index) {
    //save reference to first link

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

    //if only one link
    if (list->head->next == NULL) {
        list->head->prev = NULL;
    }
    else {
        list->head->next->prev = NULL;
    }

    list->head = list->head->next;
    //return the deleted link
    return;
}

void print(DLL *list) {
    struct Node* temp = list->head;
    printf("Forward: ");
    while (temp != NULL) {
        printf("[%d] ", temp->val);
        temp = temp->next;
    }
    printf("\n");
}

void print_reverse(DLL *list) {
    struct Node* temp = list->head;
    if (temp == NULL) return; // empty list, exit
    // Going to last Node
    while (temp->next != NULL) {
        temp = temp->next;
    }
    // Traversing backward using prev pointer
    printf("Reverse: ");
    while (temp != NULL) {
        printf("[%d] ", temp->val);
        temp = temp->prev;
    }
    printf("\n");
}

int main() {
    DLL *list = newDLL();
    int i;
    for (i = 1; i < 6; i++) {
        append(list, newnode(i));
    }

    print(list);
    deleteAt(list, -1);
    deleteAt(list, 5);
    deleteAt(list, 0);
    print(list);
    deleteAt(list, 2);
    print(list);
    deleteAt(list, 2);
    print(list);
    insertAt(list, -1, newnode(6));
    insertAt(list, 3, newnode(6));
    insertAt(list, 0, newnode(7));
    print(list);
    insertAt(list, 1, newnode(8));
    print(list);
    insertAt(list, 4, newnode(9));
    print(list);
    print_reverse(list);

    return 0;
}

answered question

1 Answer

7

The part where you insert at index has problems:

temp = malloc is wrong, it should start with temp = head.

in the insertion:

temp->val = newnode->val;
temp->next = list->head->next;
list->head->next = temp;
temp->prev = list->head;

temp->next should not be head->next, it should be newnode. newnode->next should be temp->next etc.

posted this

Have an answer?

JD

Please login first before posting an answer.

Ads

Categories