INFSCI 0015 Data Structures Lecture 21: Linked Lists Implementation - PowerPoint PPT Presentation

1 / 28
About This Presentation
Title:

INFSCI 0015 Data Structures Lecture 21: Linked Lists Implementation

Description:

Traversing list as ADT. We need an extra pointer to store current position ... traverse */ emptyList and listCount. int emptyList (LIST *pList) return (pList ... – PowerPoint PPT presentation

Number of Views:50
Avg rating:3.0/5.0
Slides: 29
Provided by: peterbru
Category:

less

Transcript and Presenter's Notes

Title: INFSCI 0015 Data Structures Lecture 21: Linked Lists Implementation


1
INFSCI 0015 - Data StructuresLecture 21 Linked
Lists Implementation
  • Peter Brusilovsky
  • http//www2.sis.pitt.edu/peterb/0015-011/

2
Data Structures
  • typedef struct
  • int data
  • struct node link
  • NODE
  • typedef struct
  • int count
  • NODE pos / for traverse /
  • NODE head
  • NODE rear
  • LIST

3
ADT Interface
  • LIST createList ()
  • LIST destroyList (LIST list)
  • int addNode (LIST pList, int newdata)
  • int removeNode(LIST pList, int d_data)
  • static int retrieveNode(LIST pList, int key)
  • int traverse (LIST pList, int from, int
    t_data)
  • int listCount (LIST pList)
  • int emptyList (LIST pList)
  • int fullList (LIST pList)

4
createList
  • LIST createList()
  • LIST list
  • list (LIST ) malloc (sizeof (LIST))
  • if (list)
  • list-gthead NULL
  • list-gtpos NULL
  • list-gtrear NULL
  • list-gtcount 0
  • / if /
  • return list
  • / createList /

5
(No Transcript)
6
Service function _search
  • int _search (LIST pList, NODE pPre, NODE
    pLoc, int s_data)
  • 1 if greater than last node -- fail
  • 2 move along the list while s_data gt
    (pLoc)-gtdata
  • 3 if s_data (pLoc)-gtdata -- found
  • 4 otherwise s_data lt (pLoc)-gtdata -- fail

7
Service function _search (1)
  • int _search (LIST pList, NODE pPre, NODE
    pLoc, int s_data)
  • pPre NULL
  • pLoc pList-gthead
  • if (pList-gtcount 0)
  • return 0
  • / Test for argument gt last node in list /
  • if ( s_data gt pList-gtrear-gtdata)
  • pPre pList-gtrear
  • pLoc NULL
  • return 0
  • / if /

8
Service function _search (2)
  • while ( s_data gt (pLoc)-gtdata )
  • / Have not found search argument location /
  • pPre pLoc
  • pLoc (pLoc)-gtlink
  • / while /
  • if (s_data (pLoc)-gtdata)
  • / argument found--success /
  • return 1
  • else / i.e., s_data lt (pLoc)-gtdata /
  • return 0
  • / _search /

9
RetrieveNode with _search
  • static int retrieveNode(LIST pList, int key)
  • / Local Declarations /
  • NODE pPre
  • NODE pLoc
  • / Statements /
  • return _search (pList, pPre, pLoc, key)
  • / retrieveNode /

10
Service function _insert (1)
  • static int _insert (LIST pList, NODE pPre, int
    newdata)
  • NODE pNew
  • if (!(pNew (NODE ) malloc(sizeof(NODE))))
  • return 0
  • pNew-gtdata newdata
  • pNew-gtlink NULL
  • if (pPre NULL)
  • / Adding before first node or to empty list.
    /
  • pNew-gtlink pList-gthead
  • pList-gthead pNew
  • if (pList-gtcount 0)
  • / Adding to empty list. Set rear /
  • pList-gtrear pNew

11
Insert at the beginning of the list
12
Service function _insert (2)
  • else
  • / Adding in middle or at end /
  • pNew-gtlink pPre-gtlink
  • pPre-gtlink pNew
  • / Now check for add at end of list /
  • if (pNew-gtlink NULL)
  • pList-gtrear pNew
  • / if else /
  • (pList-gtcount)
  • return 1
  • / _insert /

13
Insert in the the middle of the list
14
addNode
  • int addNode (LIST pList, int newdata )
  • int found
  • int success
  • NODE pPre
  • NODE pLoc
  • found _search (pList, pPre, pLoc, newdata)
  • if (found 1) / Duplicate /
  • return (1)
  • success _insert (pList, pPre, newdata)
  • if (!success)
  • / Overflow /
  • return (-1)
  • return (0)
  • / addNode /

15
Service function _delete
  • void _delete (LIST pList, NODE pPre, NODE
    pLoc)
  • if (pPre NULL)
  • / Deleting first node /
  • pList-gthead pLoc-gtlink
  • else
  • / Deleting any other node /
  • pPre-gtlink pLoc-gtlink
  • / Test for deleting last node /
  • if (pLoc-gtlink NULL)
  • pList-gtrear pPre
  • (pList-gtcount)--
  • free (pLoc)
  • return
  • / _delete /

16
Delete first
17
Delete General case
18
destroyList
  • LIST destroyList (LIST pList)
  • NODE deletePtr
  • if (pList)
  • while (pList-gtcount gt 0)
  • / Delete node /
  • deletePtr pList-gthead
  • pList-gthead pList-gthead-gtlink
  • pList-gtcount--
  • free (deletePtr)
  • free (pList)
  • / if /
  • return NULL
  • / destroyList /

19
removeNode
  • int removeNode(LIST pList, int d_data)
  • / Local Declarations /
  • int found
  • NODE pPre
  • NODE pLoc
  • / Statements /
  • found _search (pList, pPre, pLoc, d_data)
  • if (found)
  • _delete (pList, pPre, pLoc)
  • return found
  • / removeNode /

20
Traversing list as ADT
  • We need an extra pointer to store current
    position
  • get_first will set pos head
  • get_next will set pos pos-gtlink

21
traverse (1)
  • int traverse (LIST pList, int fromWhere, int
    t_data)
  • int success
  • if (fromWhere 0)
  • /Start from first node /
  • if (pList-gtcount 0)
  • success 0
  • else
  • pList-gtpos pList-gthead
  • t_data pList-gtpos-gtdata
  • success 1
  • / if else /

22
traverse (2)
  • else
  • / Start from current position /
  • if (pList-gtpos-gtlink NULL)
  • success 0
  • else
  • pList-gtpos pList-gtpos-gtlink
  • t_data pList-gtpos-gtdata
  • success 1
  • / if else /
  • / if fromwhere else /
  • return success
  • / traverse /

23
emptyList and listCount
  • int emptyList (LIST pList)
  • return (pList-gtcount 0)
  • / emptyList /
  • int listCount(LIST pList)
  • return pList-gtcount
  • / listCount /

24
fullList
  • int fullList (LIST pList)
  • NODE temp
  • if ((temp (NODE )malloc (sizeof (NODE))))
  • free (temp)
  • return 0
  • / Dynamic memory full /
  • return 1
  • / fullList /

25
Driver function (1)
  • void main()
  • LIST mylist
  • int i, res
  • int value
  • mylist createList()
  • while(1)
  • printf("\nList manipulation\n\n")
  • printf("1. Add\t\t")
  • printf("2. Remove\n")
  • printf("3. Find\t\t")
  • printf("4. Empty\n")
  • printf("5. Count\t")
  • printf("6. Print\n")
  • printf("7. End\n\n")

26
Driver function (2)
  • if(i 1)
  • printf("Value? ")
  • scanf("d",value)
  • if((res addNode(mylist, value)) 0)
  • printf("Value d added in\n", value)
  • else if(res -1)
  • printf("List is Full\n")
  • else printf("Duplicate key d\n", value)
  • else if (i 2)
  • printf("Value? ")
  • scanf("d",value)
  • if(removeNode(mylist, value))
  • printf("Node d removed\n", value)
  • else printf("No node with key d\n", value)

27
Driver function (3)
  • else if (i 3)
  • printf("Value? ")
  • scanf("d",value)
  • if (retrieveNode(mylist, value))
  • printf("Node d found\n", value)
  • else
  • printf("Node d not found\n", value)
  • else if (i 4)
  • if (emptyList(mylist))
  • printf("List is empty\n")
  • else
  • printf("List is not empty\n")

28
Driver function (4)
  • else if (i 5)
  • printf("d elements in the list\n",
    listCount(mylist))
  • else if (i 6)
  • printf("( ")
  • if (traverse(mylist, 0, value))
  • do
  • printf("d ", value)
  • while (traverse(mylist, 1, value))
  • printf(")\n")
  • else
  • destroyList(mylist)
  • return
Write a Comment
User Comments (0)
About PowerShow.com