Ads Top

LINK LIST DELETING A NODE PROGRAM USING CPP

#include<iostream.h>
#include<conio.h>
struct item

{
int no:
          char name[40]
          struct item *next;
};
void main ()
{
          struct item *ptr, *first, *temp;
          char ch='y';
          int number, flag=0;
          clrscr();
          ptr=new item;
          first=ptr;
          while(ch=='y'||ch=='y')
{

          clrscr();
          cout<<"enter the item number";
          cin>>ptr->no;
          cout<<"enter the item name";

cin>>ptr->name;
          cout<<"do you want to add more items(y/n):";
          cin>>ch;
          if(ch=='y'||ch=='y')
{
          ptr->next= NULL;
          clrscr();
          ptr=first;
          cout<<"item no Name\n";
          while(ptr!=NULL
{
          cout<<ptr->no<<"\t\t"<<ptr->name<<endl;
          ptr=ptr->next;
}
          cout<<"enter the item numer to be deleted:";
          cin>>number;
          ptr=first;

if(ptr->no==number)
{
          first=ptr->next;
          delete ptr;
}

OUTPUT:

enter the item number 1
enter the item name books
do you want to add more item(y/n)):y
enter the item number2
enter the item name pen
do you want to add more items(y/n):n

item no                 name
1                 books
2                 pen

enter the item number to be deleted:1
2        pen


No comments:

Touchfeeds. Powered by Blogger.