You are on page 1of 27

Unit 4

Topics
Pointers Pointers and Arrays Pointers and Strings Pointers and Structures Structure pointer Pointers and Data Structures
Linked List Stack Queue

Arrays and Pointers


a[i] is equivalent to *(a + i) p[i] is equivalent to *(p + i)
Pointer arithmetic ~ array indexing

An array name is an address!

Declaration int *p; int a[N];

p = a is equivalent to p = &a[0] p = a + 1 is equivalent to p = &a[1]

structure pointer
#include <stdio.h> struct card { char *face; char *suit; }; int main() { struct card aCard; struct card *cardPtr; aCard.face = "Ace"; aCard.suit = "Spades"; cardPtr = &aCard; printf( "%s%s%s\n%s%s%s\n%s%s%s\n", aCard.face, " of ", aCard.suit, cardPtr->face, " of ", cardPtr->suit, ( *cardPtr ).face, " of ", ( *cardPtr ).suit ); return 0; }

Pointers and Data Structures


Linked Lists
Each element in a linked list can be placed anywhere in memory. The elements are linked with each other using an explicit link field. To access the element you can use the starting pointer of the list. STACKS A stack is a list of elements with insertions and deletions at one end. A stack data structure has the LIFO (last in first out) property. QUEUES A queue is a list with insertions at one end and deletions at the other end. A queue exhibits the FIFO (first in first out) property.

Linked Lists
A Head B C

A linked list is a series of connected nodes Each node contains at least


A piece of data (any type) Pointer to the next node in the list
node

Head: pointer to the first node The last node points to NULL

A
data pointer

Examples of the Nodes of a Linked List


A node in a linked list is a structure that has at least two fields. One of the fields is a data field; the other is a pointer that contains the address of the next node in the sequence.
number link

A node with one data field: struct node{ int number; struct node * link;

The Nodes of a Linked List Examples


A node with three data fields:
struct student{ char name[20]; int id; double grdPts; struct student *next_student;

Name

id

grdPts

next_student

The Nodes of a Linked List Examples


A structure in a node:
struct person{ char name[20]; char address[30]; char phone[10]; }; struct person_node{ data next struct person data; struct person_node };

name

address

phone

next

*next;

data

Basic Operations on a Linked List


1. Add a node. 2. Delete a node. 3. Search for a node. 4. Traverse (walk) the list. counting operations or operations.

Useful for aggregate

Adding Nodes to a Linked List


Adding a Node There are four steps to add a node to a linked list: Allocate memory for the new node. Determine the insertion point (you need to know only the new nodes predecessor (pPre) Point the new node to its successor. Point the predecessor to the new node.

Pointer to the predecessor (pPre) can be in one of two states: it can contain the address of a node (i.e. you are adding somewhere after the first node in the middle or at the end) it can be NULL (i.e. you are adding either to an empty list or at the beginning of the list)

Before:
pNew pHead

Adding Nodes to an Empty Linked List


39

Code:

pNew -> next = pHead; // set link pHead = pNew;// point list to first

to NULL node

pPre

pNew pHead After: pPre

39

The Queue Operations


A queue is like a line of people waiting for a bank teller. The queue has a front and a rear.
$ $

Front Rear

The Queue Operations


New people must enter the queue at the rear. The C++ queue class calls this a push, although it is usually called an enqueue operation.
$ $

Front Rear

The Queue Operations


When an item is taken from the queue, it always comes from the front. The C++ queue calls this a pop, although it is usually called a dequeue operation.
$ $

Front Rear

You might also like