Amazon.com Widgets Exercise chapter 11 problem 3.
Welcome, Guest. Please login or register.
Did you miss your activation email?
July 31, 2014, 02:38:16 PM
Home Help Search chat Login Register 
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+ Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+ Programming in C, 3rd ed. (Stephen Kochan)
| |-+ Questions and Help
| | |-+ Exercise chapter 11 problem 3.
Pages: [1] Go Down
Print
Author Topic: Exercise chapter 11 problem 3. (Read 1071 times)
A31Chris
Newbie
*
Posts: 20


Email




on: May 28, 2012, 11:03:23 AM

I may be misunderstanding the assignment.

For context I'll post my solution for exercise 11 problem two here. It works but I don't think it is right looking at the answer for 11_3.

Is the listHead structure only going to have its .next member initialized?

Also the instructions for 11_2 are:
Quote
Write a function called insertEntry to insert a new entry into a linked list. Have the procedure take as arguments a pointer to the list entry to be inserted(of type struct entry as defined in this chapter), and a pointer to an element in the list after which the new entry is to be inserted.

The stipulation in exercise 3 is that the previous exercises function limitations was it 'only inserts an element after ane existing element in the list'. Maybe thats whats confusing me. In my solution I would just put insertEntry(newEntry,laterEntry) with 'laterEntry' pointing to the first element in the list. I would have to reset the pointer to point to the new head of the list on either exercise so it doesn't seem to me how what I did is hardwired to insert an entry only AFTER an existing entry in the list. Leading me to believe my solution and/or understanding of exercise 11_2 is wrong.





Code: (Objective-C)
#include <stdio.h>

struct entry
{
int value;
struct entry *next;
};

void insertEntry(struct entry *insertedEntry,struct entry *laterEntry)
{
insertedEntry->next=laterEntry;
}

int main(void)
{


struct entry n1,n2,n3,n2_3;
struct entry *list_pointer=&n1;

n1.value=100;
n1.next=&n2;

n2.value=200;
n2.next=&n3;

n3.value=300;
n3.next=(struct entry *)0; //Mark list end with null pointer

n2_3.value=250;

printf("Before linked list insertion:\n");
while (list_pointer !=(struct entry *)0)
{
printf("%i\n",list_pointer->value);
list_pointer=list_pointer->next;
}

list_pointer=&n1; // Point list_pointer to front of list again
n2.next=&n2_3;

printf("After linked list insertion:\n");

insertEntry(&n2_3,&n3);

while (list_pointer !=(struct entry *)0)
{
printf("%i\n",list_pointer->value);
list_pointer=list_pointer->next;
}

getchar();

return 0;

}
Last Edit: May 28, 2012, 11:17:50 AM by A31Chris Logged
A31Chris
Newbie
*
Posts: 20


Email




Reply #1 on: June 02, 2012, 10:11:36 AM

Alright I see my misunderstanding:

[Have the procedure take as arguments a pointer to the list entry to be inserted and a pointer to an element in the list after which the new entry is to be inserted[/quote]

I thought it meant a pointer to an element that will appear after the new insertion in the linked list. Not the other way around. Ok thanks for the help guys.
Logged
Pages: [1] Go Up
Print
Jump to:



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.