0% found this document useful (0 votes)
20 views15 pages

Lect06,08 Linked Lists

A linked list is a dynamic data structure that uses pointers to link its nodes. It allows for automatic memory allocation as new nodes can be added whenever needed. A linked list node contains a data field and a pointer to the next node. The first node is created using malloc and its next pointer is set to null. More nodes can then be added by changing the next pointer of the previous node. Data from nodes can be printed by traversing the list from the root node. Nodes can be inserted or deleted anywhere in the list by adjusting the next pointers.

Uploaded by

Michel Samir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views15 pages

Lect06,08 Linked Lists

A linked list is a dynamic data structure that uses pointers to link its nodes. It allows for automatic memory allocation as new nodes can be added whenever needed. A linked list node contains a data field and a pointer to the next node. The first node is created using malloc and its next pointer is set to null. More nodes can then be added by changing the next pointer of the previous node. Data from nodes can be printed by traversing the list from the root node. Nodes can be inserted or deleted anywhere in the list by adjusting the next pointers.

Uploaded by

Michel Samir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 15

What Is a Linked List (LL)?

Data
Next

(Pointer) node

root

 Dynamic data structure that uses pointers for its


implementation.

 Programmer can automatically create a new place to store


data whenever necessary (train).
Define a LL Node

Data
Next
(Pointer) node

root

struct node
{
int x; //Data (1 or more items)
struct node *next; //Next (Pointer)
};
Create The First Node

root

#include <stdlib.h> //malloc Function


void main()
{
struct node *root; //Create Pointer
root=(struct node*)malloc(sizeof(struct node));
root->x = 12; //Store data value
root->next = 0; //Null Pointer
}
 malloc Function:
C library function located in stdlib.h library. It allocates the
requested memory and returns a pointer to it.

root=(struct node*)malloc(sizeof(struct node));

Type Casting Number of bytes to reserve


Create More Nodes

root

1 node 1 2 node 2 3

3
Print Nodes’ Data

root
currentnode node 1 node 2
Insert a Node

Old value

2 50 1
Inserted node

1
2
Delete a Node (node 1)

Old value

1
Complete Program

Circular Linked List
e.g. multi-player game – music list
Data

Next
(Pointer) node

root

struct node
{
int x; //Data (1 or more items)
struct node *next; //Next (Pointer)
};
Create The First Node

root

#include <stdlib.h> //malloc Function


void main()
{
struct node *root; //Create Pointer
root=(struct node*)malloc(sizeof(struct node));
root->x = 12; //Store data value
root->next = 0; root->next = root;
Replace with //First node
}
Create More Nodes
 node 1 node 2
root
1 2
3

3 Replace with
Print Nodes’ Data
 node 1 node 2
root
currentnode
Doubly Linked List
e.g. bus stop (where one can go in both
Data

directions)
root
next (Pointer)

node Previous (Pointer)

struct node
{
int x; //Data (1 or more items)
struct node *next; //Next (Pointer)
struct node *previous; //Previous (Pointer)
};

You might also like