Showing posts with label Programming. Show all posts
Showing posts with label Programming. Show all posts

Saturday, May 8, 2010

C Programming

/*Simplest Program of Doubly Linked List */
#include
#include
#include
#include
#include
void add();
void del();
void dis();
struct node
{
int d;
struct node *next,*pre;
};
struct node *start,*current,*temp,*p,*t;
int ele;
void main()
{
int ch;
clrscr();
start=current=temp=NULL;
do
{
printf("\nEnter\n1.Insert a Node\n2.Delete a Node\n3.Display a Node\n4.Exit\n\nEnter your choice here:");
scanf("%d",&ch);
switch(ch)
{
case 1:
add();
break;
case 2:
del();
break;
case 3:
dis();
break;
case 4:
exit(0);
default:
printf("\007Wrong Choice\007");
sleep(10);
}
}
while(ch!=4);
getch();
}

void add()
{
int q=1,n;
p=start;
t=p->next;
printf("\nEnter the position to insert the node");
scanf("%d",&n);
temp=(struct node *)malloc(sizeof(struct node));
temp->next=temp->pre=NULL;
printf("\n Enter the Value");
scanf("%d",&temp->d);
if(start==NULL)
{
start=current=temp;
}
else if(n==1)
{
temp->next=start;
start->pre=temp;
start=temp;
}
else
{
while(q<=n-2)
{
p=p->next;
t=t->next;
q++;
}
temp->next=t;
t->pre=temp;
p->next=temp;
temp->pre=p;
if(p==current)
{
current=temp;
}
}
}

void del()
{
int q=1,n;
p=start;
t=start->next;
if(start==NULL)
{
printf("\007\nCannot Del List Empty\n\n");
}
else
{
printf("\nEnter the position to Delete the node");
scanf("%d",&n);
if(n==1)
{
start=start->next;
free(p);
}
else
{
while(q<=n-2)
{
p=p->next;
t=t->next;
q++;
}
p->next=t->next;
t->next->pre=p;
if(t==current)
{
current=p;
}
}
}
}

void dis()
{
if(start==NULL)
{
printf("\nCannot Display,list Empty");
}
else
{
int ch1;
printf("\nEnter\n1.Display from Start\n2.Display from End\n\nEnter your choice here");
scanf("%d",&ch1);
if(ch1==1)
{
temp=start;
printf("\n\nList elements from Start\n\n");
while(temp!=NULL)
{
printf("%d, ",temp->d);
temp=temp->next;
}
}
else if(ch1==2)
{
temp=current;
printf("\n\nList elements from End\n\n");
while(temp!=NULL)
{
printf("%d, ",temp->d);
temp=temp->pre;
}
}
else
{
printf("\007\007Wrong Choice!");
}
}
}

Thursday, April 22, 2010

A message to you all...

This blog is a plateform for those who want to be a programmer or who are programmer...
All the "Suggestions and Comments" to make this platform more useful are invited...