C/C++ :: Print Name And Ages Forward Then Reverse Using Double Linked List
Apr 28, 2015
I have to write a c program that will allow the user to enter names with ages. The name will be no longer than 40 characters. The name and age should be stored in a node of a doubly linked list. I cant use global data. I need to use 3 subroutines. the first one needs to enter the data and pass the head and tail pointers to the subroutine. the next subroutine needs to print the names and ages to the screen and file output.txt from first to last. the last subroutine needs to print out names and ages to the screen and file output.txt from the last to first.
Im getting several errors when i try to run this program. The first subroutine to get the data from the user and build the double linked list. but i seem to be having issues.
#include <stdio.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int entry(char [][41], int []); // subroutine used for data entry //
void printit(char [][41], int [], int); // subroutine used for data printing //
void reverseprintit(char [][41], int [], int); // subroutine used for reverse data printing //
Implement a recursive function named void printBack(DoublyLinkedNode<T>* node) for the class DoublyLinkedCircularList which will print out the elements in the list from back to front. The function is initially called with the first node in the list. You may not make use of the previous(prev) links
This is my solution where I got 2 out of a possible 3 marks:
template<class T> void DoublyLinkedCircularList<T> :: printBack(DoublyLinkedNode<T>* node) { if(node->next == NULL) //Correct- 1 mark return 0; else printBack(node->next); //Correct - 1 mark cout << current-> element << " "; }
I was assigned to reverse a unidirectional linked list in C, where I'm not allowed to make a copy of the given linked list, or to change the actual data.
The only thing I'm allowed to do is to manipulate the list's pointers. After doing some thinking and scribbling on a piece of paper, I came up with this:
Initialize three pointers: p1,p2,p3, for the first, second and third elements, respectively. p1->next=NULLwhile (p3 != NULL) do p2->next=p1p1=p2p2=p3p3=p3->nextp2->next=p1return p2 as the head of the reversed list
This is a pseudo code, of course, as I'm less interested in the actual implementation, and more about the algorithm itself. I was wondering if there's a more elegant way of doing this, since this gets quite complicated considering the cases where the list has less than three elements in it.
I am trying to create a linked list and then reverse it. So far I have created the link list however I am having difficulties figuring out the steps to reverse it. What is the logic behind reversing this linked list. I am not able to use recursion. I am supposed to create a copy of the linked list with the nodes reversed.
I was trying to reverse a linklist in reverse direction using the recursion. I was able to reverse n - 1 element but it is not printing the first one. Below is my code.
Code:
typedef struct linklist { int data; linklist *next; };
void add(int data,linklist **node) {
[code]....
This is happening since recursion is starting from second node, which is due to reason not printing the first one when recursion print values from stack once
node != NULL
Condition is met.
Currently I am using below statement for printing the first element;
reverse_recur(node); printf(" Print In Reverse Order %d ",node->data);
1. Construct a class diagram that can be used to represent food items. A type of food can be classified as basic or prepared. Basic food items can be further classified as meat, fruit, veg or Grain. The services provide by the class should be the ability to enter data for new food, to change data for food and to display existing data about food.
using this class definition write a main program to include a simple menu that offers the following choices:
1. Add food 2. Modify Food 3. Delete Food 4. Exit this menu
2. Read a list of numbers from a file and then print them out in reverse order. State whether the list is palindromic or not.
I'm having a small issue here with my linked list.I built a linked list with strings and it worked perfectly.Now since i'm using strtok() to separate the string.for now here's what i've got:
I'm having trouble inserting a node in a nth position into a double linked list. My for loop is giving me an exception handler error and I can't link the node after the new node is inserted back to the new node. Here is my code,
void mylist::insertpos(const int &pos, const string &s, const int & c) { listnode* temp1 = new listnode(s,c); temp1->s =s; temp1->next = NULL;
[Code]....
I attached my header file incase you need to see the definitions for my objects.
So im trying to write a code that will take data in from a user and when that user enters specific character then i want to pop the top of the stack and place that node on a new stack. Then if the user enters a different specific character then i want to place what was just popped off the stack back on to the original stack. Anyways i'm just testing what i have without all the complicated stuff with the specific characters, but i get an error and i'm not not well versed in exception handling. So this is what i have so far. the push seems to work well but the pop seems to be giving me problems.
Stack::Stack(){ top = NULL; bottom = NULL; } //method to push a line of text onto the stack void Stack::push(string data)
I am developing a double linked list in C ( development environment ) is QT with mingw32 4.7 gcc / g++ compiler , in the header file I have defined struct as follows :
When compiling I am getting the following error : 'NODE' undeclared (first use in this function)
and
each undeclared identifier is reported only once for each function it appears in
I have also attached the screen shot from the QT IDE
look's like the compiler is not able to pick up the definition of NODE structure , same happens in Netbeans IDE , interesting thing is if change the source file from c to cpp the error goes away .
I am trying this without a head/start pointer which would generally hold the address of first node. I have 3 nodes here from which I am trying to delete last node, but its not happening. I might be wrong in my logic and this is my first linked list program.
add of p1::0x9605008 add of p2::0x9605018 add of p3::0x9605028 add of p1->prev::(nil) add of p1->next::0x9605018 add of p2->prev::0x9605008 add of p2->next::0x9605028 add of p3->prev::0x9605018 add of p3->next::(nil)
no of nodes 3
enter the addresss of node to delete it 0x9605028
after deletion attempted
add of p1::0x9605028 add of p2::0x9605018 add of p3::0x9605028 add of p1->prev::0x9605018 add of p1->next::(nil) add of p2->prev::0x9605008 add of p2->next::0x9605028 add of p3->prev::0x9605018 add of p3->next::(nil)
no of nodes 3
In this example i am trying to delete the node 3 which is p3, by deleting its address 0x9605028. But after deletion the node count is still 3 and addresses are like unexpected!
I am doing c++ program to read coordinates from text file into double linked list. Everything seems to work perfectly but it stores only 298 items in linked list. Im not sure if my code is wrong or I missed something. On the debugger length of list is over a 1000 but like I said it print outs only 298.
A while ago i was asked to write a program for a class that used a "Double ended queue with a current-position marker."
An example of some of the functions that i created are:
boolean atFirst() // Returns true if first element is current. Pre: !isEmpty(). boolean atLast() // Returns true if last element is current. Pre: !isEmpty(). void makeEmpty() // Sets this List to the empty state. Post: isEmpty(). void moveFirst() // Sets current marker to first element. void movePrev() // Moves current marker one step toward first element. void moveNext() // Moves current marker one step toward last element. void insertBeforeFirst(int data) // Inserts new element before first element.
My question is whether a double ended queue with pointer is the same thing as a "doubly linked list" in this case. The terminology is throwing me of a little. If the two concepts are different, how is a doubly linked list different?
Well, basically, what I've been doing was creating a class that would implement the concept of Double Linked List, but that would behave like a queue ( the STL implementation is deque, or Double Ended Queue ).
The problem occured when I have been trying to generalize the class using templates. I mean, why use only integers ? Why not double or char or what not ?
Worked perfectly before including all the template stuff..
// Source.cpp <=> Main.cpp #include <iostream> #include "DList.h" using namespace std; int main(void) { DList<int> *list; list = new DList<int>();
[Code] .....
The errors returned by the compiler:
Error1error C2955: 'Node' : use of class template requires template argument listc:usersjumperdesktopc++ otherdouble linked listdouble linked listdlist.h6 Error2error C2955: 'Node' : use of class template requires template argument listc:usersjumperdesktopc++ otherdouble linked listdouble linked listdlist.h6
I was assigned to print a linked list but as a vector of char (I cannot use the normal string type) , this is what I have:
char* List::asString(){ Node* ite = new Node(); ite= first;//ite is like an iterator of the list for(int i=0; i<sizeOfList; ++i){//sizeOfList is the total of node of the list
[Code] ....
But when I print that, I get a bunch of weird symbols...
I'm trying to print a single linked list backward with functions/classes. I have created the main file and 3 header files. But I'm getting an error on one of the header files, linkedListIterator after adding #include "linkedListType.h". It says that "linkedLlistType.h" is calling itself. And when I try to run it, I get an error of "too many header files." I have tried changing the headers many times, but nothing seems to work.
.cpp file:
/*(Printing a single linked list backward) Include the functions reversePrint and recursiveReversePrint, as discussed in this chapter, in the class linkedListType. Also, write a program function to print a (single) linked list backward. (Use either the class unorderedLinkedList or the class orderedLinkedList to test your function.)*/
Here's what I have to do: Using strcpy() pass it two strings of characters (such as a name like John Johnson) and copy the second string into the first, in reverse order, and print both. Write a main to exercise your function and to illustrate that the function is working properly.
I'm currently learning how to work with linked lists, specifically, doubly linked lists, and I have come across a problem with my program when I attempt to print it backwards. Here is the portion of the code :
#include <iostream> using namespace std; struct node { int data; //int to store data in the list node *next; //pointer to next value in list node *prev; //pointer to previous value in list
[Code] ....
The purpose of this program is to create a list, print it forwards, backwards, insert an element, erase an element, and then destroy the list. I have chopped it down to just the create, print forward, and print backward functions.
The issue I have is that in the createList function I am modifying both begin and end but I can only return one or the other. This means that whichever I don't return is still NULL in the main function and therefore does not point to anything. I've tried setting the begin/current/end to not equal NULL but createList won't work if I do that.
How I could modify both? Just to be clear, the list HAS TO be created in a function, it would be very easy to just initialize it in the main...
I would like to add a new function to my class, "reverse" that would reverse the order of the list. I have added a prototype in the "public:" section of the code (see the comment with PROTOTYPE in it). Your task is to complete the implementation (see the comment with IMPLEMENTATION in it. See the main() function to see how the results should look
#include #include using namespace std; class List { public: // Constructs an empty list List();
[Code]....
I cant seem to get the right function to put into reverse I have tried everything Im not sure where to start!
Using the array to accept 10 testscore. Calculate and print the highest, lowest, fifth test score entered and all the test score entered in reverse order.
How i would get it to print the highest,and lowest and in reverse order. I'm confused as to what to do...
I am on a little project involving TCP socket-programming and multiple threads but need passing structures between two threads.
The first thread receives data with respective ID, temp, etc,.. Each ID is a list-item of a linked list and every ID has again a linked list of all the received data.
The second thread just have to access the list and perform some manipulations on it.
My solution: pass the head pointer of the linked list. Easy enough, right? Somehow, I am stuck receiving error message:" dereferencing pointer to incomplete type."
For the sake of ease and simplicity I just added a stripped down version of the project.
The magic (or not) happens in MainProcess.c: The Data thread should receive the pointer (a think) from the Connection thread.