0% found this document useful (0 votes)
42 views

Ch5 List

This document discusses lists and their implementation using linked nodes. It defines a list as a collection of elements with a linear relationship between them. It then describes common list operations like creation, insertion, deletion, retrieval and printing of elements. It also provides code implementations for creating and manipulating linked lists in C++.

Uploaded by

api-3815042
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

Ch5 List

This document discusses lists and their implementation using linked nodes. It defines a list as a collection of elements with a linear relationship between them. It then describes common list operations like creation, insertion, deletion, retrieval and printing of elements. It also provides code implementations for creating and manipulating linked lists in C++.

Uploaded by

api-3815042
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 10

What is a List?

● A list is a homogeneous collection of


elements, with a linear relationship
between elements.

● That is, each list element (except the


first) has a unique predecessor, and
each element (except the last) has a
unique successor.

1
ADT Unsorted List Operations
Transformers
■ Create List change state
■ Insert Item
■ Delete Item

Observers
■ FullList
■ Empty List
■ Print List observe state

■ Retrieve Item

2
List Declaration
#include<stdlib.h>
#include<iostream.h>

typedef int ElementType;


struct NodeType{
ElementType info;
NodeType *link;
};
typedef NodeType *ListType;

3
void CreateList(ListType *l){
*l=NULL;
}

int EmptyList(ListType l){


return (l==NULL);
}

int FullList(ListType l){


return (0);
}
4
Insert an item to the list at the Rear
void InsertRear(ListType *l,ElementType x){
if (*l==NULL){
*l=new(NodeType);
(*l)->link=NULL;
(*l)->info=x;
}else{
NodeType *p=*l;
while(p->link!=NULL) p=p->link;
p->link=new(NodeType);
p->link->link=NULL;
p->link->info=x;
}
} 5
Retrieving an Item from a list:

6
Retrieving an Item from a list:

NodeType* Find(ListType l,ElementType x)


{
while((l!=NULL)&&(l->info!=x)) l=l->link;
return(l);
}

7
Deleting an Item from a list:

void Delete(ListType *l,ElementType x){


NodeType *prev=NULL,*node=*l;
while((node!=NULL)&&(node->info!=x)){
prev=node;
node=node->link;
}
if(node!=NULL){
if(prev==NULL) *l=node->link;
else prev->link=node->link;
delete(node);
}
}
8
Printing a list

void Print(ListType l){


while(l!=NULL){
cout<<l->info<<" ";
l=l->link;
}
}

9
Summary

Array-based or linked representation for

● Stacks
● Queues
● Lists

10

You might also like