Recitation 8 - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

Recitation 8

Description:

... keyboard and creates a linked list G10 with nodes containing the ... Write a function that returns the value stored in the last node of linked list phead. ... – PowerPoint PPT presentation

Number of Views:21
Avg rating:3.0/5.0
Slides: 12
Provided by: csU73
Category:

less

Transcript and Presenter's Notes

Title: Recitation 8


1
Recitation 8
  • COP 3502
  • Christopher Elwell
  • Spring 2005

2
A Few Reminders
  • Office hours MTF 2-4 PM, CC1-202
  • Also, W 2-3 PM, Sat. 11-12 Noon
  • Test grades are posted
  • Programming Assignment 2 submitted (should be
    graded soon)

3
Linked Lists
  • Assume we have this structure
  • Struct node
  • int data
  • Struct node next
  • Struct node phead points to the head of the
    list.

4
Linked Lists
  • What is the output of this?
  • struct node pfirst
  • pfirstphead
  • printf(First List\n)
  • while (pfirst!NULL)
  • pfirst-gtdatapfirst-gtdata10
  • printf(d, pfirst-gtdata)
  • pfirstpfirst-gtnext
  • Contents of list (initially) 27,30,9,80,70

5
Linked Lists
  • Output should be
  • First List
  • 37 40 19 90 80

6
Problem 2
  • Write a program which prompts the user to enter
    10 integers from the keyboard and creates a
    linked list G10 with nodes containing the
    integers in the same order.

7
Problem 2
  • int i, input
  • struct node g10, current
  • g10(struct node ) malloc(sizeof(struct node))
  • currentg10
  • for (i1 ilt10 i)
  • scanf(d, current-gtdata)
  • if (i!10)
  • curr-gtnext(struct node ) malloc(sizeof(struct
    node))
  • currcurr-gtnext

8
Problem 3
  • Write a routine which returns 1 if any node of
    linked list LL contains the value 650 and
    otherwise returns 0.

9
Problem 3
  • int find650(struct node head)
  • struct node currhead
  • int found0
  • while ((curr!NULL)(found0))
  • if (curr-gtdata650) found1
  • currcurr-gtnext
  • return found

10
Problem 4
  • Write a function that returns the value stored in
    the last node of linked list phead.

11
Problem 4
  • int getlast(struct node phead)
  • struct node currphead
  • while (curr!NULL)
  • if (curr-gtnextNULL) return curr-gtdata
  • currcurr-gtnext
  • return 0
Write a Comment
User Comments (0)
About PowerShow.com