GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 09-Nov-2004, 03:47
sosy2001 sosy2001 is offline
New Member
 
Join Date: Oct 2004
Posts: 12
sosy2001 is on a distinguished road

Ineed some help with thos code - Linear Linked List


CPP / C++ / C Code:
#include <stdio.h>
typedef char DATA;
struct linked_list {
DATA   d;
struct linked_list   *next;
};
typedef struct linked_list ELEMENT;
typedef ELEMENT *   LINK;

I should modify this header file list.h above and replace the typedef line with
->

CPP / C++ / C Code:
struct data{
char name[10];
int age;
int weight;
}
typedef struct data DATA;

I have to write a function creat_list() to transform an array of type DATA to a linear linked list. Then write another funtion that counts the number of people abpove both a given aga and weight.

I have read the chapter on linear linked list, but I still dont know how to start this problem. Can some help me with this?
Last edited by LuciWiz : 09-Nov-2004 at 04:29. Reason: Please insert your c code between [c] & [/c] tags
  #2  
Old 10-Nov-2004, 12:57
drewdaman drewdaman is offline
New Member
 
Join Date: Sep 2004
Posts: 14
drewdaman is on a distinguished road
can't really give you code.. because i do't have any.. but i have done this in java...

have a method called insert or something in your linked list. with this, just add an element to your link list.. ie add a node.

your contructor for the linked list should create an empty list. in your method to transfer data from the array to the list, just insert till the end of the array.. i'm not sure if i understand your problem correctly.. but if i do, it shouldn't be too hard to do!

assignment from school?

ps. why are you using structs? i recommend using classes for this.. its much easier and neater... i think anyways.. i guess i have used classes more...
  #3  
Old 10-Nov-2004, 17:30
sosy2001 sosy2001 is offline
New Member
 
Join Date: Oct 2004
Posts: 12
sosy2001 is on a distinguished road

Thank You


Whats The Difference Between Java And C? Yes This Is A Class Assignment,and I Dont Really Understand This Topic Of Linked Lists. Thats Why I Am Here Seeking Help.



Sosy2001
  #4  
Old 10-Nov-2004, 18:46
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 313
nkhambal is a jewel in the roughnkhambal is a jewel in the rough
Hi,

Pls see post

http://www.gidforums.com/t-3759.html

It will give you some idea about building a linked list.

Thanks,
  #5  
Old 10-Nov-2004, 21:49
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by sosy2001
Whats The Difference Between Java And C? Yes This Is A Class Assignment,and I Dont Really Understand This Topic Of Linked Lists. Thats Why I Am Here Seeking Help.



Sosy2001
Then you need to ask specific questions about linked lists so you can understand. What is it you don't get? What are you having trouble with?
__________________

Age is unimportant -- except in cheese
  #6  
Old 11-Nov-2004, 10:17
drewdaman drewdaman is offline
New Member
 
Join Date: Sep 2004
Posts: 14
drewdaman is on a distinguished road
i know i know.. was a student myself just a couple of months ago!
i tried looking for my old java implementation so you could check it out.. but it seems to have got deleted. but, i can point you to a first year computer science website where you might find something useful.

http://www.site.uottawa.ca/~turcotte/teaching/csi-1101/

check out the solutions to assignment 8. that course used to be in java when i took it. but it might have changed.
  #7  
Old 11-Nov-2004, 10:23
drewdaman drewdaman is offline
New Member
 
Join Date: Sep 2004
Posts: 14
drewdaman is on a distinguished road
actually, i was looking and i found this code in java for a linked list.. might help you out.

JAVA Code:
import java.util.NoSuchElementException;

// doubly-linked list
// where Node is a nested class, which now also has a backward reference,
// this implementation has a head and a tail.

public class LinkedList extends Object {

    // nested-class

    private static class Node {

	private Object value;

	private Node previous; // <-- NEW
	private Node next;

	Node (Object value, Node previous, Node next) {  // <-- CHANGED
	    this.value = value;
	    this.previous = previous;
	    this.next = next;
	}
    }

    // Instance variables

    private Node head;
    private Node tail;

    // Representation of the empty list.

    public LinkedList () {
        head = null;
	tail = null;
    }

    // IDEM

    public int size() {
	Node p = head;
	int count = 0;
	while (p!=null) {
	    p = p.next;
	    count++;
	}
	return count;
    }

    public void addFirst(Object o) {

	if (o == null)
	    throw new IllegalArgumentException("null");

	head = new Node(o, null, head); // has no previous!

	if (tail == null)
	    tail = head;
	else
	    head.next.previous = head; // all the elements must be connected
	                               // to their predecessor (memory diagram?)
    }

    public void addLast(Object o) {

	if (o == null)
	    throw new IllegalArgumentException("null");

	if (head == null) {

	    head = new Node(o, null, null);
	    tail = head;

	} else {

	    tail.next = new Node(o, tail, null); // draw the memory diagram for this
	    tail = tail.next;

	}
    }

    // IDEM

    public Object get(int pos) {

	if (pos < 0) 
	    throw new IndexOutOfBoundsException(Integer.toString(pos));

	Node p = head;
	
	for (int i=0; i<pos; i++)
	    if (p == null)
		throw new IndexOutOfBoundsException(Integer.toString(pos));
	    else
		p = p.next;

	return p.value;
    }

    public void add(int pos, Object o) {

	if (o == null)
	    throw new IllegalArgumentException("null");

	if (pos < 0) 
	    throw new IndexOutOfBoundsException(Integer.toString(pos));

	if (pos == 0) {
	    head = new Node (o, null, head);
	    if (tail == null)
		tail = head;
	    else
		head.next.previous = head; // memory diagram
	} else {

	    Node p = head;
	    
	    for (int i = 0; i < (pos-1); i++)
		if (p == null)
		    throw new IndexOutOfBoundsException(Integer.toString(pos));
		else
		    p = p.next;
	    
	    p.next = new Node (o, p, p.next);

	    if (p == tail)
		tail = p.next;
            else
		p.next.next.previous = p.next;
	}
    }

    public boolean remove(Object o) {

	if (head == null)
	    return false;
	
	// having previous pointers opens up avenues for
	// other strategies

	Node p = head;

	while (p != null && ! p.value.equals(o))
		p = p.next;

	if (p == null) // o was not found
	    return false;

	if (p == head) { // o occurs at position 0

	    head = head.next;

	    if (head == null)
		tail = null; // this was the last element
	    else
		head.previous = null;

	    p.value = null;
	    p.next = null;

	} else {

	    Node nodeToDelete = p;

	    p = p.previous; // moving backwards

	    p.next = nodeToDelete.next;

	    if (nodeToDelete == tail)
		tail = p;
	    else
		p.next.previous = p;

	    nodeToDelete.value = null;
	    nodeToDelete.next = null;

	}

	return true;
    }

    public Object removeFirst() {

	if (head == null)
	    throw new NoSuchElementException();
	
	Node toDelete = head;
  	Object savedValue = head.value;

      	head = head.next;

	if (head == null)
	    tail = null;
	else
	    head.previous = null; // <-- !!!
    
	toDelete.next = null;
	toDelete.value = null;

  	return savedValue;
    }

    public Object removeLast() {

	if (head == null)
	    throw new NoSuchElementException();
	
	// Ok, we now have at least one element

        Node toDelete;

        if (head.next == null) {

            toDelete = head;
            head = null;
            tail = null;

        } else {

	    // no traversal involved

            toDelete = tail;

            tail = tail.previous;
	    tail.next = null;
        }

        Object savedValue;
	savedValue = toDelete.value;

	toDelete.value = null;
        toDelete.next = null;

        return savedValue;
    }
}




You will find that on the same website if you click on lecture notes, then source code, then 03/
 
 

Recent GIDBlogWelcome to Baghdad by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Merge sort on a linked list Temujin_12 C++ Forum 1 06-Mar-2008 20:33
bus error in linked list picknicker187 C Programming Language 2 08-Oct-2004 10:44
Insert problem in linked list with two function code Kay Chan C++ Forum 1 03-Sep-2004 09:52
help on linked lists any1????? nick4 C Programming Language 1 17-May-2004 09:32
[include] list1.h -- Linked list class dsmith C Programming Language 2 04-May-2004 09:42

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 17:25.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.