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

Amandsa

The document contains C code to implement functions for a doubly linked list including: 1. Functions to create, display, find length of a doubly linked list 2. Functions to insert nodes at the beginning, end, and a specified position of the list 3. Functions to delete nodes from the beginning and end of the list 4. A function to reverse the doubly linked list
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)
18 views

Amandsa

The document contains C code to implement functions for a doubly linked list including: 1. Functions to create, display, find length of a doubly linked list 2. Functions to insert nodes at the beginning, end, and a specified position of the list 3. Functions to delete nodes from the beginning and end of the list 4. A function to reverse the doubly linked list
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/ 31

0801IT221017

Q1. WRITE A PROGRAM TO DISPLAY ALL THE FUNCTIONMS OF SINGLY LINKED LIST
#include<stdio.h>

#include<stdlib.h>

#include<math.h>

struct node

{ int data; struct

node *next ;

} *head,*tail;

int l;

int length(){

struct node *temp;

int count=0;

temp = head;

while(temp!=NULL){

count++;

l = count;

temp = temp->next;

printf("length of list %d\n",count);

return count;

void create(){

struct node *temp, *newnode;

int choice = 1;

head = NULL ;

while(choice){

newnode = (struct node*)malloc(sizeof(struct node));

printf("enter the data\n");

1
0801IT221017

scanf("%d", &newnode->data);

newnode->next = NULL;

if(head == NULL){

head = temp = newnode;

else{

temp->next = newnode;

temp = temp->next;

printf("do you want to continue 0/1 \n");

scanf("%d",&choice);

void display(){

printf("display\n");

struct node *temp;

temp = head;

if(head == NULL){

printf("list is empty");

else{

while(temp != NULL){

printf(" %d ",temp->data);

tail = temp;

temp = temp->next;

} printf("\n"); }

2
0801IT221017

void insertAtBeg(){ struct node *newnode;

newnode=(struct node*)malloc(sizeof(struct node));

printf("enter data");

scanf("%d",&newnode->data);

newnode->next=NULL;

if(head==NULL){

head=newnode;

else{

newnode->next = head;

head = newnode;

void insertAtEnd(){ struct node *newnode;

newnode=(struct node*)malloc(sizeof(struct node));

printf("enter data\n");

scanf("%d",&newnode->data);

newnode->next = NULL;

if(head==NULL){

head = tail = newnode;

else{

tail->next=newnode;

tail=newnode;

void insertAtPos(){ struct node *newnode, *temp;

3
0801IT221017

temp = head;

int pos, i=1 ;

printf("enter the position\n");

scanf("%d",&pos);

l = length();

if(pos<1||pos>l){

printf("invalid positiion");

else if(pos == 1){

insertAtBeg();

else{

newnode = (struct node*)malloc(sizeof(struct node));

newnode->next = NULL;

while(i<pos-1){

temp=temp->next;

i++;

} printf("enter data");

scanf("%d",&newnode->data);

newnode->next = temp->next;

temp->next = newnode;

void reverse(){

struct node *prev,*nextnode, *current;

prev=NULL;

current=nextnode=head;

while(nextnode!=NULL){

nextnode=nextnode->next;

4
0801IT221017

current->next=prev;

prev=current;

current=nextnode;

head=prev;

void insert(){

struct node *newnode,*temp;

int choice;

temp = head;

printf("enter 1 for inserting at begining\n");

printf("enter 2 to insert at end\n");

printf("enter 3 to insert at any position\n");

scanf("%d",&choice);

if(choice == 1){

insertAtBeg();

else if(choice == 2){

insertAtEnd();

else{

insertAtPos();

5
0801IT221017

void delfrombeg(){

struct node *temp;

temp = head;

if(head==NULL){

printf("list is empty");

else{

head = head->next;

free(temp);

void delfromend(){

struct node *temp, *prev;

temp = head;

if(head == NULL){

printf("list is empty\n");

else{

while(temp->next != NULL){

prev = temp;

temp= temp->next;

prev ->next = NULL;

tail=prev;

free(temp);

void delfromPos(){

int l,pos, i=1;

6
0801IT221017

struct node *temp, *prev;

printf("enter the position\n");

scanf("%d",&pos);

temp = head;

l=length;

if(pos<1 || pos>l){

printf("invalid position\n");

else if(pos == 1){

delfrombeg();

else if(pos == l){

delfromend();

else{ while(i<pos){

prev = temp;

temp = temp->next;

i++;

prev->next = temp->next;

free(temp);

void delete(){

int n;

printf("enter 1 to delete from begining\n");

printf("enter 2 to delete from end\n");

7
0801IT221017

printf("enter 3 to delete from any position\n");

scanf("%d",&n); if(n==1){ delfrombeg();

else if(n==2){

delfromend();

else{

delfromPos();

int main() {

printf("ENROLLEMENT NO: 0801IT221017\n");

printf("SINGLY LINKED LIST\n");

int choice, m=1 ;

while(m){

printf("ENTER 1 TO CREATE\n ENTER 2 TO INSERT \n ENTER 3 TO DELETE \n ENTER 4 TO REVERSE \n


ENTER 5 TO DISPLAY\n");

scanf("%d",&choice);

switch(choice){

case 1 : create();

break;

case 2 : insert();

break;

case 3 : delete();

break;

case 4 : reverse();

8
0801IT221017

break;

case 5 : display();

break;

default : printf("invalid input\n ");

printf("do you want to continue\n");

scanf("%d",&m); }

return 0;

OUTPUT:

9
0801IT221017

Q 2. WRITE A PROGRAM TO SHOW ALL THE OPERATIONS ON DOUBLY LINKED LIST.


ANS.
#include<stdio.h>

#include<stdlib.h>

#include<math.h>

struct node {

int data;

struct node*next,*prev;

} *head,*tail;

int l;

10
0801IT221017

int length(){

struct node *temp;

int count=1;

temp = head;

while(temp!=NULL){

count++;

l = count;

temp = temp->next;

printf("length of list %d\n",count);

return count;

void create(){

struct node *newnode;

int choice = 1;

head = NULL ;

while(choice){

newnode = (struct node*)malloc(sizeof(struct node));

printf("enter the data\n");

scanf("%d", &newnode->data);

newnode->next = NULL;

newnode->prev = NULL;

if(head == NULL){

head = tail = newnode;

else{

11
0801IT221017

tail->next = newnode;

newnode->prev = tail;

tail = tail->next;

printf("do you want to continue 0/1 \n");

scanf("%d",&choice);

void display(){

printf("display\n");

struct node *temp;

temp = head;

if(head == NULL){

printf("list is empty");

else{

while(temp != NULL){

printf(" %d",temp->data);

temp = temp->next;

printf("\n");

void insertAtBeg(){

struct node *newnode;

newnode=(struct node*)malloc(sizeof(struct node));

printf("enter data");

scanf("%d",&newnode->data);

12
0801IT221017

newnode->next=NULL;

newnode->prev=NULL;

if(head==NULL){

head=newnode;

else{

newnode->next = head;

head = newnode;

void insertAtEnd(){ struct node *newnode;

newnode=(struct node*)malloc(sizeof(struct node));

printf("enter data\n");

scanf("%d",&newnode->data);

newnode->next = NULL;

newnode->prev = NULL;

if(head==NULL){

head = tail = newnode;

else{

tail->next=newnode;

newnode->prev=tail;

tail=newnode;

void insertAtPos(){

13
0801IT221017

struct node *newnode, *temp;

temp = head;

int pos, i=1 , l ;

printf("enter the position\n");

scanf("%d",&pos);

l = length();

if(pos<1||pos>l){

printf("invalid positiion");

else if(pos == 1){

insertAtBeg();

else{

newnode = (struct node*)malloc(sizeof(struct node));

newnode->next = NULL;

newnode->prev = NULL;

while(i<pos-1){
temp=temp->next;
i++;

printf("enter data");

scanf("%d",&newnode->data);

newnode->next = temp->next;

newnode->prev = temp;

temp->next = newnode;

void reverse(){

struct node*nextnode, *current;

14
0801IT221017

if(head == NULL){

printf("List is empty\n");

else{ current=head;

while(current!=NULL){

nextnode=current->next;

current->next=current->prev;

current->prev=nextnode;

current=nextnode;

current = head;

head=tail;

tail=current;

void insert(){

struct node *newnode,*temp;

int choice;

temp = head;

printf("enter 1 for inserting at begining\n");

printf("enter 2 to insert at end\n");

printf("enter 3 to insert at any position\n");

scanf("%d",&choice);

if(choice == 1){

insertAtBeg();

else if(choice == 2){

insertAtEnd();

15
0801IT221017

else{

insertAtPos();

void delfrombeg(){

struct node *temp;

temp = head;

if(head==NULL){

printf("list is empty");

else{

head = head->next;

head->prev = NULL;

free(temp);

void delfromend(){

struct node *temp;

temp=tail;

if(tail == NULL){

printf("list is empty\n");

else{

tail=tail->prev;

tail->next = NULL;

free(temp);

}}

16
0801IT221017

void delfromPos(){

int l,pos, i=1;

struct node *temp;

printf("enter the position\n");

scanf("%d",&pos);

temp = head;

l=length;

if(pos<1 || pos>l){

printf("invalid position\n");

else if(pos == 1){

delfrombeg();

else if(pos == l){

delfromend();

else{ while(i<pos){

temp = temp->next;

i++;

temp->prev->next = temp->next;

temp->next->prev=temp->prev;

free(temp);

void delete(){

int n;

17
0801IT221017

printf("enter 1 to delete from begining\n");

printf("enter 2 to delete from end\n");

printf("enter 3 to delete from any position\n");

scanf("%d",&n); if(n==1){

delfrombeg();

else if(n==2){

delfromend();

else{

delfromPos();

int main() {

printf("ENROLLEMENT NO:0801IT221017\n");

printf("DOUBLY LINKED LIST\n");

int choice, m=1 ;

while(m){

printf("ENTER 1 TO CREATE\n ENTER 2 TO INSERT \n ENTER 3 TO DELETE \n ENTER 4 TO REVERSE \n


ENTER 5 TO DISPLAY\n");

scanf("%d",&choice);

switch(choice){

case 1 : create();

break;

case 2 : insert();

break;

case 3 : delete();

break;

case 4 : reverse();

18
0801IT221017

break;

case 5 : display();

break;

default : printf("invalid input\n ");

printf("do you want to continue\n");

scanf("%d",&m);

return 0;

OUTPUT ;

19
0801IT221017

20
0801IT221017

Q 3. WRITE A PROGRAM TO DISPLAY ALL THE FUNCTION IN DOUBLY CIRCULAR LINKED LIST.
ANS.

#include<stdio.h>
#include<stdlib.h>
#include<math.h>

struct node
{
int data;
struct node *next ;
struct node *prev;
} *head,*tail;

int l;
int length(){

struct node *temp;

int count=1;

temp = head;
while(temp!=tail){
count++;

l = count;

temp = temp->next;
}
printf("length of list %d\n",count);
return count;

}
void create(){

21
0801IT221017

struct node *newnode;


int choice = 1;
head = NULL ;
while(choice){
newnode = (struct node*)malloc(sizeof(struct node));
printf("enter the data\n");
scanf("%d", &newnode->data);
newnode->next = newnode;
newnode->prev = newnode;
if(head == NULL){
head = tail = newnode;

}
else{
tail->next = newnode;
newnode->prev = tail;
newnode->next = head;
tail = tail->next;
}
printf("do you want to continue 0/1 \n");
scanf("%d",&choice);
}
}
void display(){
printf("display\n");
struct node *temp;
temp = head;
if(head == NULL){

22
0801IT221017

printf("list is empty");
}
else{

while(temp != tail){
printf(" %d ",temp->data);
temp = temp->next;

}
printf("%d",tail->data);
}
printf("\n");
}
void insertAtBeg(){
struct node *newnode;
newnode=(struct node*)malloc(sizeof(struct node));
printf("enter data");
scanf("%d",&newnode->data);
newnode->next=newnode;
newnode->prev=newnode;
if(head==NULL){

head=tail=newnode;

}
else{
newnode->next = head;
head->prev = newnode;
newnode->prev = tail;
head=newnode;
tail->next=head; }

23
0801IT221017

}
void insertAtEnd(){
struct node *newnode;
newnode=(struct node*)malloc(sizeof(struct node));
printf("enter data\n");
scanf("%d",&newnode->data);
newnode->next = newnode;
newnode->prev = newnode;
if(head==NULL){

head = tail = newnode;

}
else{
newnode->next=tail->next;
tail->next=newnode;
newnode->prev=tail;
tail=newnode;
head->prev=tail;
}
}
void insertAtPos(){
struct node *newnode, *temp;
temp = head;
int pos, i=1 ;
printf("enter the position\n");
scanf("%d",&pos);
l=length();
/* if(pos<l || pos>l){ printf("invalid positiion");

24
0801IT221017

}*/
if(pos == 1){

insertAtBeg();

}
else{
newnode = (struct node*)malloc(sizeof(struct node));
newnode->next = NULL
newnode->prev = NULL;
while(i<pos-1){
temp=temp->next;
i++;
} printf("enter data");
scanf("%d",&newnode->data);
newnode->next = temp->next;
newnode->prev = temp;
temp->next = newnode;
newnode->next->prev=newnode;
}
}
void reverse(){
struct node *temp, *prevnode, *nextnode;
temp = head;
while(temp != tail){
prevnode = temp->prev;
nextnode = temp->next;
temp->prev = nextnode;
temp->next = prevnode;
temp=nextnode;

25
0801IT221017

}
temp->next=temp->prev;
temp->prev = head;

head = tail;

tail = tail->prev;

}
void insert(){
struct node *newnode,*temp;
int choice;
temp = head;

printf("enter 1 for inserting at begining\n");


printf("enter 2 to insert at end\n");
printf("enter 3 to insert at any position\n");
scanf("%d",&choice);

if(choice == 1){
insertAtBeg();
}
else if(choice == 2){
insertAtEnd();

}
else{
insertAtPos();
}
}
void delfrombeg(){
struct node *temp;

26
0801IT221017

temp = head;
if(head==NULL){
printf("list is empty");

}
else if(head->next == head ){
head=tail=NULL;
free(temp);
}
else{
head=head->next;
tail->next=head;
head->prev=tail;
free(temp);
}
}
void delfromend(){
struct node *temp;
temp=tail;
if(tail == NULL){
printf("list is empty\n");
}
else if(temp->next = tail){
head = tail= NULL;
free(temp);
}
else{ tail=tail->prev;
tail->next = temp->next;

27
0801IT221017

head->prev = tail;
free(temp);

}
}

void delfromPos(){
int l,pos, i=1;
struct node *temp;
printf("enter the position\n");
scanf("%d",&pos);
temp = head;
length();
if(pos<1 || pos>l){
printf("invalid position\n");
}
else if(pos == 1){
delfrombeg();

}
else if(pos == l){
delfromend();
}
else{ while(i<pos){
temp = temp->next;
i++;
}
temp->prev->next = temp->next;

28
0801IT221017

temp->next->prev=temp->prev;
if(temp->next==head){
tail=temp->prev;
}
free(temp);
}
}

void delete(){
int n;
printf("enter 1 to delete from begining\n");
printf("enter 2 to delete from end\n");
printf("enter 3 to delete from any position\n");
scanf("%d",&n);
if(n==1){
delfrombeg();
}
else if(n==2){
delfromend();
}
else{

delfromPos();

}}
int main() {

printf("ENROLLEMENT NO:0801IT221017\n");
printf("CIRCULAR DOUBLY LINKED LIST\n");

int choice, m=1 ; while(m){

29
0801IT221017

printf("ENTER 1 TO CREATE\n ENTER 2 TO INSERT \n ENTER 3 TO DELETE \n ENTER 4 TO


REVERSE \n ENTER 5 TO DISPLAY\n");
scanf("%d",&choice);
switch(choice){ case 1 : create(); break; case 2 : insert(); break; case 3
: delete(); break; case 4 : reverse(); break; case 5 : display(); break;
default : printf("invalid input\n "); }
printf("do you want to continue\n");
scanf("%d",&m);

return 0;
}
OUTPUT :

30
0801IT221017

31

You might also like