Skip to content

Instantly share code, notes, and snippets.

@ravirajawasthi
Created May 31, 2020 13:22
Show Gist options
  • Save ravirajawasthi/b8c0a53efec62ebd68b32f83974cb17e to your computer and use it in GitHub Desktop.
Save ravirajawasthi/b8c0a53efec62ebd68b32f83974cb17e to your computer and use it in GitHub Desktop.
#include <bits/stdc++.h>
using namespace std;
struct Node
{
int data;
struct Node *next;
struct Node *prev;
}; //*head = NULL;
struct Node *head = NULL;
void append(int val)
{
cout << "inserting " << val << endl;
struct Node *temp, *newnode;
temp = head;
//this part is for creating new node
newnode = (struct Node *)malloc(sizeof(struct Node)); // allocating memory for new node
newnode->data = val; //assigning appropriate value to newnode
newnode->next = NULL; //next is null because it will be inserted in end
//This part is to appropriately insert newnode
if (head == NULL) //if linked list has no element in it
{
head = newnode;
}
else
{
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = newnode;
newnode->prev = temp;
}
}
void display()
{
struct Node *temp = head;
while (temp != NULL)
{
cout << "here";
cout << temp->data << " ";
temp = temp->next;
}
}
int main()
{
int val;
do
{
cin >> val;
if (val > 0)
append(val);
} while (val > 0);
display();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment