3 Replies - 2536 Views - Last Post: 30 October 2009 - 07:00 AM Rate Topic: -----

#1 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,405
  • Joined: 15-July 09

Linked List of Linked Lists

Posted 28 October 2009 - 11:25 PM

Well im aiming for a library that will work some how like a text editor.
So i thought about some kind of algorith that could go like this

read buffer if not 0 then add to list on the line, else if 0 read buffer again and see what key it is(something like enter, key down, esc .....) if the key is enter move to next line and that would be a second link list of line in the link list of column

My problem is big for me, so far im just trying to make the lines link lists work, then ill get into the columns linked lists(i have no idea on how to make a linked list of linked lists) but i have seen this in my dreams :o.

Here is what i have so far for the link list of lines aka rows, any idea, comments, remarks. Would be greatly appresiated, i started this like 2 hours ago so its pretty much nothing :P

#include <iostream>
#include <conio.h>
#include "definitions.h"
using namespace std;

class Letter{
public:
	char letter;
	Letter *next;
	Letter *prev;
};
class Wr{
protected:
	Letter *start,*end;
public:
	Wr(){start=NULL;};
	bool read();
	void add(char letter);
	void show();
	void lshow();
	void remove();
};
void Wr::lshow(){
	while(start!=NULL){
		cout<<start->letter;
		start=start->next;
	}
}
void Wr::remove(){
	Letter *temp;
	end->letter=' ';
	show();
	temp=end;
	end=temp->prev;
	delete temp;
}
void Wr::add(char letter){
	Letter *temp;
	temp=new Letter;
	temp->letter=letter;
	temp->next=NULL;
	temp->prev=NULL;
	if(start==NULL)
		start=end=temp;
	else{
		temp->prev=end;
		end->next=temp;
		end=temp;
	}
}
void Wr::show(){
	cout<<end->letter;
}
bool Wr::read(){
	Letter *cur;
	cur=end;
	char let=_getch();
	if(let!=0){
		add(let);
	}
	else
		if(let==0)
			let=_getch();
			if(let==KEY_RIGHT)
				if(cur->next!=NULL)
					cur=cur->next;
				else
					cur=cur;
			else
				if(let==KEY_LEFT)
					if(cur->prev!=NULL)
						cur=cur->prev;
					else
						cur=cur;
				else
					if(let==13)
						cout<<endl;
					else
						if(let==27)
						return false;
						else
							if(let==127)
								remove();
			return true;
}
int main(){
	Wr r;
	bool flag=true;
	while(flag==true){
		flag=r.read();
		r.show();
	}
	cout<<endl;
	r.lshow();
	cin.ignore();
	cin.get();
}

The definitions library is
#ifndef definitions_H_
#define definitions_H_
#define KEY_HOME		71
#define KEY_UP		  72
#define KEY_PGUP		73
#define KEY_LEFT		75
#define KEY_CENTER	  76
#define KEY_RIGHT	   77
#define KEY_END		 79
#define KEY_DOWN		80
#define KEY_PGDN		81
#define KEY_INSERT	  82
#define KEY_DELETE	  83
#define KEY_F1		  59
#define KEY_F2		  60
#define KEY_F3		  61
#define KEY_F4		  62
#define KEY_F5		  63
#define KEY_F6		  64
#define KEY_F7		  65
#define KEY_F8		  66
#define KEY_F9		  67
#endif


Ok well im working on console to make the correspondant tests, but my IDE (Visual Studio 2008) wont let me use clrscr() to clean the console, im guessing ill need something else, so i cant do tests on remove and the move left and right functions.

Either way any comments would be great!

Is This A Good Question/Topic? 0
  • +

Replies To: Linked List of Linked Lists

#2 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Linked List of Linked Lists

Posted 28 October 2009 - 11:57 PM

I'm not going to get into your code, just answer a question.

A generic linked list (one that isn't tied to a specific implementation) contains 2 parts: The linking part (which may have just forward, or forward and backward, etc.) and the data part. The data part can be anything, including another linked list.

Oh, and a quick thing about your code. The Wr::lshow method is going to destroy your list, and you keep changing the class variable start. You should make a method variable to use, and copy start to it at the begining.
Was This Post Helpful? 1
  • +
  • -

#3 jwwicks  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 24
  • View blog
  • Posts: 162
  • Joined: 31-July 08

Re: Linked List of Linked Lists

Posted 29 October 2009 - 12:39 AM

Hello,

View Postponcho4all, on 28 Oct, 2009 - 10:25 PM, said:

Well im aiming for a library that will work some how like a text editor.
So i thought about some kind of algorith that could go like this

Ok well im working on console to make the correspondant tests, but my IDE (Visual Studio 2008) wont let me use clrscr() to clean the console, im guessing ill need something else, so i cant do tests on remove and the move left and right functions.

Either way any comments would be great!


Something like this perhaps but I wouldn't recommend link lists used in this fashion. Finding and moving a word from one place in a text file to another is hideously complex.

Here's a great link for you...
http://www.cs.unm.ed...rs/sds/sds.html

typedef struct column
{
  struct column *next;
  struct column *prev;
  char *text;
} TEXT;

typedef struct line
{
  struct line *next;
  struct line *prev;
  struct column *text;
} LINE;

LINE *lineHead = NULL;
LINE *lineTail = NULL;




As for clrscr, if you're doing a console app then system("cls") or better yet.

#include <cstdlib>

void ClearScreen();

int main(){
   ClearScreen();
   return 0; 
}

void ClearScreen()
{//begin ClearScreen
#ifdef _WIN32
   system("cls");//windows
#else
  system("clear");//linux
#endif
}//end ClearScreen




Jw

This post has been edited by jwwicks: 29 October 2009 - 12:48 AM

Was This Post Helpful? 1
  • +
  • -

#4 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,405
  • Joined: 15-July 09

Re: Linked List of Linked Lists

Posted 30 October 2009 - 07:00 AM

jwwick Ima read that document and come back when i need help on it.

Momerath i noticed that the list was going to be destroyd, sorry i uploaded the wrong code, i was on a hurry yesterday. But thank you for noticing it. This was not a task that i wanted to do for myself, it was a homework for my class, but i got it finished before delivery time, i dont think i did it the way the document that jwwick uploaded but it worked.

Thanks for the help guyz xD
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1