473,763 Members | 2,714 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Linked List Problem(changin g insertion pointer)

Hi All,

Here I have a linked list each containing a char and is double linked.
Then I have a pointer to an item in that list which is the current
insertion point.

In this funtion, the user hits the right
and left keys to move this insertion point (cursor)

Here is the problem:

But its not stable, the insertion point seems to skip an item
back, here and there. It could be the API Im using for the keyboard
input but Im not sure.

Thanks in advance for any help.
John

//a char structure, making a text string list
struct Word{
char *c;
struct Word *next,*back;
};
typedef struct Word Word;

Word *wrd;// a linked list of Word structures
Word *insert;// pointer to the current insertion point of *wrd

wrd = (allocate a list of Word structures)
insert = (pointer to an insertion point in the text in the *wrd list)

DoKey(insert,RI GHT_KEY);

/* etc */

void DoKey(struct Word *char_insert, int key){
switch(key){
case RIGHT_KEY:
if(char_insert->next!=NULL)
char_insert=cha r_insert->next;/* this is not fool proof,
any help? */
break;
case LEFT_KEY:
if(char_insert->back!=NULL)
char_insert=cha r_insert->back;/* this is not fool proof,
any help? */
break;
}
}
--
comp.lang.c.mod erated - moderation address: cl**@plethora.n et
Nov 14 '05 #1
5 6060
[Followups set to comp.lang.c - nothing personal, Seebs!]

John N. wrote:
Hi All,

Here I have a linked list each containing a char and is double linked.
Your code says char *, not char.
Then I have a pointer to an item in that list which is the current
insertion point. <...> But its not stable, the insertion point seems to skip an item
back, here and there. It could be the API Im using for the keyboard
input but Im not sure.
It's your misunderstandin g of pointers, I'm afraid.
//a char structure, making a text string list
If you have a C99 compiler, // is legal comment syntax. Do you?
struct Word{
char *c;
struct Word *next,*back;
};
typedef struct Word Word;

Word *wrd;// a linked list of Word structures
Word *insert;// pointer to the current insertion point of *wrd

wrd = (allocate a list of Word structures)
This ain't gonna compile.
insert = (pointer to an insertion point in the text in the *wrd list)
Nor is this. Please remember, when posting code here, that by definition you
do not know where the problem is. So it's not a good idea to chop out
random chunks of it when asking for help.
DoKey(insert,RI GHT_KEY);

/* etc */

void DoKey(struct Word *char_insert, int key){
switch(key){
case RIGHT_KEY:
if(char_insert->next!=NULL)
char_insert=cha r_insert->next;/* this is not fool proof,
any help? */


C is pass-by-value.

void DoKey(struct Word **char_insert, int key)
{
switch(key)
{
case RIGHT_KEY:
if((*char_inser t)->next != NULL)
{
*char_insert = (*char_insert)->next;
}
break;

case LEFT_KEY:
if((*char_inser t)->back != NULL)
{
*char_insert = (*char_insert)->back;
}
break;
default:
/* unhandled key event */
break;
}
}

--
Richard Heathfield : bi****@eton.pow ernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
--
comp.lang.c.mod erated - moderation address: cl**@plethora.n et
Nov 14 '05 #2
John N. wrote:
Here I have a linked list each containing a char and is double linked.
Then I have a pointer to an item in that list which is the current
insertion point.

In this function, the user hits the right
and left keys to move this insertion point (cursor)

Here is the problem:

But its not stable, the insertion point seems to skip an item
back, here and there. It could be the API Im using for the keyboard
input but Im not sure.
No, it isn't likely to be that.
Thanks in advance for any help.
John

//a char structure, making a text string list
struct Word{
char *c;
struct Word *next,*back;
};
typedef struct Word Word;

Word *wrd;// a linked list of Word structures
Word *insert;// pointer to the current insertion point of *wrd

wrd = (allocate a list of Word structures)
And how is this data structure initialized? Zero pointers, or
pointers to itself (a circular list)? Or are you allocating and
initializing a whole bunch of Word structures, and putting them into a
valid linked list?
insert = (pointer to an insertion point in the text in the *wrd list)
So, presumably this is a contraction for 'insert = wrd;'? If not, you
need to show the initialization, since without the initialization, it
is hard to tell what the heck you might be doing.
DoKey(insert,RI GHT_KEY);
You've not shown where the character is coming from. I know you said
the keyboard, but which variable contains the value? Or is the
'RIGHT_KEY' -- presumably a macro since it is in upper case -- the
value purportedly to be inserted?
/* etc */

void DoKey(struct Word *char_insert, int key){
switch(key){
case RIGHT_KEY:
if(char_insert->next!=NULL)
char_insert=cha r_insert->next;/* this is not fool proof,
any help? */
break;
With a doubly-linked list, when you insert a new node, you allocate
the new node and populate the data portion (c) with the data for the
node. Then, assuming that the new node is inserted after the
insertion point, you ensure that the next pointer of the new node
points to the node that the insertion point points to as the next
node, that the back pointer of the next node points to the new node,
that the back pointer of the new node points to where the back pointer
of the insertion pointer points, and the next pointer of the back node
points to the new node. You might or might not also adjust the
insertion point. The sequence is similar but different if the new
node goes before the insertion point.

You've not shown very much of any of this material. You don't
allocate new nodes and you don't fix up the pre-existing nodes.
case LEFT_KEY:
if(char_insert->back!=NULL)
char_insert=cha r_insert->back;/* this is not fool proof,
any help? */
break;
}
}


Any more help? Draw a diagram! Draw a lot of diagrams! And make
sure all your pointers are initialized.

Finally, a member 'char *c' is misleading (but not, I hasten to
emphasize, wrong). In general, a variable c is a single character,
not a pointer to a character. There are various possible names that
would be happier choices - s or p are primary options, and there are a
myriad others. From the code you show, it is impossible to deduce
whether you store pointers to null-terminated strings or pointers to
single characters in it - you don't show it being used at all, in fact.

Given your problem description, you might be better off using c as a
simple char - though a doubly linked list of single characters is an
incredibly heavy-weight structure (typically occupying 12 bytes per
character).

Another reading of your problem might be that you are wondering why
the value of 'insert' in the calling code is not modified by the code
in DoKey -- and the answer is because you don't pass insert as a
pointer to a Word pointer. This only applies if you are already sure
you've built your doubly-linked list correctly, which is (as far as
anyone can see) debatable.

--
Jonathan Leffler #include <disclaimer.h >
Email: jl******@earthl ink.net, jl******@us.ibm .com
Guardian of DBD::Informix v2003.04 -- http://dbi.perl.org/
--
comp.lang.c.mod erated - moderation address: cl**@plethora.n et
Nov 14 '05 #3
"John N." <st*******@yaho o.com> wrote in

struct Word{
char *c;
struct Word *next,*back;
};
typedef struct Word Word;

Run the following function

void integrity_test( Word *head)
{
Word *prev = NULL:
printf("Start\n ");
while(head)
{
printf("%s\n", head->c);
prev = head;
head = head->next;
}
printf("End\n") ;
while(prev)
{
printf("%s\n", prev->c);
prev = prev->back;
}
}

This should expose problems with the linked list structure.
--
comp.lang.c.mod erated - moderation address: cl**@plethora.n et
Nov 14 '05 #4
On 28 Dec 2003 06:39:31 GMT, st*******@yahoo .com (John N.) wrote:
Hi All,

Here I have a linked list each containing a char and is double linked.
Then I have a pointer to an item in that list which is the current
insertion point.

In this funtion, the user hits the right
and left keys to move this insertion point (cursor)

Here is the problem:

But its not stable, the insertion point seems to skip an item
back, here and there. It could be the API Im using for the keyboard
input but Im not sure.

Thanks in advance for any help.
John

//a char structure, making a text string list
struct Word{
char *c;
struct Word *next,*back;
};
typedef struct Word Word;

Word *wrd;// a linked list of Word structures
Word *insert;// pointer to the current insertion point of *wrd

wrd = (allocate a list of Word structures)
insert = (pointer to an insertion point in the text in the *wrd list)

DoKey(insert,R IGHT_KEY);

/* etc */

void DoKey(struct Word *char_insert, int key){
switch(key){
case RIGHT_KEY:
if(char_insert->next!=NULL)
char_insert=cha r_insert->next;/* this is not fool proof,
any help? */
break;
case LEFT_KEY:
if(char_insert->back!=NULL)
char_insert=cha r_insert->back;/* this is not fool proof,
any help? */
break;
}
}


The real question is why do you think it worked at all. The
char_insert variable inside your function is not the same variable you
defined near the top of your code.

Your function performs all of its assignments to a local variable
(actually a parameter which, since C passes by value, is a copy of the
argument and not the argument itself). When the function exits, all
local variables (other than static) are destroyed. The result of your
function's assignments are never communicated back to the calling
function.
<<Remove the del for email>>
--
comp.lang.c.mod erated - moderation address: cl**@plethora.n et
Nov 14 '05 #5
> >//a char structure, making a text string list
struct Word{
char *c;
struct Word *next,*back;
};
typedef struct Word Word;

Word *wrd;// a linked list of Word structures
Word *insert;// pointer to the current insertion point of *wrd

wrd = (allocate a list of Word structures)
insert = (pointer to an insertion point in the text in the *wrd list)

DoKey(insert,R IGHT_KEY);

/* etc */

void DoKey(struct Word *char_insert, int key){
switch(key){
case RIGHT_KEY:
if(char_insert->next!=NULL)
char_insert=cha r_insert->next;/* this is not fool proof,
any help? */
break;
case LEFT_KEY:
if(char_insert->back!=NULL)
char_insert=cha r_insert->back;/* this is not fool proof,
any help? */
break;
}
}


The real question is why do you think it worked at all. The
char_insert variable inside your function is not the same variable you
defined near the top of your code.

Your function performs all of its assignments to a local variable
(actually a parameter which, since C passes by value, is a copy of the
argument and not the argument itself). When the function exits, all
local variables (other than static) are destroyed. The result of your
function's assignments are never communicated back to the calling
function.


Hi All, Thanks for the quick responses.

I posted a shorter example of my code. The problem turned out to be
my switch/case statement that handled key input (not posted here) and
the insertion pointer was being misshandled.
--
comp.lang.c.mod erated - moderation address: cl**@plethora.n et
Nov 14 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
1881
by: OMouse | last post by:
Hi, I just switched to using STL for my linked lists and obviously I need a way to insert. I have all the necessary includes (list & algorithm) and the other functions that I've used (erase & find) work. But when I try to insert the iterator that was found, gcc 3.3.4 tells me that I'm missing a few inputs. Anyway, here's the snippet of code. main.cpp ----------------- #include "usernode.h" #include <list>
6
1456
by: Randy Bush | last post by:
i am trying to insert into a singly linked list hold = self.next self.next = DaClass(value) self.next.next = hold but i suspect (from print statement insertions) that the result is not as i expect. as the concept and code should be very common, as i am too old for pride, i thought i would ask.
3
1781
by: Tom Timmermann | last post by:
In the process of building a link list, I noticed that successive calls to malloc() return a pointer address that keeps getting larger. Can I always count on this behavior and so do pointer comparisons like < or > ? Or is it possible for malloc to return a pointer address smaller than some previous call ? TomT
7
1442
by: Doug Stell | last post by:
I am having a problem with the corruption of a list. It occurs only the first time that I call a function and never happens on subsequent calls. Any suggestions would be appreciated. I call the function, passing in a list as the input data. The function must manipulate and operate on a copy of that list's data, without altering the list in the calling routine. def myFunc(listA): listB = listA
3
3063
by: Suyash Upadhyay | last post by:
Hello All, I am a beginner of C Programming, I am working on linked list now-a-days, i have successfully created a linked list and displayed, but when i tried to insert an element in mid of linked list, it doesnt take place. I think I have written every thing write. Please help me, #include "stdio.h" struct node { int value; struct node* link;
4
1482
by: eight02645999 | last post by:
hi i have a list (after reading from a file), say data = I wanted to insert a word after every 'a', and before every 'd'. so i use enumerate this list: for num,item in enumerate(data): if "a" in item: data.insert(num+1,"aword") if "d" in item:
0
977
by: Chuckk Hubbard | last post by:
I solved this problem. I still don't understand why, but it seems Python was appending a reference to the first element at the end of the list, rather than copying the data. I used something like: "self.regionlist.append(list(self.regionlist)" and now it works fine, i.e., altering the appended list member doesn't change the copied one. -Chuckk
1
1183
by: Chuckk Hubbard | last post by:
Hello. This program is clunky, I know; I'm not a programmer, but I need to use this program, so I'm writing it. The problem: I have a cursor following the mouse that shows frequency ratios of potential notes in relation to 1/1 (something like Middle C). At any time, the user may hit "t" to move 1/1 to wherever the cursor is. There is also the option to use many regions, so that some of the notes in the score, in region 0, for instance,...
8
5149
by: mike_solomon | last post by:
I have a button <input type="submit" name="Delete" value="Delete"> This code can not be changed I want to use Javascript to change the type I tried:
0
9563
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9386
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9998
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9938
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9822
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8822
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7366
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
3
3523
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.