Question

C PROGRAMMING Doubly Linked List For this program you’ll implement a doubly linked list of strings....

C PROGRAMMING

Doubly Linked List For this program you’ll implement a doubly linked list of strings. You must base your code on the doubly linked list implementation given in my Week 8 slides. Change the code so that instead of an ‘int’ each node stores a string (choose a suitable size). Each node should also have a next node pointer, and previous node pointer. Then write functions to implement the following linked list operations:

• A printList function that prints the items in the list starting at the head, one item per line of output.

• An insertSorted function that can be used to insert strings (such as people’s names) and keep the list sorted. You’ll have to find the right place to insert the string according to alphabetical order. You can base your logic on the search code given in my slides. After that you can use the addNode, appendNode, or insertNode functions from my slides, as appropriate.

For example, when the list is empty you can immediately add the first string using addNode (or appendNode): Henry Suppose the next string is “Arun” which is earlier alphabetically. You would search for the node that comes after the new string and find “Henry”. Since this is the first node in the list you add the new one at the front using addNode: Arun Henry Imagine the next string is “Raveena” which comes after all existing nodes. You would reach the end of the list when searching and use insertNode (or appendNode) to add it after the node “Henry”. Arun Henry Raveena If the next string is “Gina” you would search for the first node that comes after the new string and find “Henry”. You add the new node “Gina” after the node that comes 2 before “Henry” using the insertNode function. Arun Gina Henry Raveena Don’t forget to use strcmp to compare strings, and strcpy to copy/assign them! There are several other ways to implement this logic – for example you could write an “insertBefore” function which is not given in my slides.

Main function: Your program’s main function should do the following in the order shown. – Initialize an empty linked list. – Use a loop to input strings from the user and add them to the list by calling insertSorted. The loop should stop when the user types “Done” (case insensitive, use strcasecmp). The word “Done” should not be added to the list. – Print the resulting sorted list. Computational Complexity Analysis: In comments at the top of your main .c file give the computational complexity of your program’s main loop using big-O notation. Consider what happens as the number of strings entered increases, including what happens in insertSorted. Write a couple of sentences to justify your answer. You should write clean modular code as much as possible, making use of functions to avoid code duplication

Homework Answers

Answer #1

Solution - The required code is given below -

In this code, the structure defined is as follows -

struct node {
   char *data;          // to store the array to characters
   struct node *next;       // next pointer to linked list
   struct node *prev;      // prev pointer to linked list
};

There are two functions Insert_to_list() and Print_list().

  1. Insert_to_list() function takes two parameters head and the string to insert. It inserts every string in sorted order. The order is determined by comparing the current string, prev string in linked list and next string in linked list using strcmp() function.
  2. Print_list() takes one parameter to print the list.

In this code i have given input in the code itself but you can also take input values from the user using this code -

char str[20];
scanf("%s",str);
while(1){
   scanf("%s",str);
   if(strcmp("Done",str)==0 || strcmp("done",str)==0)
       break;
   Insert_to_list(&head,str);
}

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

// Structure of doubly linked list

struct node {
   char *data;
   struct node *next;
   struct node *prev;
};

// print the linked list

void Print_list(struct node** head) {
   struct node *ptr = *head;
   printf("\n[ ");
  
   while(ptr != NULL) {      
      printf("%s ",ptr->data);
      ptr = ptr->next;
   }
   printf(" ]\n");
}

// insert element in sorted order in the linked list

void Insert_to_list(struct node** head_ref, char *data) {

// Allocating a new node for linked list and

// initializing it with new string with prev and next NULL
    struct node *newNode = (struct node*) malloc(sizeof(struct node));
    newNode->data = data;
    newNode->next = NULL;
    newNode->prev = NULL;

    struct node* current;
    if (*head_ref == NULL)
        *head_ref = newNode;

    else if (strcmp((*head_ref)->data,newNode->data) >= 0) {
        newNode->next = *head_ref;
        newNode->next->prev = newNode;
        *head_ref = newNode;
    }
    else{
        current = *head_ref;
        while (current->next != NULL && strcmp(current->next->data,newNode->data) < 0)
            current = current->next;
        newNode->next = current->next;
        if (current->next != NULL)
            newNode->next->prev = newNode;
        current->next = newNode;
        newNode->prev = current;
    }
}

void main() {

// Inserting string to linked list.
   struct node* head = NULL;
   Insert_to_list(&head, "One");
   Insert_to_list(&head, "Two");
   Insert_to_list(&head, "Three");
   Insert_to_list(&head, "Four");
   Insert_to_list(&head, "Five");
   printf("The linked list is : \n");

// printing the list
   Print_list(&head);
}

Refer to the screenshot of code and output -

Output -

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
public class DoublyLinkedList { Node Head; // head of Doubly Linked List //Doubly Linked list Node...
public class DoublyLinkedList { Node Head; // head of Doubly Linked List //Doubly Linked list Node class Node { int value; Node prev; Node next; // Constructor to create a new node Node(int d) { value = d; } } // Inserting a node at the front of the list public void add(int newData) { // allocate node and put in the data Node newNode = new Node(newData); // Make the next of new node as head // and previous...
Write C language code for a function that takes a doubly linked list as a parameter...
Write C language code for a function that takes a doubly linked list as a parameter and deletes all the nodes in even positions from the first to the last after displaying the content of each node to the console.
Implement a function that detects if there is a “loop” in a singly linked list. A...
Implement a function that detects if there is a “loop” in a singly linked list. A “loop” is defined as a link from one node to itself or to another node that is before the node in the list. The function may only make one pass through the list. It must return 1 if a “loop” exists; 0 otherwise. Be sure to test your function! You may start with code that you have written for other labs. This is a...
C++ Assumption and Terminology: Forward Linked List FLL has "head" Doubly Linked List DLL has "head"...
C++ Assumption and Terminology: Forward Linked List FLL has "head" Doubly Linked List DLL has "head" and "tail" Each node in FLL has "next" Each node in DLL has "next" and "prev" Q1: Complete the following constructor code for FLL FLL::FLL() { Q2: Complete the FLL insert-front code (which insert node new at the front of FLL) FLL::insert-front( node * new) { Q3: Complete the DLL insert-after code (which insert node new after node p) DLL::insert-after(node *p, node *new) {
I'm working with doubly linked lists in c++, I have written my classes and constructors. I...
I'm working with doubly linked lists in c++, I have written my classes and constructors. I need help with a randomizing method, any guidance or sample code would be appreciated, I'm pretty lost. For the method: void DLL::Random(); I want to basically shuffle/randomize my list. My list is a list of strings (names) if that's important to know. I'm mainly struggling with how to use pointers to prev and next to apply to each node and then move them throughout...
1. Design and implement a CircularLinkedList, which is essentially a linked list in which the next...
1. Design and implement a CircularLinkedList, which is essentially a linked list in which the next reference of the tail node is set to refer back to the head of the list (rather than null) . Start from the SinglyLinkedList provided in the Lecture (not the built-in Java LinkedList class). 2. Starting from  SinglyLinkedList you will need to modify or complete methods: first(), last(), addFirst(), addLast(), removeFirst(), toString(), and also create a new rotate() method which will move the tail to...
USING JAVA LANGUAGE : Using Doubly Linked List, create a java code that does the following...
USING JAVA LANGUAGE : Using Doubly Linked List, create a java code that does the following Without using LinkedList from the JAVA LIBRARY. and please include methods for each function. Create a menu that contains the following operations : 1. Add new node to DLL. ( as a METHOD ) 2. Delete a node from DLL. ( as a METHOD ) 3. Show how many nodes in DLL. ( as a METHOD ) 4. Print all data in the DLL....
Write in Java (Not Javascript) Provide an implementation of priority queue using double-ended doubly linked lists....
Write in Java (Not Javascript) Provide an implementation of priority queue using double-ended doubly linked lists. Recall that double-ended means keeping first and last references and doubly linked feature allows us to go backwards, using a prev reference at each Link. Also, note that the greater the number, the lower the priority. For instance, 2 is of higher priority compared to 5. Specifically, write a class LinkedListPriorityQ which implements the priority queue methods: boolean isEmpty() void enqueue(int item) int dequeue()...
IN JAVA LANGUAGE Linked List-Based Stack Implementation Implement Stack using a Linked List Use the language...
IN JAVA LANGUAGE Linked List-Based Stack Implementation Implement Stack using a Linked List Use the language library LinkedList Stack methods will call the LinkedList methods You can use string as the object Instead of using an array, as the StackLab did, here you will use a Linked List from your language's library. Implement all the methods of Stack : push(), pop(), size(), printStackDown(), etc, using calls to the linked list methods that correspond to the actions need. In the array...
#Linked Lists and Classes #C++ Hi, please use singly linked list method to do this question....
#Linked Lists and Classes #C++ Hi, please use singly linked list method to do this question. Thank you! Here’s the contents of a file called example.cpp: // example.cpp #include "LinkedList.h" #include <iostream> #include <string> using namespace std; int main() { cout << "Please enter some words (ctrl-d to stop):\n"; LinkedList lst; int count = 0; string s; while (cin >> s) { count++; lst.add(remove_non_letters(s)); } // while cout << "\n" << count << " total words read in\n"; cout <<...