Professional Documents
Culture Documents
List Processing
List Processing
1
Self Referential Structures 1/4
struct node {
A structure of type struct node
int data;
struct node *next; };
data next
struct node a,b;
a b
a b
1 &b 2 NULL
a b
a.next=&b
*(a.next).data =? a.next->data =?
3
Self Referential Structures 3/4
struct node { int data; struct node *next; };
struct node * p1 ;
struct node * p2;
// Create objects using pointers, pointers store the address of each object
p1= (struct node *)malloc(sizeof(struct node));
p2= (struct node *)malloc(sizeof(struct node));
p1->data = 4; p1 p2
p2->data= 5;
data=4 // next=p2 data=5 // next=NULL
4
Self Referential Structures 4/4
p1->next == p2
p1->next->next == p3
p2->data == p1->next->data
p3->data == p1->next->next->data
5
Linear Linked Lists
data next data next data next NULL
head head->next
head head->next
struct node
{ int data; struct node *next; };
struct node * head; // declares the pointer for first node (head)
7
Linear Linked Lists : 2 nodes in main()
struct node
{ int data; struct node *next; };
main()
{ struct node * head;
/* Create List */
head = (struct node *)malloc(sizeof(struct node));
head
head->data=1; data=1
head->next=NULL; next=NULL
main()
{ NODE * head;
head
head = malloc(sizeof(NODE));
data=1
head->data=1; head->next=NULL; next=NULL
previous previous
Head
next
next next
NULL
Circle linked lists (next)
10
Basic Linear Linked List Operations
1. Creating a list
2. Counting elements of list
3. Printing data in list
4. Inserting elements(nodes) to lists
5. Deleting elements(nodes) of list
6. Finding/searching elements in the list
7. Sorting elements
8. etc..
11
CREATING LINEAR LINKED LIST
An integer array (x[4]) will be used to create a list.
12
Example 1: In main() add from head
typedef struct node {int data; struct node *next; } NODE;
main(){ int x[4]={1,2,3,4};
NODE * head=NULL; NODE* tmp=NULL;
for (i=0; i<4; i++)
{ if(head==NULL) // FIRST NODE IN LIST
{ head=malloc(sizeof(NODE));
head->data=x[i];
head->next =NULL; }
else { tmp=malloc(sizeof(NODE));
tmp->data=x[i];
tmp->next=head;
head=tmp; }
}
}
16
How to call create functions in Example 3 and 4
typedef struct node
{ int data; struct node *next; } NODE;
NODE * create_ite (int x[] , int size) ; // prototype for iterative function
NODE * create _rec(int x[], int size) ; // prototype for recursive function
main()
{
int x[4]={1,2,3,4};
NODE * head1;
NODE * head2;
head1=create_ite(x,4);
head2=create_rec(x,4); }
17
Count Elements of a List
18
Example 1 : Iteration
int count_list_ite (NODE * head)
{ int count=0;
for (; head != NULL; head = head -> next)
++count;
return count; }
Example 2 : Recursion
int count_list_rec (NODE * head)
{ if (head == NULL) return 0;
else return(1 + count_list_rec(head -> next)); }
19
How to call count functions in Example 1 and 2
typedef struct node
{ int data; struct node *next; } NODE;
main()
{
int x[4]={1,2,3,4}; int size1, size2 ;
NODE * head;
head=create(x,4);
size1= count_list_ite(head);
size2= count_list_rec(head); }
20
Print Elements of a List
21
Example 1 : Using Iteration
void print_ite (NODE * head)
{ NODE * p;
if (head == NULL)
printf(“NULL list”);
else { for (p = head; p != NULL; p = p -> next)
printf(“%d\n ”, p -> data); }
}
22
Example 2 : Using Recursion
void print_rec (NODE * head)
{
if (head == NULL) printf(“NULL list”);
else { printf(“%d\n”, head -> data);
print_rec(head ->next);
}
}
23
Insertion of Elements in a List
void insert(NODE * p1, NODE * p2, NODE * q)
{ assert (p1-> next == p2);
/* if the expression inside assert is false, the system will print a
message and the program will be aborted */
p1->next = q;
q->next = p2; } initially
p2
C next
p1 A next
NULL
B next
q
24
How to call insert function
typedef struct node
{ int data; struct node *next; } NODE;
/* Function prototypes */
NODE * create_rec( int x[], int size);
void insert(NODE * p1, NODE * p2, NODE * q)
void print_ite (NODE * head)
main()
{ int x[4]={1,2,3,4};
NODE * head;
NODE n;
n.data=7; n.next=NULL;
head=create(x,4);
insert(head->next,head->next->next, &n);
print_ite(head); }
25
Delete Elements of a List
26
Example 1 : Using Iteration
void delete (NODE * head)
{ NODE * p; NODE * q;
if (head == NULL) printf(“NULL list”);
else { p=head;
while (p != NULL;)
{ q=p;
p = p -> next ;
free(q); } }
}
27
Example 2 : Using Recursion
void delete (NODE * head)
{
if (head != NULL)
{ delete(head ->next);
free(head);
}
}
28