Inserting at certain position in Linked Lists

Hello,

suppose I have a struct named nodeType with(info of type Type, and a pointer named link of type nodeType)
also I have a class named linkedListType with private members (*first of type nodeType, *last of type nodeType, count of type integer)

This code to insert a new node at certain position
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
template<class Type>
void linkedListType<Type>::insertAt(const Type &item, int pos){
	if(pos<=0 || pos>count)
	{
		cout<<"Incorrect position"<<endl;
	}
	else{
		nodeType<Type> *newNode;
		newNode=new nodeType<Type>;
		assert(newNode!=NULL);
		newNode->info=item;

		if(pos==1 && pos<count){
			newNode->Link=first;
			first=newNode;
		}
		else if(pos==count){
			newNode->Link=NULL;
			last=newNode;
		}
		else{
			nodeType<Type> *cur=first, *tail=NULL;
			for(int i=0; i<pos-1; i++)
			{
				tail=cur;
				cur=cur->Link;
			}

			newNode->Link=cur;
			tail->Link=newNode;
		}
		count++;
	}
}

Is my code right?
Also, do I need to delete (cur & tail) when their work is over?
Ruthless wrote:
Is my code right?

Have you tested it? Writing test units is a very important skill to have. I recommend doing this, and if something is broken then come back and tell us what doesn't work and we'll be glad to help you from there.

Ruthless wrote:
Also, do I need to delete (cur & tail) when their work is over?

You only delete objects that were created with new. Since cur and tail are just pointers you created on the stack, they will be gone by the time this function exits.
For some reason I am unable to run my program.. I always get these 2 errors

1>MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
1>c:\users\ruthless\documents\visual studio 2012\Projects\LinkedList\Debug\LinkedList.exe : fatal error LNK1120: 1 unresolved externals

I googled about this and tried multiple things, still same problem.
Yes I am sure that I started a new project in the right way, but I have no clue what is this error about.

That is why I asked if this function is right or no, I need to know how to write it in the correct way for tomorrow and I am unable to test it.
Well these are linker errors. You created an empty project right?
Yes it is an empty project.

Well, I just noticed that I have an extra template for a function I didn't write so the compiler thought its with the main function.
I removed it now and my program worked.

Everything works fine, but when I choose a position equals to the length, nothing changes.

It turned out that code is useless
1
2
3
4
5
		else if(pos==count){
			newNode->link=NULL;
			last->link=newNode;
			last=newNode;
		}


I am now able to insert at all available positions except if I want to insert at the end, couldn't figure out how.
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
template<class Type>
void linkedListType<Type>::insertAt(const Type &item, int pos){
	if(pos<=0 || pos>count+1)
	{
		cout<<"Incorrect position"<<endl;
	}
	else{
		nodeType<Type> *newNode;
		newNode=new nodeType<Type>;
		assert(newNode!=NULL);
		newNode->info=item;

		if(pos==1 && pos<count){
			newNode->link=first;
			first=newNode;
		}
		else{
			nodeType<Type> *cur=first, *tail=NULL;
			for(int i=0; i<pos-1; i++)
			{
				tail=cur;
				cur=cur->link;
			}
			if(cur==NULL){
				cout<<"Last condition"<<endl;
				newNode->link=NULL;
				last->link=newNode;
				last=newNode;
			}
			else{
				newNode->link=cur;
				tail->link=newNode;
			}
		}
		count++;
	}
}


EDIT: I used count+1 in the first if statement and now everything is good.
Thank you.
Last edited on
Insertion at the end should be trivial. If you have a tail pointer, tail->next = newNode. If no tail pointer, iterate till the end then do the same.
Topic archived. No new replies allowed.