Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

PRACTICAL NUMBER 10 DSU

#include<iostream>
#include<stdlib.h>
using namespace std;
struct node
{
int data;
struct node *next;
};

struct node *first(struct node **p,int value)


{
struct node *k=new node;
k->data=value;
k->next=(*p);
(*p)=k;
};
struct node *after(struct node *prev_node,int value)
{
if(prev_node==NULL)
{
cout<<"prevnode can not be null:\n";
}
else
{
struct node *new_node=new node;
new_node->data=value;
new_node->next=prev_node->next;
prev_node->next=new_node;

}
};
struct node *end(struct node **p,int value)
{
struct node *new_node=new node;
new_node->data=value;
new_node->next=NULL;
if(*p==NULL)
{
*p=new_node;
}
struct node *last=*p;
while(last->next!=NULL)
{
last=last->next;
}
last->next=new_node;

};
void print(struct node *k)
{
while(k!=NULL)
{
cout<<k->data<<endl;
k=k->next;
}
}
void deletenode(struct node **head_re,int key)
{
struct node *temp=*head_re,*prev;
if(temp !=NULL&&temp->data==key)
{
*head_re=temp->next;
free(temp);
return;
}
}
int main()
{
struct node *head=NULL;
first(&head,58);
first(&head,96);
first(&head,23);
print(head);
after(head->next->next,888);
after(head,999);
cout<<"add after \n";
print(head);
end(&head,2362);
end(&head,8956);
cout<<"\nadd at end:\n";
print(head);
deletenode(&head,23);
cout<<"\n\n";
print(head);
}

You might also like