-4

Create a program to store a song playlist using linked-list. The program should be able to:

  1. Insert a new song at the front of the playlist
  2. Insert a new song at the back of the playlist
  3. Insert a new song in between the playlist
  4. Delete any song in the playlist
  5. Display the playlist

I mentioned the errors using comments

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

struct node
{
  char str[];
  struct node* next;
} *

void insertAtBeginning(struct Node** ref, char data[]) //expecte indentifier  or '(' before 'void'
{
  // Allocate memory to a node
  struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));

  // insert the item
  new_node->item = data;
  new_node->next = (*ref);

  // Move head to new node
  (*ref) = new_node;
}

// Insert a node after a node
void insertAfter(struct Node* node, char data[])
{
  if (node == NULL)
  {
    printf("the given previous node cannot be NULL");
    return;
  }

  struct Node* new_node = (struct Node*)malloc(sizeof(struct Node)); //error: invalid application of 'sizeof' to incomplete type 'struct Node'
  new_node->item = data; // error: dereferencing pointer to imcomplete type 'struct Node'
  new_node->next = node->next;
  node->next = new_node;
}

void insertAtEnd(struct Node** ref, char data[
])
{
  struct Node* new_node = (struct Node*)malloc(sizeof(struct Node)); // error: invalid application of 'sizeof' to imcomplete type 'struct Node'
  struct Node* last = *ref;

  new_node->item = data; // error: dereferencing pointer to imcomplete type 'struct Node'
  new_node->next = NULL;

  if (*ref == NULL)
  {
    *ref = new_node;
    return;
  }

  while (last->next != NULL)
    last = last->next;

  last->next = new_node;
  return;
}

void deleteNode(struct Node** ref, char key[])
{
  struct Node* temp = *ref, * prev;

  if (temp != NULL && temp->item == key) // error: dereferencing pointer to imcomplete type 'struct node'
  {
    *ref = temp->next;
    free(temp);
    return;
  }
  // Find the key to be deleted
  while (temp != NULL && temp->item != key)
  {
    prev = temp;
    temp = temp->next;
  }

  // If the key is not present
  if (temp == NULL)
    return;

  // Remove the node
  prev->next = temp->next;

  free(temp);
}

// Print the linked list
void printList(struct Node* node)
{
  while (node != NULL)
  {
    printf(" %s ", node->item); // error: dereferencing the pointer to imcomplete type 'struct Node'
    node = node->next;
  }
}

int main()
{
  char song[50]; 
  struct Node* head = NULL;

  insertAtEnd(&head, "red");
  insertAtBeginning(&head, "dangerously");
  insertAtBeginning(&head, "attention");
  insertAtEnd(&head, "blue");
  insertAfter(head->next, 5); // error: dereferencing the pointer to imcomplete type 'struct Node'

  printf("Linked list: ");
  printList(head);

  printf("\nAfter deleting an element: ");
  deleteNode(&head, "random");
  printList(head);
}
sss
  • 1
  • 2

1 Answers1

1

Your program does not even compile, there are many issues.

The most important ones:

  1. You declare struct Node but in the code you use struct node. Identifiers are case sensitive.
  2. The declaration of struct node ends with a *, instead of a ;.
  3. You use arrays without declaring a size like char str[]; instead of char str[100];, this makes no sense in C.
  4. You use a field item whereas the struct you declare does not have a field named item.

And you should read the chapter dealing with strings in your C learning material. There is no real string type in C, you cannot assign strings with the = operator, use strcpy.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115