Showing posts with label display. Show all posts
Showing posts with label display. Show all posts

Wednesday, April 6, 2016

C program to implement link list with add first, add after, delete , reverse and display features

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int info;
struct node *link;
};
struct node *start,*newptr;
struct node *create_new_node(int i);
void insertend(struct node *n);
void insertmid(struct node *o);
void insertbeg(struct node *m);
void reverse(int c);
void del(int d);
void display(struct node *x);
int count(struct node *c);
void main()
{
int info,a,c;
struct node *np;
char ch;
start=NULL;
clrscr();
do
{
printf("ENTER YOUR CHOICE:\n");
printf("Enter 1 to insert at begining\n");
printf("Enter 2 to insert after a given element\n");
printf("Enter 3 to insert at end\n");
printf("Enter 4 to delete a particular element\n");
printf("Enter 5 to reverse the current linked list\n");
scanf("%d",&a);
if(a==1)
{
printf("Enter the element\n");
scanf("%d",&info);
np=create_new_node(info);
insertbeg(np);
}
else if(a==2)
{
printf("Enter the element\n");
scanf("%d",&info);
np=create_new_node(info);
insertmid(np);
}
else if(a==3)
{
printf("Enter the element\n");
scanf("%d",&info);
np=create_new_node(info);
insertend(np);
}
else if(a==4)
{
printf("Enter the element\n");
scanf("%d",&info);
np=create_new_node(info);
del(info);
}
else if(a==5)
{
reverse(c);
}
display(start);
c=count(start);
printf("The number of nodes are %d\n",c);
printf("\nDo you want to proceed? y/n\n");
scanf(" %c",&ch);
}
while(ch=='y');
getch();
}
/*CREATION OF NEW NODE*/
struct node *create_new_node(int i)
{
newptr=(struct node *)malloc(sizeof(struct node));
newptr->info=i;
newptr->link=NULL;
return newptr;
}
/*INSERTION AT BEGINING*/
void insertbeg(struct node *m)
{
if(start==NULL)
{
start=m;
}
else
{
m->link=start;
start=m;
}
}
/*INSERTION AT END*/
void insertend(struct node *n)
{
struct node *temp;
if(start==NULL)
{
start=n;
}
else
{
temp=start;
while(temp->link!=NULL)
temp=temp->link;
temp->link=n;
}
}
/*INSERTION AFTER A GIVEN ELEMENT*/
void insertmid(struct node *o)
{
int value;
struct node *temp;
temp=start;
printf("Enter the number after which ");
printf("new element has to be inserted:\n");
scanf("%d",&value);
while(temp!=NULL)
{
if(temp->info==value)
{
o->link=temp->link;
temp->link=o;
}
temp=temp->link;
}}
/*DELETE*/
void del(int d)
{
struct node *temp,*ptr;
temp=start;
while(temp!=NULL)
{
if(temp->link->info==d)
{
ptr=temp->link;
temp->link=ptr->link;
free(ptr);
}
temp=temp->link;
}
}
/*REVERSE*/
void reverse(int c)
{
struct node *p1,*p2,*p3;
if(start->link==NULL)
return;
p1=start;
p2=p1->link;
p3=p2->link;
p1->link=NULL;
p2->link=p1;
while(p3!=NULL)
{
p1=p2;
p2=p3;
p3=p3->link;
p2->link=p1;
}
start=p2;
}
/*DISPLAY*/
void display(struct node *x)
{
printf("\nCurrent linked list is:\t");
do
{
printf("%d\t",x->info);
x=x->link;
}
while(x!=NULL);
printf("\n");
}
/*COUNT THE NO. OF NODES*/
int count(struct node *c)
{
int co=0;
do
{
co++;
c=c->link;
}
while(c!=NULL);
return co;
}

output
ENTER YOUR CHOICE:
Enter 1 to insert at begining
Enter 2 to insert after a given element
Enter 3 to insert at end
Enter 4 to delete a particular element
Enter 5 to reverse the current linked list
1
Enter the element 2
Current linked list is:2
Do you want to proceed? y/n y
ENTER YOUR CHOICE:
Enter 1 to insert at begining
Enter 2 to insert after a given element
Enter 3 to insert at end
Enter 4 to delete a particular element
Enter 5 to reverse the current linked list
1
Enter the element 5
Current linked list is:5 2
Do you want to proceed? y/n y
ENTER YOUR CHOICE:
Enter 1 to insert at begining
Enter 2 to insert after a given element
Enter 3 to insert at end
Enter 4 to delete a particular element
Enter 5 to reverse the current linked list
4
Enter the element 2
Current linked list is:5
Do you want to proceed? y/n n

Monday, April 4, 2016

C program to implement sorting in link list

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
 int info;
 struct node* ptr;
}*START;
void create()
{
 int n;
 struct node *temp;
 temp=(struct node*)malloc(sizeof(node));
 if(temp==NULL)
  {
   printf("Memory not available");
  }
 printf("Enter data to enter");
 scanf("%d",&n);
 temp->info=n;
 temp->ptr=NULL;
 if(START==NULL)
  {
   START=temp;
  }
 else
  {
   struct node *t =START;
   while(t->ptr!=NULL)
    {
     t=t->ptr;
    }
   t->ptr=temp;
  }
}
void display()
{
 struct node *t=START;
 printf("\n");
 while(t!=NULL)
  {
   printf("%d ",t->info);
   t=t->ptr;
  }
}
void sort()
{
 struct node *t1=START;
 struct node *t2;
 int count=0;
 int i=0,j;
 while(t1!=NULL)
 {
  count++;
  t1=t1->ptr;
 }
 for(i=0;i<count;i++)
  {
   t1=START;
   for(j=0;j<count-i-1;j++)
    {
      if(t1->info>(t1->ptr)->info)
       {
int temp=t1->info;
t1->info=(t1->ptr)->info;
(t1->ptr)->info=temp;
       }
       t1=t1->ptr;
    }
  }
}
void main()
{
 START=NULL;
 int ch;
 do
 {
  printf("\nEnter choice 1.Insert 2.display 3.Sort 4.Exit");
  scanf("%d",&ch);
  switch(ch)
   {
    case 1:
     create();
     break;
    case 2:
     display();
     break;
    case 3:
     sort();
     break;
    case 4:
     exit(0);
     break;
   }
 }while(ch!=4);
}
output

Thursday, March 31, 2016

C program to implement link list using malloc

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<process.h>
struct node{
int info;
struct node *link;
}*start;
void addfirst()
 {
  struct node *ptr;
  int n;
  ptr=start;
  struct node *nw;
  nw=(node *)malloc(sizeof(node));
  printf("Enter item to insert");
  scanf("%d",&nw->info);
  if(nw==NULL)
   {
    printf("Overflow");
    exit(0);
   }
  if(start==NULL)
   {
    nw->link=NULL;
    start=nw;
   }
  else
   {
    nw->link=start;
    start=nw;
   }
 }
void addlast()
 {
  int n;
  struct node *nw,*ptr;
  nw=(node *)malloc(sizeof(node));
  if(nw==NULL)
   {
    printf("Overflow");
    exit(0);
   }
  printf("Enter data to enter");
  scanf("%d",&nw->info);
  ptr=start;
  while(ptr->link!=NULL)
   ptr=ptr->link;
  nw->link=NULL;
  ptr->link=nw;
 }
void addafter()
 {
  int n,pos;
  struct node *nw,*ptr;
  nw=(node *)malloc(sizeof(node));
  if(nw==NULL)
   {
    printf("overflow");
    exit(0);
   }
  printf("Enter data to insert");
  scanf("%d",&nw->info);
  printf("\n\nEnter node no afterwhich you want to insert node");
  scanf("%d",&pos);
  ptr=start;
  for(n=1;n<pos;n++)
   ptr=ptr->link;
  nw->link=ptr->link;
  ptr->link=nw;
 }
void deletenode()
 {
  int n;
  printf("Enter position of the node you want to delete\n\n");
  scanf("%d",&n);
  struct node *ptr;
  ptr=start;
  for(int i=1;i<n-1;i++)
   ptr=ptr->link;
  if(i==1)
   {
    start=start->link;
   }
  else
   {
    ptr->link=ptr->link->link;
   }
 }
void display()
 {
  struct node *ptr;
  ptr=start;
  while(ptr!=NULL)
   {
    printf(" ->%d",ptr->info);
    ptr=ptr->link;
   }
 }
void main()
{
 int n;
 start=0;
 do
 {
  printf("\n\n1.ADDFIRST\n\n2.ADDAFTER\n\n3.ADDLAST\n\n4.DISPLAY\n\n5.DELETE\n\n6.EXIT\n\n");
  scanf("%d",&n);
  if(n==1)
   {
    addfirst();
   }
  if(n==2)
   {
    addafter();
   }
  if(n==3)
   {
    addlast();
   }
  if(n==4)
   {
    display();
   }
  if(n==5)
   {
    deletenode();
   }
 }while(n!=6);
}
output
1. ADDFIRST
2.ADDAFTER
2.ADDLAST
4.DISPLAY
5.DELETE
6.EXIT
1
Enter item to insert 5
1. ADDFIRST
2.ADDAFTER
3.ADDLAST
4.DISPLAY
5.DELETE
6.EXIT
3
Enter data to enter
5
1. ADDFIRST
2.ADDAFTER
3.ADDLAST
4.DISPLAY
5.DELETE
6.EXIT
4
->3->5

Friday, March 11, 2016

C program to show add, append, sort, delete, display, search operation on array

#include<iostream.h>
#include<conio.h>
class List
 {
  int a[50];
  int c;
  public:
   void add(int x);
   void deleteall();
   int delete_index(int index);
   void delete_number(int x);
   int search(int x);
   void insert(int x,int index);
   void sort();
   void append(int x);
   void display();
   int get(int index);
   List();
 };
void List::List()
 {
  c=-1;
 }
void List::add(int x)
 {
  c++;
  a[c]=x;
 }
void List::deleteall()
 {
  int i;
  for(i=0;i<=c;i++)
   {
    a[i]=NULL;
   }
  c=-1;
 }
int List::delete_index(int index)
 {
  if(index>c)
   {
    cout<<"\nIndex is out of bound";
    return -1;
   }
  else
   {
    int r=a[index];
    int i;
    for(i=index;i<c;i++)
     {
      a[i]=a[i+1];
     }
    c--;
    return r;
   }
 }
void List::delete_number(int x)
 {
  int i;
  for(i=0;i<=c;i++)
   {
    if(a[i]==x)
     {
      break;
     }
   }
  if(i<=c)
   {
    for(int k=i;k<c;k++)
     {
      a[k]=a[k+1];
     }
     c--;
     cout<<"\nNumber deleted at index "<<i;
   }
  else
   {
    cout<<"\nEntered number not present";
   }
 }
int List::search(int x)
 {
  int i;
  for(i=0;i<=c;i++)
   {
    if(a[i]==x)
     {
      break;
     }
   }
  if(i<=c)
   {
    return i;
   }
  else
   {
    return -1;
   }
 }
void List::insert(int x,int index)
 {
  int i;
  if(index>(c+1))
   {
    cout<<"\nList is shorter";
   }
  else
   {
    for(i=c;i>=index;i--)
     {
      a[i+1]=a[i];
     }
    a[index]=x;
    c++;
   }
 }
void List::sort()
 {
  int i;
  int j;
  int temp;
  for(i=0;i<c;i++)
   {
    for(j=0;j<c-i;j++)
     {
      if(a[j]>a[j+1])
       {
temp=a[j+1];
a[j+1]=a[j];
a[j]=temp;
       }
     }
   }
 }
void List::append(int x)
 {
  c++;
  a[c]=x;
 }
void List::display()
 {
  int i;
  for(i=0;i<=c;i++)
   {
    cout<<"\t"<<a[i];
   }
 }
int List::get(int index)
 {
  return (a[index]);
 }
void main()
 {
  int ch;
  int no,in,x;
  List List_object;
  do
   {
    cout<<endl<<"1.Add an item\n 2.Delete All \n3.Delete at perticular index\n4.Delete perticular number \n5.Search a number \n6.Insert a number at perticular index \n7.Sort List \n8.Append an item \n9.Display list \n10.Get item at perticular index";
    cin>>ch;
    switch(ch)
     {
      case 1:
       cout<<"\nEnter a number";
       cin>>no;
       List_object.add(no);
       break;
      case 2:
       List_object.deleteall();
       break;
      case 3:
       cout<<"\nEnter Index to delete";
       cin>>in;
       x=List_object.delete_index(in);
       cout<<"\nDeleted number="<<x;
       break;
      case 4:
       cout<<"\nEnter number to delete";
       cin>>no;
       List_object.delete_number(no);//Bool
       break;
      case 5:
       cout<<"\nEnter number to search";
       cin>>no;
       x=List_object.search(no);
       if(x==-1)
{
cout<<"\nNumber not present";
}
       else
{
cout<<"\nNumber present at "<<x;
}
       break;
      case 6:
       cout<<"\nEnter no and index";
       cin>>no>>in;
       List_object.insert(no,in);
      break;
     case 7:
       List_object.sort();
       List_object.display();
       break;
     case 8:
       cout<<"\nEnter a no to append";
       cin>>no;
       List_object.append(no);
       break;
     case 9:
       List_object.display();
       break;
     case 10:
       cout<<"\nEnter index to find number";
       cin>>in;
       x=List_object.get(in);
       cout<<"\nNumber is "<<x;
     }
   }while(ch!=11);
  getch();
 }

output
 1.Add an item
 2.Delete All 
 3.Delete at perticular index
 4.Delete perticular number 
 5.Search a number 
 6.Insert a number at perticular index 
 7.Sort List 
 8.Append an item 
 9.Display list 
 10.Get item at perticular index
 1
 Enter a number 6
 1.Add an item
 2.Delete All 
 3.Delete at perticular index
 4.Delete perticular number 
 5.Search a number 
 6.Insert a number at perticular index 
 7.Sort List 
 8.Append an item 
 9.Display list 
 10.Get item at perticular index
 8
 Enter a no to append 2
 1.Add an item
 2.Delete All 
 3.Delete at perticular index
 4.Delete perticular number 
 5.Search a number 
 6.Insert a number at perticular index 
 7.Sort List 
 8.Append an item 
 9.Display list 
 10.Get item at perticular index
 9
 6 2
 1.Add an item
 2.Delete All 
 3.Delete at perticular index
 4.Delete perticular number 
 5.Search a number 
 6.Insert a number at perticular index 
 7.Sort List 
 8.Append an item 
 9.Display list 
 10.Get item at perticular index 
 5 
 Enter number to search 8
 Number not present
 1.Add an item
 2.Delete All 
 3.Delete at perticular index
 4.Delete perticular number 
 5.Search a number 
 6.Insert a number at perticular index 
 7.Sort List 
 8.Append an item 
 9.Display list 
 10.Get item at perticular index 
 11