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

L4-Linked listsBLC

The document discusses linked lists and various operations on them like initialization, insertion at the beginning and end, and finding/deleting nodes. It includes code snippets for initializing an empty linked list and inserting nodes at the beginning by changing the next pointer of the head node. Inserting at the end requires traversing the list to find the tail node and adding the new node after it.

Uploaded by

shamiul alam
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

L4-Linked listsBLC

The document discusses linked lists and various operations on them like initialization, insertion at the beginning and end, and finding/deleting nodes. It includes code snippets for initializing an empty linked list and inserting nodes at the beginning by changing the next pointer of the head node. Inserting at the end requires traversing the list to find the tail node and adding the new node after it.

Uploaded by

shamiul alam
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 35

Linked Lists

Review Discussion
CSEDIU
struct node {
Menu
int data;
struct node* next;
};

typedef struct node Node;


typedef struct node* List;

List Initialize();
void InsertBegin(List l,int d);
void InsertEnd(List l, int d);
void Insert(List l, Node* pos,int d);
Node* Find(List l,int d);
void Delete(List l, int d);

CSE@DIU
Menu Menu

Initialize
InsertBegin
InsertEnd
Insert
Find
Delete

CSE@DIU
Menu

Initialize

CSE@DIU
Menu

List Initialize()
{
Node* temp;
temp = (Node*)calloc(1,sizeof(Node));
return temp;
}

CSE@DIU
Menu

head

List Initialize()
{
Node* temp;
main() temp = (Node*)calloc(1,sizeof(Node));
{ return temp;
List head; }
head = Initialize();
}

CSE@DIU
Menu

InsertBegin

CSE@DIU
Menu
1 10 8 4 6 3 2 5

head

CSE@DIU
Menu
1 10 8 4 6 3 2 5

head

void InsertBegin(List head,int d)


{
Node* temp;
temp = (Node*)calloc(1,sizeof(Node));
temp->data = d;

temp->next = head->next;
head->next = temp;
}
CSE@DIU
Menu
1 10 8 4 6 3 2 5

head

void InsertBegin(List head,int d)


1
{
Node* temp;
temp = (Node*)calloc(1,sizeof(Node));
temp->data = d;

temp->next = head->next;
head->next = temp;
}
CSE@DIU
Menu
1 10 8 4 6 3 2 5

head

X 1

CSE@DIU
Menu
1 10 8 4 6 3 2 5

head

void InsertBegin(List head,int d)


1
{
Node* temp;
temp = (Node*)calloc(1,sizeof(Node));
temp->data = d;

temp->next = head->next;
head->next = temp;
}
CSE@DIU
Menu
1 10 8 4 6 3 2 5

head

void InsertBegin(List head,int d)


1
{
Node* temp;
temp = (Node*)calloc(1,sizeof(Node));
temp->data = d;

temp->next = head->next;
head->next = temp;
}
CSE@DIU
Menu
1 10 8 4 6 3 2 5

head

void InsertBegin(List head,int d)


1 {
Node* temp;
temp = (Node*)calloc(1,sizeof(Node));
temp->data = d;

head->next = temp;
temp->next = head->next;
}
CSE@DIU
Menu
1 10 8 4 6 3 2 5

head

X 1

void InsertBegin(List head,int d)


{
Node* temp;
temp = (Node*)calloc(1,sizeof(Node));
temp->data = d;

temp->next = head->next;
head->next = temp;
}
CSE@DIU
Menu
1 10 8 4 6 3 2 5

head

X 1

void InsertBegin(List head,int d)


10
{
Node* temp;
temp = (Node*)calloc(1,sizeof(Node));
temp->data = d;

temp->next = head->next;
head->next = temp;
}
CSE@DIU
Menu
1 10 8 4 6 3 2 5

head

X 1

void InsertBegin(List head,int d)


10
{
Node* temp;
temp = (Node*)calloc(1,sizeof(Node));
temp->data = d;

head->next = temp;
temp->next = head->next;
}
CSE@DIU
Menu

InsertEnd

CSE@DIU
Menu
1 10 8 4 6 3 2 5

head

X 10 1

tail

void InsertEnd(List head,int d)


{
Node *tail,*temp;
tail = head;
........
........
}

CSE@DIU
Menu
1 10 8 4 6 3 2 5

head

X 10 1

tail

void InsertEnd(List head,int d)


{
Node *tail,*temp;
tail = head;
while(tail->next != NULL)
tail = tail->next;
........
........
}

CSE@DIU
Menu
1 10 8 4 6 3 2 5

head

X 10 1

tail

void InsertEnd(List head,int d)


{
Node *tail,*temp;
tail = head;
while(tail->next != NULL)
tail = tail->next;
........
........
}

CSE@DIU
Menu
1 10 8 4 6 3 2 5

head tail

X 10 1

8
void InsertEnd(List head,int d)
{
........
........
temp = (Node*)calloc(1,sizeof(Node));
temp->data = d;

tail->next = temp;

CSE@DIU
Menu

Insert

CSE@DIU
Menu
1 10 8 4 6 3 2 5

head

X 10 1 8

void Insert(List head,Node* p,int d)


{
temp = (Node*)calloc(1,sizeof(Node));
temp->data = d;

temp->next = p->next;
p->next = temp;
}

CSE@DIU
Menu
1 10 8 4 6 3 2 5

head

X 10 1 8

4
void Insert(List head,Node* p,int d)
{
temp = (Node*)calloc(1,sizeof(Node));
temp->data = d;

temp->next = p->next;
p->next = temp;
}

CSE@DIU
Menu
1 10 8 4 6 3 2 5

head

X 10 1 4 8

CSE@DIU
Menu

Find

CSE@DIU
Menu
1 10 8 4 6 3 2 5

head

X 10 1 4 8

void Find(List l,Node* p,int d)


{
Node *temp;
temp = l;
while(temp->next != NULL)
{
if(temp->next->data == d)
return temp;
temp = temp->next;
}
return NULL;
}
CSE@DIU
Menu
1 10 8 4 6 3 2 5

head temp

X 10 1 4 8

void Find(List l,Node* p,int d)


{
Node *temp;
temp = l;
while(temp->next != NULL)
{
if(temp->next->data == d)
return temp;
temp = temp->next;
}
return NULL;
}
CSE@DIU
Menu

Delete

CSE@DIU
Menu
1 10 8 4 6 3 2 5

head

X 10 1 4 8

void Delete(List l,Node* p,int d)


{
Node *temp,*del;
temp = Find(l,d);
if(temp != NULL)
{
del = temp->next;
temp->next = del->next;
free(del);
}
}
CSE@DIU
Menu
1 10 8 4 6 3 2 5

head temp del

X 10 1 4 8

void Delete(List l,Node* p,int d)


{
Node *temp,*del;
temp = Find(l,d);
if(temp != NULL)
{
del = temp->next;
temp->next = del->next;
free(del);
}
}
CSE@DIU
Menu
10 8 4 6 3 2 5

head

X 10 4 8

CSE@DIU
Menu

int main
{
List l;
Node* temp;
l = Initialize();
InsertBegin(l,1);
InsertBegin(l,10);
InsertEnd(l,8);
temp = Find(l,8);
Insert(l,temp,4);
Delete(l,1);
}
CSE@DIU
Menu

Enjoy Linking You in List

CSE@DIU

You might also like