Linked Lists

I'm trying to make a function that inserts a node where I want.

This is the form of the function: add(char *name, int ent).

If I assign 3 to ent, the name should be placed third.

Also I need to make a function that flips the whole list.

Please somebody help me.

This is what I've got so far.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include "stdafx.h"
#include <iostream>
using namespace std;
void add(char *addname);
void add(char *addname, int entrynum);
void output();
void reverse();
struct person
{
	char *name;
	person *next;
};
person *newPerson;
person *head = NULL;
person *currentPerson;
int main()
{
	newPerson = new person;
	newPerson->next = NULL;
	head = newPerson;
	
	add("Adam");
	add("Ben");
	add("Chris");
	add("Derek");
	add("Earl");
	add("Trump", 3);    <- For this, Trump should go btw Ben and Chris.
	output();
	cout << endl << endl;
	delete newPerson;
    return 0;
}
void add(char *addname)
{
	person *newFace = new person;
	newFace->name = addname;
	newFace->next = NULL;
	newPerson->next = newFace;
	newPerson = newFace;
}
void add(char *addname, int entry)     <- I'm having trouble w/ this function,
{
	person *newFace = new person;
	newFace->name = addname;
}
void output()
{
	currentPerson = head->next;
	while (currentPerson != NULL)
	{
		cout << currentPerson->name << endl;
		currentPerson = currentPerson->next;
	}
}
void reverse()      <- and this.
{

} 
Last edited on
OP: dhayden's quote from here: http://www.cplusplus.com/forum/beginner/208101/#msg981165

Normally a list uses two classes: one for the list itself and one for a node within the list. You're using one class to represent both, which is a little unusual.
is also relevant in your case. you should streamline your design and there are several examples on this forum about inserting/deleting nodes based on such design: for eg. this http://www.cplusplus.com/forum/beginner/208086/#msg981383
I'm sure you'll find many more online as well
Topic archived. No new replies allowed.