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
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...
C++ Goals  Build single linked lists using pointers  Learn how to manipulate linked lists...
C++ Goals  Build single linked lists using pointers  Learn how to manipulate linked lists In this lab, you will create simple single linked structures consisting of Node objects. Each node will have a pointer to the next node. You will use a head pointer to keep track of the first node in the linked list, and a tail pointer to keep track of the last node in the linked list. Set both head and tail to NULL when...
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 <<...
Write a C program: Implement the abstract data type (ADT) queue (FIFO) of strings. ADT has...
Write a C program: Implement the abstract data type (ADT) queue (FIFO) of strings. ADT has the following methods: clear – clears the queue; is_empty – returns 1 if the queue is empty, otherwise 0; is_full – returns 1 if the queue is full, otherwise 0; add – adds a new string at the end of the queue; remove – removes the string from the front of the queue. Use ADT queue to solve the following problems: • Write an...
Data Structures using C++ Searching a Linked List Here are the declarations for a simple unsorted...
Data Structures using C++ Searching a Linked List Here are the declarations for a simple unsorted linked list of ints that ends in a null pointer. //=============================================================== class Cell { friend class UList; private: int data; Cell* next; Cell( int dt, Cell* nx=nullptr ) : data(dt), next(nx) {} }; //=============================================================== class UList { private: Cell* head = nullptr;    // stationary head pointer. Cell* scan = nullptr;          // for walking down the List. Cell* follow = nullptr; public: void find( int...
8.19 LAB: Grocery shopping list (linked list: inserting at the end of a list) PLEASE ANSWER...
8.19 LAB: Grocery shopping list (linked list: inserting at the end of a list) PLEASE ANSWER IN C++ Given main(), define an InsertAtEnd() member function in the ItemNode class that adds an element to the end of a linked list. DO NOT print the dummy head node. Ex. if the input is: 4 Kale Lettuce Carrots Peanuts where 4 is the number of items to be inserted; Kale, Lettuce, Carrots, Peanuts are the names of the items to be added...
It is about C++linked list code. my assignment is making 1 function, in below circumstance,(some functions...
It is about C++linked list code. my assignment is making 1 function, in below circumstance,(some functions are suggested for easier procedure of making function.) void search_node(struct linked_list* list, int find_node_ value) (The function to make) This function finds the node from the list that value is same with find_node_value and count the order of the node. This function should print message “The order of (node_value) is (order).” and error message “Function search_node : There is no such node to search.”....
In this code, I build a single-linked list using a node class that has been created....
In this code, I build a single-linked list using a node class that has been created. How could I change this code to take data of type T, rather than int. (PS: ignore the fact that IOHelper.getInt won't work for the type T... ie second half of main). Here's my code right now: public class SLList { public SLNode head = null; public SLNode tail = null; public void add(int a) {// add() method present for testing purposes SLNode newNode...