Python,C,C++ and JAVA programs for CBSE, ISC, B.Tech and I.T Computer Science and MCA students

The Programming Project: February 2013

Wednesday, February 13, 2013

C Program #10

/* Circular Link List */

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct circular_list
{
int d;
struct circular_list *next;
};
typedef struct circular_list node;
void create(node *);
void display(node *,node *);
node *search(node *,node *);
void insert(node *,node *);
void displaypos(node *,node *);
int main()
{
int choice;
node *root,*head,*tag;
root=(node *)malloc(sizeof(node));
head=root;
printf("\n ***********************MENU FOR LIST******************* \n");
printf("\n Press 1 to create:");
printf("\n Press 2 to display:");
printf("\n Press 3 to search:");
printf("\n Press 4 to insert");
printf("\n Press 5 to display with respect to an element in the list:");
printf("\n Press 6 to exit \n");
while(1)
{
printf("\n Enter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
create(root);
break;
case 2:
display(root,head);
break;
case 3:
tag=search(root,head);
if(tag==NULL)
printf("\n Element not found:\n");
else
printf("\n The element is %d",tag->d);
break;
case 4:
insert(root,head);
break;
case 5:
displaypos(root,head);
break;
case 6:
exit(0);
break;
default:
break;
}
}
return 0;
}
void create(node *record)
{
int n,i;
node *p;
p=record;
printf("\n Enter the number of elements to be added:");
scanf("%d",&n);
printf("\n Enter the elements:\n");
scanf("%d",&record->d);
for(i=1;i<=n-1;i++)
{
record->next=(node *)malloc(sizeof(node));
record=record->next;
scanf("%d",&record->d);
}
record->next=p;
return;
}
void display(node *record,node *head)
{
printf("\n List elements are:\n");
while(1)
{
printf(" %d ",record->d);
record=record->next;
if(record==head)
break;
}
return;
}
node *search(node *record,node *head)
{
display(record,head);
node *p;
int n,flag=0;
printf("\n Enter the item to be searched:");
scanf("%d",&n);
while(record->next!=NULL)
{
if(record->d==n)
{
flag=1;
p=record;
break;
}
record=record->next;
if(record==head)
          break;
}
if(flag==1)
return (p);
else
return (NULL);
}
void insert(node *record,node *head)
{
node *pos,*p,*temp;
printf("\n Search the element after which the item has to be inserted:");
pos=search(record,head);
if(pos==NULL)
{
printf("\n Element cannot be inserted:");
}
else
{
p=(node *)malloc(sizeof(node));
printf("\n Enter the item to be inserted:");
scanf("%d",&p->d);
temp=pos->next;
pos->next=p;
p->next=temp;
}

}
void displaypos(node *record,node *head)
{
node *tag,*p;
printf("\n Search the item w.r.t which the list will be displayed:");
tag=search(record,head);
if(tag==NULL)
{
printf("\n List Cannot be displayed:\n");
}
else
{
p=tag;
printf("\n List with respect to %d",tag->d);
while(tag->next!=p)
{
printf(" %d ",tag->next->d);
tag=tag->next;
}
}
return;
}

C Program #9

/*Doubly Linked List */

#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
struct list_element
{
int d;
struct list_element *next,*prev;
};
typedef list_element node;
void create(node *);
void displayn(node *);
void displayr(node *);
node *search(node *);
node *inserteb(node *);
void insertel(node *);
void inserte(node *);
node *deletef(node *);
void deletee(node *);
void deleteb(node *);
int main()
{
int choice;
node *root,*record,*tag ;
record=(node *)malloc(sizeof(node));
root=record;
printf("\n ***********************MENU FOR LIST******************* \n");
printf("\n Press 1 to create:");
printf("\n Press 2 to display list in forward drection:");
printf("\n Press 3 to display list in reverse direction:");
printf("\n Press 4 to search in the list:");
printf("\n Press 5 to inserte at the begining of the list:");
printf("\n Press 6 to inserte at the end of the list:");
printf("\n Press 7 to inserte between first and last element:");
printf("\n Press 8 to delete the first element:");
printf("\n Press 9 to delete the last elemnt:");
printf("\n Press 10 to delete between the last and first elements:");
printf("\n Press 11 to exit:");
while(1)
{
printf("\n Enter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
create(root);
break;
case 2:
displayn(root);
break;
case 3:
displayr(root);
break;
case 4:
tag=search(root);
if(tag==NULL)
printf("\n Element not found:\n");
else
printf("\n The element is %d",tag->d);
break;
case 5:
root=inserteb(root);
break;
case 6:
insertel(root);
break;
case 7:
inserte(root);
break;
case 8:
root=deletef(root);
break;
case 9:
deletee(root);
break;
case 10:
deleteb(root);
   break;
case 11:
exit(0);
break;
default:
break;
}
}
return 0;
}
void create(node *record)
{
int i,n;
node *p;
printf("\n Enter the number of elements:");
scanf("%d",&n);
printf("\n Enter the elements:\n");
scanf("%d",&record->d);
record->prev=NULL;
for(i=1;i<=n-1;i++)
{
p=record;
record->next=(node *)malloc(sizeof(node));
record=record->next;
scanf("%d",&record->d);
record->prev=p;
}
record->next=NULL;
return;
}
void displayn(node *record)
{
 printf("\n");
while(record!=NULL)
{
printf("\t %d",record->d);
record=record->next;
}


}
void displayr(node *record)
{
printf("\n");
while(record->next!=NULL)
{
 // printf("\t %d",record->d);
record=record->next;
}
printf("\n");
while(record!=NULL)
{
printf("\t %d",record->d);
record=record->prev;
}
}
node *search(node *record)
{
int n,flag=0;
node *p;
printf("\n Enter the element to be searched:\n");
scanf("%d",&n);
while(record!=NULL)
{
if(record->d==n)
{
flag=1;
p=record;
break;
}
record=record->next;
}
if(flag==1)
return (p);
else
return (NULL);
}
node *inserteb(node *record)
{
node *p;
p=(node *)malloc(sizeof(node));
printf("\n Enter the element to be inserted:");
scanf("%d",&p->d);
p->next=record;
record->prev=p;
p->prev=NULL;
return (p);
}
void insertel(node *record)
{
node *p;
p=(node *)malloc(sizeof(node));
printf("\n Enter the element to be inserted:");
scanf("%d",&p->d);
while(record->next!=NULL)
{
record=record->next;
}
record->next=p;
p->next=NULL;
p->prev=record;
}
void inserte( node *record)
{
node *p,*tag,*pv;
displayn(record);
printf("\n Enter the element after which insertion has to be done:");
tag=search(record);
if(tag==NULL)
{
printf("\n Cannot be inserted");
}
else
{
pv=tag;
p=(node *)malloc(sizeof(node));
printf("\n Enter the element to be inserted:");
scanf("%d",&p->d);
p->next=tag->next;
tag->next=p;
p->prev=pv;
p->next->prev=p;

}
}
node *deletef(node *record)
{
node *p;
p=record->next;
p->prev=NULL;
free(record);
return(p);
}
void deletee(node *record)
{
 node *p;
while(record->next!=NULL)
{
record=record->next;
}
p=record->prev;
p->next=NULL;
p->prev=record->prev->prev;
   free(record);
}
void deleteb(node *record)
{
 node *p,*tag;
 displayn(record);
 printf("\n Item to be deleted, should be searched:\n");
 tag=search(record);
 if(tag==NULL)
printf("\n Element not found, try again");
 else
{
p=tag->prev;
p->next=tag->next;
p->next->prev=p;
free(tag);
}
}