How to write a progam to creation and display of elements in both forward and reverse direction in a doubly linked list in C Programming Language ?
https://en.wikipedia.org/wiki/Doubly_linked_list
Solution:
/* Creation and display of elements in both forward and reverse direction in a doubly linked list */
#include <stdio.h>
#include <stdlib.h>
struct node* create(struct node *, struct node **, int);
void display(struct node*);
void displays(struct node*, struct node*);
struct node
{
int data;
struct node *rptr, *lptr;
};
int main()
{
struct node *head, *tail;
head= NULL;
tail= NULL;
int i, n, value;
printf("\nEnter the number of values u want to enter\n");
scanf("%d", &n);
for(i=0; i<n; i++)
{
printf("\nEnter the number you want to enter\n");
scanf("%d",&value);
head=create(head, &tail, value);
}
printf("\nThe data in forward direction is printed below\n");
display(head);
printf("\nThe data in backward direction is printed below\n");
displays(tail, head);
}
struct node* create(struct node *head1, struct node **tail1, int dat)
{
struct node* newnode, *temp;
newnode= (struct node*) malloc (sizeof(struct node));
newnode->data=dat;
newnode->rptr= newnode->lptr= NULL;
if(head1 == NULL)
{
newnode->lptr=newnode->rptr=NULL;
head1=newnode;
}
temp=head1;
while(temp->rptr != NULL)
temp=temp->rptr;
temp->rptr= newnode;
newnode->lptr=temp;
newnode->rptr=NULL;
*tail1 = newnode;
temp=temp->rptr;
return head1;
}
void display(struct node* head)
{
while(head!= NULL)
{
printf("%d\n",head->data);
head=head->rptr;
}
}
void displays(struct node *tail, struct node *head)
{
while (tail != head)
{
printf("%d\n", tail->data);
tail=tail->lptr;
}
if(tail == head)
printf("%d\n", tail->data);
}
Learn More :
Display
- DISPLAY SOURCE CODE AS OUTPUT IN C PROGRAM
- Sort Three Numbers - program reads in three Integers and displays them in ascending order.
- C Program To Display The Number In A Specific Formats
- C Program that Display a IBM Logo
- C program calculates a given function in range given by user, stores the data in arrays and displays the answer in a table.
- LED ON OFF For One Sec/Count and Display on the Attached Serial Monitor
- Pre Order, Post order, In order Implement Binary Tree using linked list
- C Program to accept m*n matrix from user and display the elements of given matrix using function
- C Program to accept n numbers & store all prime numbers in an array & display this result
- C program to display the transpose of given 3 X 3 matrix
- C Program to accept a string from user, delete all vowels from that string & display the result
- C Program To Create Two Singly Linked List and Perform Following Operation
- Create Two Singly Linked List Perform Differences Display It C Program
- C Program to Create, Display, Insert and Deletion of Queue Elements
- C Program to Display a real time clock (HH:MM:SS) on the LCD
- Program to Display Pie Chart Accepting User Input C Program
- Linked List For Getting Employee Details, Display and Search For Salary C Program
- Menu driven program in the creation,display,search, insertion and deletion of a node in the linked list
- Program to display the following pattern in C
- Display the Following Pattern * ** *** **** ***** C Program
Doubly Linked List
Create
- How To Write a C program that creates customers' bills for a carpet company when the following is given ?
- C Program to Create VIRUS
- Creates new main.c with empty main boilerplate template
- C Program to create a solution to the Towers of Hanoi game
- C Program to Create a copy of an image
- Ardunio: Create a crystal ball to tell you the future C Program
- C Program to Creating pico device
- Add x and y coordinates to create a new struct in C Program
- C program allocates new nodes and creates a four element list with fixed values
- C Program To Create Two Singly Linked List and Perform Following Operation
- Create Two Singly Linked List Perform Differences Display It C Program
- C Program Implement Binary Search Tree And Its Traversals
- C Program to Create, Display, Insert and Deletion of Queue Elements
- Calculator Using IF-ELSE in C Program
Element
- Given a numerical value, check if at least one of the elements of the vector is equal to the numerical value if so, say where in the negative case, say that there is no.
- C program to Given a vector (integer or real), determine what is the maximum value of element what is the position in which this element is located
- Write the procedure , which is one of a sum , a product and a geometric average in the panel for the NxM elements are located on opposite diagonal and main diagonal . Remind ! Counting only odd elements !
- Napisać funkcję obliczającą funkcję geometryczną w tablicy NxM elementowej z elementów o wartościach parzystych znajdujących się pod główną i ponad przeciwną przekątną.
- C Program Array NxM Elements Geometric/Arithmetic
- C Program To Returns the nth element of the Fibonacci sequence.
- C Program to search an element using linear search or binary search (menu driven program)
- Fibonacci Multi Threaded C Program To Print Element Of Fibonacci Series
- C Program to accept n numbers from user & find out the maximum element out of them by using dynamic memory allocation
- C Program to accept m*n matrix from user and display the elements of given matrix using function
- C program to reverse an array elements using Dynamic Memory Allocation
- C Program to calculate the sum of elements of upper triangle of a n*n matrix using DMA
- C Program to accept n numbers from user store these numbers into an array & reverse an array elements using function
- C Program to accept 5 names from user & store these names into an array. Sort these array elements in alphabetical order
- Calculate sum of element of upper triangle of m*n matrix by using dynamic memory allocation
- Calculate sum of non-diagonal element in m*n matrix C Program
- Calculate sum of element of lower triangle of m*n matrix by using dynamic memory allocation
- C Program to Implemention Bubble Sort using array
- C Program to Create, Display, Insert and Deletion of Queue Elements
Direction
Reverse
- C Program to Reverse a Word
- C Program Array Example: Reverse
- C Program To Find Reverse Of Any Digit Number
- C Program To Find The Length, Reverse Of A String And To Check It Is Palindrome or Not
- C Program To Reverse A Number
- C Program To Reverse The String Using Pointer
- C Program To Sort An Array Of Names In Alphabetical And Reverse Order
- C Program To Reverse A Given Number
- C Program To Reverse A Number
- C Program to Reversing a Five Digit Integer Number
- C program to reverse an array elements using Dynamic Memory Allocation
- C Program to accept n numbers from user store these numbers into an array & reverse an array elements using function
- Input Number and Print it's Reverse C Program
Forward