0% found this document useful (0 votes)
69 views

Linked List Assignment

The document discusses various operations on linked lists such as creating a linked list from an array, inserting and deleting nodes, counting and traversing nodes, reversing the list, and calculating the sum of node values. Code snippets are provided to demonstrate recursive and iterative implementations for traversing, counting, reversing, and summing the linked list. The document is authored by Riddhi More for a data structures assignment on linked lists.

Uploaded by

Sachin More
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views

Linked List Assignment

The document discusses various operations on linked lists such as creating a linked list from an array, inserting and deleting nodes, counting and traversing nodes, reversing the list, and calculating the sum of node values. Code snippets are provided to demonstrate recursive and iterative implementations for traversing, counting, reversing, and summing the linked list. The document is authored by Riddhi More for a data structures assignment on linked lists.

Uploaded by

Sachin More
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Data Structure Linked List Riddhi More, Roll no.

33

DATA STRUCTURE
Linked List

Riddhi More, Roll No. 33


30th Sep 2020
Data Structure Linked List Riddhi More, Roll no.33

Data Structure – Assignment


Linked Lists

/* creating a linked List from an Array */

void create (int A[], int n) {

    int i;
    struct Node *t, *last;
    first = (struct Node *)malloc(sizeof(struct Node));
    first -> data = A[0];
    first -> next = NULL;
    last = first;

    for (i = 1; i < n; i ++) {
        t = (struct Node*)malloc(sizeof(struct Node));
        t -> data = A[i];
        t -> next = NULL;
        last -> next = t;
        last = t;
    }
}

/* Display function */

void Display(struct Node *p) {
    while (p != NULL)
    {
        printf("%d ",p -> data);
        p = p -> next;/* code */
    } 
}

/* A recursive approach to display */

void Display_r(struct Node *p) {

    if (p != NULL) {
        printf("%d ", p -> data);
        Display_r (p ->next);
    }
}
Data Structure Linked List Riddhi More, Roll no.33

Q1. Inserting an element in the list after a specified node.


/* the function countNode() is defined further in Q3. */

void insertNode (struct Node *p, int index, int x) {
    
    struct Node *t;
    
    /* check vaidity of index*/
    if (index < 0 || index > countNode(p))
        return;

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


    t -> data = x;

    /* for inserting before first node */
    if (index == 0) {
        t -> next = first;
        first = t;
    }
    /* for inserting other than before first node*/
    else {
        for (int i = 0; i < index - 1; i ++)
            p = p -> next;
        t -> next = p -> next;
        p -> next = t;
    }
}

Q2. Deleting an element from the list after the nth node.
/* deleting node after given index*/

int deleteAfterNode (struct Node *p, int index) {
    struct Node *q = NULL;
    int x = -1;

    /*validate index*/
    if (index < 0 || index > (countNode(p) + 1))
        return -1;
    
    for (int i = 0; i < index; i ++) {
        q = p;
        p = p -> next;
    }
    q -> next = p -> next;
    x = p -> data;
    free(p);
    return x;
}
Data Structure Linked List Riddhi More, Roll no.33

Q3. Counting the number of nodes in the list

int countNode (struct Node *p) {
    int counter = 0;
    while (p != NULL) {
        counter ++;
        p = p -> next;
    }

    return counter;
}

/* A recursive approach for counting nodes in the list */

int countNode_r (struct Node *p) {
    if (p == 0)
        return 0;
    else countNode_r(p -> next) + 1;
}

Q4. Reversing the linked list.


/*reversing by element: copy element to new array
and insert in linked list in reverse order.*/
void reverseByelement(struct Node *p) {
    int *A, i = 0;
    struct Node *q = p;

    A = (int *)malloc(sizeof(int)*countNode(p));
   /*copying*/
    while (q != NULL) {
        A[i] = q -> data;
        q = q -> next;
        i ++;
    }
    q = p;
    i --;
    while (q != NULL) {
        q -> data = A[i];
        q = q -> next;
        i --;
    }
}
Data Structure Linked List Riddhi More, Roll no.33

/* reversing links: Sliding Pointer method */

void reverseBySlide(struct Node *p) {
    struct Node *q = NULL,*r = NULL;

    while(p != NULL) {
    r = q;
    q = p;
    p = p -> next;
    q -> next = r;
 }
    first = q;
}

/*reversing link by recursive method*/

void reverseList_r(struct Node *q,struct Node *p) {
    if (p) {
        reverseList_r(p, p -> next);
        p->next=q;
    }
    else
    first = q;
}

Q5. Sum of the data in the linked list.


int addNodes (struct Node *p) {
    int sum = 0;
    while (p) {
        sum = sum + p -> data;
        p = p -> next;
    }
    return sum;
}

You might also like