0% found this document useful (0 votes)
8 views

Doubly Linked List

Uploaded by

mandarphotos27
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Doubly Linked List

Uploaded by

mandarphotos27
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

1]doubly linked list basic

#include <stdio.h>
#include<stdlib.h>
void main()
{
struct node
{
struct node* prev;
int data;
struct node* next;
};
struct node *head,*temp,*newnode;
head=0;
int choice=1,count=0;
while(choice==1)
{
newnode=(struct node*)malloc(sizeof(struct node));
printf("enter element:");
scanf("%d",&newnode->data);
newnode->next=0;
newnode->prev=0;
if(head==0)
{
head=temp=newnode;
}
else
{
temp->next=newnode;
newnode->prev=temp;
temp=newnode;
}
printf("want to continue:");
scanf("%d",&choice);
}
temp=head;
printf("elements are:\n");
while(temp!=0)
{
printf("%d\n",temp->data);
temp=temp->next;
count++;
}
printf("count is:%d",count);
}

2]I insertion ALL

#include <stdio.h>
#include<stdlib.h>
void insatbeg();
void insatend();
void insatpos();
void display();
struct node
{
struct node* prev;
int data;
struct node* next;
};
struct node *head=0,*temp,*newnode;
int count=0;
void main()
{
int choice=1,wish,op;
while(choice==1)
{
newnode=(struct node*)malloc(sizeof(struct node));
printf("enter element:");
scanf("%d",&newnode->data);
newnode->next=0;
newnode->prev=0;
if(head==0)
{
head=temp=newnode;
}
else
{
temp->next=newnode;
newnode->prev=temp;
temp=newnode;
}
printf("want to continue press 1 else anything:");
scanf("%d",&choice);
}

printf("select operation:\n1-insert at begging:\n2-insert at end:\n3-insert ant a pos:\n");


scanf("%d",&op);
switch(op)
{
case 1:(insatbeg());
break;
case 2:(insatend());
break;
case 3:(insatpos());
}
display();
}
void display()
{
temp=head;
printf("elements are:\n");
while(temp!=0)
{
printf("%d\n",temp->data);
temp=temp->next;
count++;
}
printf("count is:%d",count);
}
void insatbeg()
{
newnode=(struct node*)malloc(sizeof(struct node));
printf("enter the element you want to insert:");
scanf("%d",&newnode->data);
newnode->prev=0;
head->prev=newnode;
newnode->next=head;
head=newnode;
}
void insatend()
{
temp=head;
while(temp->next!=0)
{
temp=temp->next;
}
newnode=(struct node*)malloc(sizeof(struct node));
printf("enter the element you want to insert:");
scanf("%d",&newnode->data);
newnode->prev=temp->next;
newnode->next=0;
temp->next=newnode;
temp=newnode;
}
void insatpos()
{
temp=head;
int pos,i=0;
printf("enter position to insert:");
scanf("%d",&pos);
newnode=(struct node*)malloc(sizeof(struct node));
printf("enter the element you want to insert:");
scanf("%d",&newnode->data);
while(i<pos-1)
{
temp=temp->next;
i++;
}
newnode->prev=temp->prev;
newnode->next=temp;
temp->prev->next=newnode;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy