In: Computer Science
I was supposed to conver a singly linked list to a doubly linked list and everytime I run my program the output prints a bunch of random numbers constantly until I close the console.
Here is the code.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
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(struct
node));
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 using
previous 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 the
position
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 the
position
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<position && temp != NULL; i++)
{
temp = temp-> next;
}
//if position is 1, move the head pointer to next and free node
and 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 node
and 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 tail
pointer
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;
}
code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
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(struct node));
temp-> data = newelement;
//close link of new node
temp->next = NULL;
temp->prev = NULL;
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 using previous 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 the position
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 the position
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<position && temp != NULL; i++)
{
temp = temp-> next;
}
//if position is 1, move the head pointer to next and free node and 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 node and 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 tail pointer
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()
{
//tail is already dedclared global varialbe
// 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;
}
output: