Untitled
unknown
plain_text
a year ago
2.5 kB
9
Indexable
// Online C compiler to run C program online
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node *next;
struct node *prev;
}*start=NULL,*last=NULL;
void createdll(int data){
struct node *ptr=(struct node*)malloc(sizeof(struct node));
ptr->data=data;
ptr->next=NULL;
ptr->prev=NULL;
if(start==NULL){
start=ptr;
last=ptr;
}
else{
ptr->prev=last;
last->next=ptr;
last=ptr;
}
}
void print(struct node *start){
struct node *temp=start;
while(temp!=NULL){
printf("%d",temp->data);
temp=temp->next;
}
}
void deletefirst(){
struct node *temp=start;
start=start->next;
free(temp);
}
void addfirst(struct node *start,int data){
struct node *ptr=(struct node*)malloc(sizeof(struct node));
ptr->data=data;
ptr->next=start->next;
ptr->prev=NULL;
start->next->prev=ptr;
start=ptr;
}
void add_after(struct node* start, int n , int given)
{
struct node* t = start;
struct node* ptr = (struct node*) malloc(sizeof(struct node));
ptr->data = n;
ptr->next = NULL;
ptr->prev = NULL;
while(t->data != given){
t = t->next;
}
ptr->next = t->next;
ptr->prev = t;
t->next->prev = ptr;
t->next = ptr;
}
void add_before(struct node* start, int n, int given)
{
struct node* ptr = (struct node*) malloc(sizeof(struct node));
ptr->data = n;
ptr->next = NULL;
ptr->prev = NULL;
struct node* temp = start;
while(temp->data != given)
{
temp = temp->next;
}
ptr->next = temp;
ptr->prev = temp->prev;
temp->prev->next = ptr;
temp->prev = ptr;
}
void delete_after(struct no* start, int given)
{
struct node *temp = start;
while (temp != NULL && temp->data != given)
{
temp = temp->next;
}
struct node* t = temp->next;
temp->next = temp->next->next;
free(t);
}
void reverse(struct node *start){
struct node *t1=start,*t2=last;
while(t2->next!=t1 && t1!=t2){
int temp=t1->data;
t1->data=t2->data;
t2->data=temp;
t1=t1->next;
t2=t2->prev;
}
}
void main() {
int n;
printf("enter no. of nodes=");
scanf("%d",&n);
for(int i=0;i<n;i++){
int data;
scanf("%d",&data);
createdll(data);
}
print(start);
deletefirst();
print(start);
addfirst(start,13);
print(start);
add_before(start,1221,23);
add_after(start,1221,23);
}Editor is loading...
Leave a Comment