I am trying to make a circular linked list and I think I am not linking it properly in the middle area because I can print the last and first node, but not the rest...

#include <iostream>
#include <new>
using namespace std;

struct node
{  int  Item;
   node* Next;
}; // end struct

typedef node* ptrType;
ptrType Head = NULL; // for zero-length list
ptrType Temp, Last;

// create circular list of n elements 
void add()
{
	int n;
	cout << "What is n? ";
	cin >> n;
	if (n > 0)
	{
		Head = new(node);
		Head -> Item = n;
		Temp = Head->Next;
		for (int i = n; i >= 1; i--)
		{
			Temp = new(node);
			Temp -> Item = i;
			Temp -> Next = Temp;
			Last = Temp;
		}
		Last -> Next = Head; // to make the list circular
	}
	else; // do nothing, n == 0 means list is empty
}

void print_circle(ptrType Head)
{
	if(Head!=NULL)
	{
		cout << Last->Next->Item;
		cout << Last->Item;
		cout << Last->Next->Next->Item;//problem is here!@!@!
		ptrType First = Last -> Next;
		Temp = First;
		while(Temp != First)
		{
			cout << Temp -> Item << endl;
			Temp = Temp -> Next;
		}
	}
}

int main()
{
	add();
	print_circle(Head);
	cout << "\nran through functions";

	char end;
	cin >> end;
	return 0;
}

Recommended Answers

All 5 Replies

That didn't look like a circular linked list. Anyway, I made a few corrections and it worked fine.

void add()
{
    int n;
    cout << "What is n? ";
    cin >> n;
    if (n > 0)
    {
        Head = new(node);
        Head -> Item = n;
        Last = Head;
        for (int i = n-1; i >= 1; i--)
        {
            Temp = new(node);
            Temp -> Item = i;
            Last -> Next = Temp;
            Last = Temp;
        }
        Last -> Next = Head;
    }
    else;
}
void print_circle(ptrType Head)
{
    if(Head!=NULL)
    {
        Last = Head;
        while (Last->Next != Head)
        {
            cout<<Last->Item;
            Last = Last->Next;
        }
        cout<<Last->Item;
    }
}

But I would suggest you to try working it out yourself.

thanks for help, but I'm still getting the same error message.. here is what it says

What is m? 5
5
Unhandled Exception: System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
at print_circle(node* Head) in c:\users\joe\desktop\lab2\lab2\mylist.cpp:line42
at main() in c:\users\joe\desktop\lab2\lab2\mylist.cpp:line 54
at _mainCRTStartup()

We can't see your corrected code, but why do you feel you have to send Head to print_circle() but not to add()? Since Head is declared with global scope on line 11 it can be used anywhere in the program and shouldn't need to be sent anywhere. That can be good, or bad, depending on the circumstances, though generally variables defined with global scope are discouraged whenever possible.

it was in the lab to send it that way. idk why. I'm used to using templates and OOP design when messing with linked list bc it's easier for me to understand... anyways here is what I have now.

#include <iostream>
#include <new>
using namespace std;

struct node
{  int  Item;
   node* Next;
}; // end struct

typedef node* ptrType;
ptrType Head = NULL; // for zero-length list
ptrType Temp, Last;

// create circular list of n elements 
void add()
{
	int n;
	cout << "What is n? ";
	cin >> n;
	if (n > 0)
	{
		Head = new(node);
		Head -> Item = n;
		Last = Head;
		for (int i = n-1; i >= 1; i--)
		{
			Temp = new(node);
			Temp -> Item = i;
			Temp -> Next = Temp;
			Last = Temp;
		}
		Last -> Next = Head; // to make the list circular
	}
	else; // do nothing, n == 0 means list is empty
}

void print_circle(ptrType Head)
{
	if(Head!=NULL)
	{
		Last = Head;
		while(Last -> Next != Head)
		{
			cout << Last -> Item;
			Last = Last -> Next;
		}
		cout << Last -> Item;
	}
}

int main()
{
	add();
	print_circle(Head);
	cout << "\nran through functions";

	char end;
	cin >> end;
	return 0;
}

The error message you posted looks like it has to do with print_circle(), not the cout statement you posted immediately above it. The only possible problem I see with print_circle is that you are passing a global variable. I'd try redeclaring print_circle() to take no arguments, make appropriate adjustment to the function call, and recompile.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.