write a menu driven program to create a linked list, insert an item, delete an item, and display the list of items. the program should contain a class with the required data and functions.

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

//Structure containing a Data part & a
//Link part to the next node in the List

struct Node
{
 int Data;
 struct Node *Next;
}*Head;


//Adding a Node at the end of the list

void Add(int num)
{
  struct Node *temp1, *temp2;

  temp1=(struct Node *)malloc(sizeof(struct Node));
  temp1->Data=num;

  // Copying the Head location into another node.
  temp2=Head;

  if(Head == NULL)
  {
     // If List is empty we create First Node.
     Head=temp1;
     Head->Next=NULL;
  }
  else
  {
     // Traverse down to end of the list.
     while(temp2->Next != NULL)
     temp2=temp2->Next;

     // Append at the end of the list.
     temp1->Next=NULL;
     temp2->Next=temp1;
  }
}



// Counting number of elements in the List

int length()
{
  struct Node *cur_ptr;
  int count=0;

  cur_ptr=Head;

  while(cur_ptr != NULL)
  {
     cur_ptr=cur_ptr->Next;
     count++;
  }
  return(count);
}

// Deleting a node from List depending upon the data in the node.

int delNodeData(int num)
{
  struct Node *prev_ptr, *cur_ptr;

  cur_ptr=Head;

  while(cur_ptr != NULL)
  {
     if(cur_ptr->Data == num)
     {
    if(cur_ptr==Head)
    {
       Head=cur_ptr->Next;
       free(cur_ptr);
       return 0;
    }
    else
    {
       prev_ptr->Next=cur_ptr->Next;
       free(cur_ptr);
       return 0;
    }
     }
     else
     {
    prev_ptr=cur_ptr;
    cur_ptr=cur_ptr->Next;
     }
  }

  printf("\nElement %d is not found in the List", num);
  return 1;
}

// Displaying list contents

void display()
{
  struct Node *cur_ptr;

  cur_ptr=Head;

  if(cur_ptr==NULL)
  {
     printf("\nList is Empty");
  }
  else
  {
      printf("\nElements in the List: ");
      //traverse the entire linked list
      while(cur_ptr!=NULL)
      {
      printf(" -> %d ",cur_ptr->Data);
      cur_ptr=cur_ptr->Next;
      }
      printf("\n");
  }
}



int main(int argc, char *argv[])
{
 int i=0;

 //Set HEAD as NULL
 Head=NULL;

 while(1)
 {
    clrscr();
    printf("\n\nMENU\n");
    printf(" \n1.Insert a Node");
    printf(" \n2.Delete a Node in the List");
    printf(" \n3.Display the Elements in the List");
    printf(" \n4.Count number of elements in the List");
    printf(" \n\n5. Exit\n");
    printf(" \nChoose Option: ");
    scanf("%d",&i);

    switch(i)
    {

      case 1:
      {
      int num;
      printf(" \nEnter the Number to insert: ");
      scanf("%d",&num);
      Add(num);
      display();
      printf("\n\n\nPress Any key to continue...");
      getch();
      break;
      }

      case 2:
      {
      int num;
      printf(" \nEnter the number to be deleted from List: ");
      scanf("%d",&num);
      delNodeData(num);
      display();
      printf("\n\n\nPress Any key to continue...");
      getch();
      break;
      }


      case 3:
      {
      display();
      printf("\n\n\nPress Any key to continue...");
      getch();
      break;
      }

      case 4:
      {
      display();
      printf(" \nTotal number of nodes in the List: %d",length());
      printf("\n\n\nPress Any key to continue...");
      getch();
      break;
      }


      case 5:
      {
      struct Node *temp;

      while(Head!=NULL)
      {
          temp = Head->Next;
          free(Head);
          Head=temp;
      }
      exit(0);
      }

      default:
      {
      printf("\nWrong Option choosen");
      printf("\n\nPress any key to continue...");
      getch();
      }
    }
 }
}

is my program correct according to the question or there are mistakes or missing codes?

deceptikon commented: Thank you for formatting your code +0

Why are you asking us if your program is correct? You should be able to determine that yourself by testing the code you wrote. If the program does everything the question wanted then it is correct. There may or may not be better ways to code it but that doesn't mean other ways are more or less correct then how you wrote it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.