In C language dont use stdlibh or stringh ONLY use include o

In C language!!!!

\"don\'t use stdlib.h or string.h, ONLY use #include <stdio.h> or <stdbool.h>!!!\"

Using Pointers:

A doubly linked list is a list in which each entry contains a pointer to the preceding entry in the list as well as a pointer to the next entry in the list. Define the appropriate structure definition for a doubly linked list entry and then write a small program that implements a small doubly linked list and prints out the elements of the list.

Solution

The program woulbe be like.

#include<stdio.h>

//Below is the structure definition of doubly linked list.

struct List{

struct list *next; //to traverse in forward direction

struct * previous; //to traverse in backward direction

}*head,*last;

void createlist(int n) //function to create list

{

int i, element;
struct list *node;
if(n >= 1)
{
head = (struct list*)malloc(sizeof(struct list));
if(head != NULL)
{
printf(\"Enter data of 1 node: \");
scanf(\"%d\", &element);
head->element = element;
head->prev = NULL;
head->next = NULL;
last = head;
/*
* Creates rest of the n-1 nodes
*/
for(i=2; i<=n; i++)
{
node = (struct list *)malloc(sizeof(struct list));
if(node != NULL)
{
printf(\"Enter data of %d node: \", i);
scanf(\"%d\", &element);
node->element = element;
node->prev = last; //Links new node with the previous node
node->next = NULL;
   last->next = list; //Links previous node with the new node
last = node; //Makes new node as last/previous node
}
else
{
printf(\"Unable to allocate memory.\");
break;
}
}
printf(\"\ List created\ \");
}
else
{
printf(\"Unable to allocate memory\");
}
}

}

void showFirst() //function to display elements starting from first (head)

{ int n = 1;

if(head == NULL)
{
printf(\"List is empty.\");
}
else
{
temp = head;
printf(\"\ \ The elements traversing from starting:\ \");

while(temp != NULL)
{
printf(\"Element of %d node = %d\ \", n, temp->element);

n++;

/* Moves the current pointer to next node */
temp = temp->next;
}
}

}

void showLast() // function to display elements from last

{

struct node * temp;
int n = 0;

if(last == NULL)
{
printf(\"List is empty.\");
}
else
{
temp = last;
printf(\"\ \ Element traversing from last:\ \");

while(temp != NULL)
{
printf(\"element of last-%d node = %d\ \", n, temp->element);

n++;

/* Moves the current pointer to previous node */
temp = temp->prev;
}
}

}

void main()

{int n; //hold number of nodes

int i;

printf(\"Enter the number of nodes you want in the list\");

scanf(\"%d\", &n);

createlist(n);

printf(\"Enter 1 to display from first or 2 to display from last\");

scanf(\"%d\", & i);

switch(i)

{

case 1 :

showFirst();

break;

case 2 :

showLast();

break;

}

}

In C language!!!! \
In C language!!!! \
In C language!!!! \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site