//insert at begining, delete at begining in singly linked list.
#include"stdio.h"
#include"conio.h"
#include"alloc.h"
typedef struct node
{
int data;
struct node *next;
}node;
void display(node *);
void del(node **);
void insert(node **,int);
void main()
{
node *first=NULL;
clrscr();
insert(&first,15);
insert(&first,18);
display(first);
del(&first);
printf("\n");
display(first);
getch();
}
void insert(node **first,int d)
{
node *newnode=(node *)malloc(sizeof(node));
if(newnode==NULL) {
printf("memory overflow");
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 del(node **first)
{
node *t;
if(first==NULL){
printf("empty list");
exit(0); }
else
{
printf("\nafter deletion.....");
t=*first;
*first=t->next;
free(t);
}
}
No comments:
Post a Comment