I was supposed to conver a singly linked list to a doubly linked list and everytime...

90.2K

Verified Solution

Question

Programming

I was supposed to conver a singly linked list to a doubly linkedlist and everytime I run my program the output prints a bunch ofrandom numbers constantly until I close the console.

Here is the code.

#include
#include
#include
#include

struct node {
int data;

struct node *next;
struct node *prev;
};

//this always points to first link
struct node *head = NULL;

//this always points to last link
struct node *tail = NULL;

//return length of list
int sizeList (struct node *pointer)
{
int length = 0;
struct node *temp;

if(pointer == head)
{
for (temp = head; temp != NULL; temp = temp-> next)
length++;
}
else
{
for (temp = tail; temp != NULL; temp = temp-> prev)
length++;
}
return length;
}

//insert element from last node
struct Node* insertLast (struct node *tail, int newelement)
{
struct node *temp = (struct node*) malloc(sizeof(structnode));
temp-> data = newelement;

if (tail == NULL){
head = temp;
tail = temp;
}
else
{
tail-> next = temp;
temp-> prev = tail;
}
//point last to new last node
tail = temp;

return tail;
};

void printList(struct node *pointer)
{
int length = 0;
struct node *temp;

//check if pointer is head and print head of node
if (pointer == head)
{
for (temp = head; temp != NULL; temp = temp-> next)
printf(\"%d \", temp-> data);
}
//check if pointer is tail and print the list from tail node usingprevious pointer
else
{
for (temp = tail; temp != NULL; temp = temp-> prev)
printf(\"%d \", temp-> data);
}

printf(\"\n\");
}

int get (struct node *pointer, int position)
{
struct node *temp;
int length = sizeList(pointer);
int tempPosition = 0;

if (length < position)
{
printf(\"Enter the position with in %d.\", length);
return -1;
}
//check if pointer is head and return the element from theposition
if (pointer == head)
{
for (temp = head; temp != NULL; temp = temp-> next)
{
if(++tempPosition == position)
{
return temp-> data;
}
}
}
//check if pointer is tail and return the element from theposition
else
{
for (temp = tail; temp != NULL; temp = tail-> prev)
{
if (++tempPosition == position)
{
return temp-> data;
}
}
}
return 0;
}

//delete node from position
struct node* removeEle(struct node *head, int position)
{
struct node *temp = head, *deleteNode = NULL;
int i;

//traverse list until we get each position
for (i=1; i{
temp = temp-> next;
}

//if position is 1, move the head pointer to next and free nodeand return to head
if (position ==1)
{
deleteNode = temp;
temp = temp-> next;
temp-> prev = NULL;
head = temp;

free (deleteNode);
return head;
}

//if deleteNode is last position move the tail to previous nodeand free last node
else if (temp == tail)
{
deleteNode = tail;
tail = tail-> prev;
tail-> next = NULL;

free(deleteNode);
return head;
}
//swap temp pointers to skip node
else if (temp != NULL)
{
temp-> prev-> next = temp-> next;
temp-> next-> prev = temp-> prev;

free(temp); //delete n node
return head;
}
else
{
printf(\"It's an invalid position..\n\");
return head;
}
};

void reverseList(struct node *node)
{
//stop and return if node is null
if (!node)
return;

//swap next and prev pointer of node
struct node* temp = node-> next;
node-> next = node-> prev;
node-> prev = temp;

//node of prev is null stop and update head and tailpointer
if(!node-> prev)
{
head = node;
if(head-> next == NULL)
{
tail = head;
}
struct node *temp = head;
while(temp-> next != NULL)
{
temp = temp -> next;
}
tail = temp;
return;
}
//invoke recursive api
reverseList(node-> prev);
}

int main()
{
struct Node *tail = NULL;

//insert element at last
tail = insertLast(tail, 10);
tail = insertLast(tail, 20);
tail = insertLast(tail, 30);
tail = insertLast(tail, 40);
tail = insertLast(tail, 50);

printf(\"Printing from Head: \");
printList(head);
printf(\"\n\");

printf(\"Printing from Tail: \");
printList(tail);
printf(\"\n\");

printf(\"Size of list from Head: %d\n\", sizeList(head));
printf(\"Size of list from Tail: %d\n\", sizeList(tail));

//remove element from position 4
printf(\"Get the element from head at position 4: \");
int element = get(head, 4);

if(element != -1)
{
printf(\"%d\n\", element);
}

//remove element from position 2
printf(\"Get the element from tail at position 2: \");
element = get(tail, 2);

if(element != -1)
{
printf(\"%d\n\", element);
}

printf(\"Remove element from position 3\n\");
head = removeEle(head, 3);

printf(\"Printing from Head after removal: \");
printList(head);
printf(\"\n\");

//invoke reverse list
reverseList(head);

printf(\"Printing from Head after reverseList: \");
printList(head);
printf(\"\n\");

printf(\"Printing from tail after reverseList: \");
printList(tail);
printf(\"\n\");

return 0;
}

Answer & Explanation Solved by verified expert
3.7 Ratings (602 Votes)
code include include include include struct node int data struct node next struct node prev this always points to first link struct node head NULL this always points to last link struct node tail NULL return length of list int sizeList struct node pointer int length 0 struct node temp ifpointer head for temp head temp NULL temp temp next length else for temp tail temp NULL temp temp prev length return length insert element from last node struct node insertLast struct node tail int newelement struct node temp struct node mallocsizeofstruct node temp data newelement close link of new node tempnext NULL tempprev NULL if tail NULL head temp tail temp else tail next temp temp prev tail point last to new last node tail    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students