// Sample program to illustrate a doubly-linked list #include <iostream> using n
ID: 3561354 • Letter: #
Question
// Sample program to illustrate a doubly-linked list
#include <iostream>
using namespace std;
struct Node {
Node* pPrev;
int data;
Node* pNext;
}; // don't forget the semicolon
void displayListForward( Node* pHead)
{
while( pHead != NULL) {
cout << pHead->data << " ";
pHead = pHead->pNext;
}
cout << " ";
}
void displayListReverse( Node* pTail)
{
// Iterate backwards, using the pPrev pointers
while( pTail != NULL) {
cout << pTail->data << " ";
pTail = pTail->pPrev;
}
cout << " ";
}
void displayListTraverse( Node* pTail, Node* pHead)
{
while( pTail != NULL) {
pTemp->pPrev = pHead;
pTemp->pNext = pHead;
}
cout << " ";
while( pTail != NULL) {
cout << pTail->data << " ";
pTail = pTail->pPrev;
}
cout << " ";
}
// Prepend node to beginning of list
void prependNode( Node * &pHead, Node * &pTail, int input)
{
// Get a new node and add the data
Node *pTemp = new Node;
pTemp->data = input;
// Reset pointers depending if this is the first node
if( pHead == NULL) {
pTemp->pPrev = pTemp;
pTemp->pNext = pTemp;
pTail = pTemp;
}
else {
// Prepend to existing node
pTemp->pPrev = pHead->pPrev;
pTemp->pNext = pHead;
pHead->pPrev = pTemp; // link current list head node back to this new one
}
// reset head pointer
pHead = pTemp;
}
int main()
{
Node *pHead = NULL;
Node *pTail = NULL;
int input =0;
cout << "Starting program..." << endl;
// Prompt for numbers to be added to list, followed by -1
cout << "Enter integers, followed by -1: ";
do {
// read input
cin >> input;
// break if end of input
if( input == -1) {
break; // end of input, don't add it to list
}
prependNode( pHead, pTail, input);
// display the list
displayListForward( pHead);
displayListReverse( pTail);
cout << endl;
} while( input != -1);
system("pause");
return 0;
}
This is my code.
Things i have to do is:
please help me
Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
/* structure for a node */
struct node
{
int data;
struct node *next;
};
/* Function to insert a node at the begining of a Circular
linked list */
void push(struct node **head_ref, int data)
{
struct node *ptr1 = (struct node *)malloc(sizeof(struct node));
struct node *temp = *head_ref;
ptr1->data = data;
ptr1->next = *head_ref;
/* If linked list is not NULL then set the next of last node */
if (*head_ref != NULL)
{
while (temp->next != *head_ref)
temp = temp->next;
temp->next = ptr1;
}
else
ptr1->next = ptr1; /*For the first node */
*head_ref = ptr1;
}
/* Function to print nodes in a given Circular linked list */
void printList(struct node *head)
{
struct node *temp = head;
if (head != NULL)
{
do
{
printf("%d ", temp->data);
temp = temp->next;
}
while (temp != head);
}
}
/* Driver program to test above functions */
int main()
{
/* Initialize lists as empty */
struct node *head = NULL;
/* Created linked list will be 12->56->2->11 */
push(&head, 12);
push(&head, 56);
push(&head, 2);
push(&head, 11);
printf("Contents of Circular Linked List ");
printList(head);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.