/////////////////////////////////////////////////////////////////
#include"stdio.h"
#include"conio.h"
#include"alloc.h"
#include"stdlib.h"
typedef struct node
{
int data;
int pos;
struct node *next;
}node;
void insertbeg(node **,int);
void insert(node **,int,int);
void del(node **,int);
void display(node *);
void main()
{
node *first=NULL;
clrscr();
printf("list is: \n");
insertbeg(&first,30);
insertbeg(&first,25);
insertbeg(&first,10);
display(first);
insert(&first,20,2);
display(first);
del(&first,2);
display(first);
getch();
}
void insertbeg(node **first,int d)
{
node *newnode=(node *)malloc(sizeof(node));
if(newnode==NULL)
{
printf("memory overflow\n");
exit(0);
}
else
{
newnode->data=d;
newnode->next=*first;
*first=newnode;}
}
void display(node *first)
{
while(first!=NULL)
{
printf("%d ",first->data);
first=first->next;
}
}
void insert(node **first,int d,int pos)
{
node *p;
int i=1;
node *newnode=(node *)malloc(sizeof(node));
if(newnode==NULL||pos<1)
{printf("memory overflow / invalid position");
exit(0);
}
else
{
p=*first;
while(i
if(p==NULL){
printf("less value\n");
exit(0);
}i++; }
newnode->data=d;
newnode->next=p->next;
p->next=newnode;
}
printf("\nafter insert middle\n");
}
void del(node **first,int pos)
{
node *p,*q;
int i;
if(*first==NULL||pos<1)
{printf("empty list / invalid position");
exit(0); }
else
{
p=*first;
q=NULL;
i=1;
while(i
p=p->next;
if(p==NULL) {
printf("less position\n");
exit(0);}
i++; }
if(q==NULL)
*first=p->next;
else
q->next=p->next;
free(p);
}
printf("\nafter deletion\n");
}
No comments:
Post a Comment