Sponsored Content
Top Forums Programming unidirectional linked list pointer problem Post 302594188 by DreamWarrior on Monday 30th of January 2012 07:20:57 PM
Old 01-30-2012
Quote:
Originally Posted by bluetxxth
yes that is exactly what I want. I am going to try to do what you mentioned, althought I am not sure if I understood the part of the special case.

"....have to special case when you want the node to be inserted as the first position in the list. Maybe, in that case, you should pass the p variable as NULL and then check for NULL in dlist_insert and handle it correctly by linking it to the head of l...."

Could you elaborate a bit on that please? Thank you again!
Not really, because I have no idea what the dlist structure looks like. But, I'd guess you have a head pointer in dlist. So:
Code:
if (p == NULL)
{
  newPosition->next = l->head;
  l->head = newPosition;
}
else
{
  newPosition->next=p->next;
  p->next=newPosition;
}

I'm also curious why you're returning p and not newPosition; I'd figure you should probably return the node you just inserted...no?

At this point, I think this may be the extent to which I can assist. At this very basic level, it smacks of your being in an algorithms class. At which point, I'm wondering if this isn't homework. I don't want to be a jerk, but if you're going to succeed in this industry, you will eventually need to have these basics figured out. So, before I assist any more, I want to see a little more thought put in by yourself. You can get there....
 

10 More Discussions You Might Find Interesting

1. Programming

Reverse single linked list

Can any one help me in reversing the single linked list and at the same time i want to print the reversed links. (2 Replies)
Discussion started by: dhanamurthy
2 Replies

2. UNIX for Dummies Questions & Answers

List linked files

A perl script that displays the list of files which have multiple links..! ls -l shows number of links in a field. (0 Replies)
Discussion started by: aadi_uni
0 Replies

3. Programming

linked list node with pointer to struct

Suppose to have: struct Tstudent { string name, surname; int matriculation_num; }; struct Tnode { Tstudent* student; Tnodo* next; } L;I want to deference that "student" pointer. For example, I tried with: *(L->student).matriculation_numbut it not worked, as terminal... (4 Replies)
Discussion started by: Luke Bonham
4 Replies

4. Programming

shared memory with linked list??

is this possible, if so plz please share with me.. Correct English please, not Cyber-/Leetspeak (11 Replies)
Discussion started by: vijay_manpage
11 Replies

5. Programming

I need C++ Code for single linked list

I need C++ Code for single linked list With operations as 1)insert at any position 2)delete any 3)change the data of any position (2 Replies)
Discussion started by: girija
2 Replies

6. Programming

Help with linked list in C

i have this code typedef struct client_list { char *client_name; struct client_list * next; int client_socket_fd; } client; client *current, *head; head = NULL; char *h="test"; add_client(current, h, head, &client_socket_fd); ... (24 Replies)
Discussion started by: omega666
24 Replies

7. Programming

How to check if something exists in linked list in C?

i have a linked list set up like typedef struct client_list { char *client_name; int client_socket_fd; struct client_list *next; } client; client *client_list=NULL; before adding to the list i check if it already exists, only if it does not then i add if (client_list==NULL... (1 Reply)
Discussion started by: omega666
1 Replies

8. Programming

Help with linked list.

#include<stdio.h> #include<stdlib.h> struct LinkedList { int val; struct LinkedList *next; }node; /*Creating a structure variable*/ typedef struct LinkedList Node; Node *start = NULL; int create(int i) { Node *temp = NULL; if (start == NULL) ... (5 Replies)
Discussion started by: prinsh
5 Replies

9. Programming

How to delete the last node in a linked list.?

How to delete the last node in a single linked list given only the pointer to last node ? Head node will not be given. (5 Replies)
Discussion started by: VSSajjan
5 Replies

10. Programming

How to reverse a linked list by traversing only once.?

Program to reverse a linked list by traversing only once. (1 Reply)
Discussion started by: VSSajjan
1 Replies
TREE(3) 						   BSD Library Functions Manual 						   TREE(3)

NAME
SPLAY_PROTOTYPE, SPLAY_GENERATE, SPLAY_ENTRY, SPLAY_HEAD, SPLAY_INITIALIZER, SPLAY_ROOT, SPLAY_EMPTY, SPLAY_NEXT, SPLAY_MIN, SPLAY_MAX, SPLAY_FIND, SPLAY_LEFT, SPLAY_RIGHT, SPLAY_FOREACH, SPLAY_INIT, SPLAY_INSERT, SPLAY_REMOVE, RB_PROTOTYPE, RB_GENERATE, RB_ENTRY, RB_HEAD, RB_INITIALIZER, RB_ROOT, RB_EMPTY, RB_NEXT, RB_MIN, RB_MAX, RB_FIND, RB_LEFT, RB_RIGHT, RB_PARENT, RB_FOREACH, RB_INIT, RB_INSERT, RB_REMOVE -- implementations of splay and red-black trees SYNOPSIS
#include <sys/tree.h> SPLAY_PROTOTYPE(NAME, TYPE, FIELD, CMP); SPLAY_GENERATE(NAME, TYPE, FIELD, CMP); SPLAY_ENTRY(TYPE); SPLAY_HEAD(HEADNAME, TYPE); struct TYPE * SPLAY_INITIALIZER(SPLAY_HEAD *head); SPLAY_ROOT(SPLAY_HEAD *head); bool SPLAY_EMPTY(SPLAY_HEAD *head); struct TYPE * SPLAY_NEXT(NAME, SPLAY_HEAD *head, struct TYPE *elm); struct TYPE * SPLAY_MIN(NAME, SPLAY_HEAD *head); struct TYPE * SPLAY_MAX(NAME, SPLAY_HEAD *head); struct TYPE * SPLAY_FIND(NAME, SPLAY_HEAD *head, struct TYPE *elm); struct TYPE * SPLAY_LEFT(struct TYPE *elm, SPLAY_ENTRY NAME); struct TYPE * SPLAY_RIGHT(struct TYPE *elm, SPLAY_ENTRY NAME); SPLAY_FOREACH(VARNAME, NAME, SPLAY_HEAD *head); void SPLAY_INIT(SPLAY_HEAD *head); struct TYPE * SPLAY_INSERT(NAME, SPLAY_HEAD *head, struct TYPE *elm); struct TYPE * SPLAY_REMOVE(NAME, SPLAY_HEAD *head, struct TYPE *elm); RB_PROTOTYPE(NAME, TYPE, FIELD, CMP); RB_GENERATE(NAME, TYPE, FIELD, CMP); RB_ENTRY(TYPE); RB_HEAD(HEADNAME, TYPE); RB_INITIALIZER(RB_HEAD *head); struct TYPE * RB_ROOT(RB_HEAD *head); bool RB_EMPTY(RB_HEAD *head); struct TYPE * RB_NEXT(NAME, RB_HEAD *head, struct TYPE *elm); struct TYPE * RB_MIN(NAME, RB_HEAD *head); struct TYPE * RB_MAX(NAME, RB_HEAD *head); struct TYPE * RB_FIND(NAME, RB_HEAD *head, struct TYPE *elm); struct TYPE * RB_LEFT(struct TYPE *elm, RB_ENTRY NAME); struct TYPE * RB_RIGHT(struct TYPE *elm, RB_ENTRY NAME); struct TYPE * RB_PARENT(struct TYPE *elm, RB_ENTRY NAME); RB_FOREACH(VARNAME, NAME, RB_HEAD *head); void RB_INIT(RB_HEAD *head); struct TYPE * RB_INSERT(NAME, RB_HEAD *head, struct TYPE *elm); struct TYPE * RB_REMOVE(NAME, RB_HEAD *head, struct TYPE *elm); DESCRIPTION
This is a legacy interface; for new code, rbtree(3) is preferred. These macros define data structures for different types of trees: splay trees and red-black trees. In the macro definitions, TYPE is the name tag of a user defined structure that must contain a field of type SPLAY_ENTRY, or RB_ENTRY, named ENTRYNAME. The argument HEADNAME is the name tag of a user defined structure that must be declared using the macros SPLAY_HEAD() or RB_HEAD(). The argument NAME has to be a unique name prefix for every tree that is defined. The function prototypes are declared with either SPLAY_PROTOTYPE or RB_PROTOTYPE. The function bodies are generated with either SPLAY_GENERATE or RB_GENERATE. See the examples below for further explanation of how these macros are used. SPLAY TREES
A splay tree is a self-organizing data structure. Every operation on the tree causes a splay to happen. The splay moves the requested node to the root of the tree and partly rebalances it. This has the benefit that request locality causes faster lookups as the requested nodes move to the top of the tree. On the other hand, every lookup causes memory writes. The Balance Theorem bounds the total access time for m operations and n inserts on an initially empty tree as O((m + n)lg n). The amortized cost for a sequence of m accesses to a splay tree is O(lg n). A splay tree is headed by a structure defined by the SPLAY_HEAD() macro. A SPLAY_HEAD structure is declared as follows: SPLAY_HEAD(HEADNAME, TYPE) head; where HEADNAME is the name of the structure to be defined, and struct TYPE is the type of the elements to be inserted into the tree. The SPLAY_ENTRY() macro declares a structure that allows elements to be connected in the tree. In order to use the functions that manipulate the tree structure, their prototypes need to be declared with the SPLAY_PROTOTYPE() macro, where NAME is a unique identifier for this particular tree. The TYPE argument is the type of the structure that is being managed by the tree. The FIELD argument is the name of the element defined by SPLAY_ENTRY(). The function bodies are generated with the SPLAY_GENERATE() macro. It takes the same arguments as the SPLAY_PROTOTYPE() macro, but should be used only once. Finally, the CMP argument is the name of a function used to compare trees noded with each other. The function takes two arguments of type struct TYPE *. If the first argument is smaller than the second, the function returns a value smaller than zero. If they are equal, the function returns zero. Otherwise, it should return a value greater than zero. The compare function defines the order of the tree elements. The SPLAY_INIT() macro initializes the tree referenced by head. The splay tree can also be initialized statically by using the SPLAY_INITIALIZER() macro like this: SPLAY_HEAD(HEADNAME, TYPE) head = SPLAY_INITIALIZER(&head); The SPLAY_INSERT() macro inserts the new element elm into the tree. The SPLAY_REMOVE() macro removes the element elm from the tree pointed by head. The SPLAY_FIND() macro can be used to find a particular element in the tree. struct TYPE find, *res; find.key = 30; res = SPLAY_FIND(NAME, head, &find); The SPLAY_ROOT(), SPLAY_MIN(), SPLAY_MAX(), and SPLAY_NEXT() macros can be used to traverse the tree: for (np = SPLAY_MIN(NAME, &head); np != NULL; np = SPLAY_NEXT(NAME, &head, np)) Or, for simplicity, one can use the SPLAY_FOREACH() macro: SPLAY_FOREACH(np, NAME, head) The SPLAY_EMPTY() macro should be used to check whether a splay tree is empty. RED-BLACK TREES A red-black tree is a binary search tree with the node color as an extra attribute. It fulfills a set of conditions: 1. every search path from the root to a leaf consists of the same number of black nodes, 2. each red node (except for the root) has a black parent, 3. each leaf node is black. Every operation on a red-black tree is bounded as O(lg n). The maximum height of a red-black tree is 2lg (n+1). A red-black tree is headed by a structure defined by the RB_HEAD() macro. A RB_HEAD structure is declared as follows: RB_HEAD(HEADNAME, TYPE) head; where HEADNAME is the name of the structure to be defined, and struct TYPE is the type of the elements to be inserted into the tree. The RB_ENTRY() macro declares a structure that allows elements to be connected in the tree. In order to use the functions that manipulate the tree structure, their prototypes need to be declared with the RB_PROTOTYPE() macro, where NAME is a unique identifier for this particular tree. The TYPE argument is the type of the structure that is being managed by the tree. The FIELD argument is the name of the element defined by RB_ENTRY(). The function bodies are generated with the RB_GENERATE() macro. It takes the same arguments as the RB_PROTOTYPE() macro, but should be used only once. Finally, the CMP argument is the name of a function used to compare trees noded with each other. The function takes two arguments of type struct TYPE *. If the first argument is smaller than the second, the function returns a value smaller than zero. If they are equal, the function returns zero. Otherwise, it should return a value greater than zero. The compare function defines the order of the tree elements. The RB_INIT() macro initializes the tree referenced by head. The redblack tree can also be initialized statically by using the RB_INITIALIZER() macro like this: RB_HEAD(HEADNAME, TYPE) head = RB_INITIALIZER(&head); The RB_INSERT() macro inserts the new element elm into the tree. The RB_REMOVE() macro removes the element elm from the tree pointed to by head. The element must be present in that tree. The RB_FIND() macro can be used to find a particular element in the tree. struct TYPE find, *res; find.key = 30; res = RB_FIND(NAME, head, &find); The RB_ROOT(), RB_MIN(), RB_MAX(), and RB_NEXT() macros can be used to traverse the tree: for (np = RB_MIN(NAME, &head); np != NULL; np = RB_NEXT(NAME, &head, np)) Or, for simplicity, one can use the RB_FOREACH() macro: RB_FOREACH(np, NAME, head) The RB_EMPTY() macro should be used to check whether a red-black tree is empty. NOTES
Some of these macros or functions perform no error checking, and invalid usage leads to undefined behaviour. In the case of macros or func- tions that expect their arguments to be elements that are present in the tree, passing an element that is not present in the tree is invalid. Trying to free a tree in the following way is a common error: SPLAY_FOREACH(var, NAME, head) { SPLAY_REMOVE(NAME, head, var); free(var); } free(head); Since var is free'd, the FOREACH() macro refers to a pointer that may have been reallocated already. Proper code needs a second variable. for (var = SPLAY_MIN(NAME, head); var != NULL; var = nxt) { nxt = SPLAY_NEXT(NAME, head, var); SPLAY_REMOVE(NAME, head, var); free(var); } Both RB_INSERT() and SPLAY_INSERT() return NULL if the element was inserted in the tree successfully, otherwise they return a pointer to the element with the colliding key. Accordingly, RB_REMOVE() and SPLAY_REMOVE() return the pointer to the removed element, otherwise they return NULL to indicate an error. AUTHORS
The author of the tree macros is Niels Provos. BSD
May 5, 2010 BSD
All times are GMT -4. The time now is 07:26 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy