Linked lists

Preview:

DESCRIPTION

This presentations gives an introduction to the data structure linked-lists. I discuss the implementation of header-based linked-lists in C. The presentation runs through the code and provides the visualization of the code w.r.t pointers.

Citation preview

Linked ListsLinked Lists

CH Gowri KumarCH Gowri Kumar

gkumar007@gmail.comgkumar007@gmail.com

April 12, 2023April 12, 2023 http://www.gowrikumar.comhttp://www.gowrikumar.com

Menustruct node {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);

April 12, 2023April 12, 2023 http://www.gowrikumar.comhttp://www.gowrikumar.com

MenuMenuMenu

InitializeInitialize

InsertBeginInsertBegin

InsertEndInsertEnd

InsertInsert

FindFind

DeleteDelete

April 12, 2023April 12, 2023 http://www.gowrikumar.comhttp://www.gowrikumar.com

Menu

InitializeInitialize

April 12, 2023April 12, 2023 http://www.gowrikumar.comhttp://www.gowrikumar.com

Menu

List Initialize()

{

Node* temp;

temp = (Node*)calloc(1,sizeof(Node));

return temp;

}

April 12, 2023April 12, 2023 http://www.gowrikumar.comhttp://www.gowrikumar.com

Menu

List Initialize()

{

Node* temp;

temp = (Node*)calloc(1,sizeof(Node));

return temp;

}

X

head

main()

{

List head;

head = Initialize();

}

April 12, 2023April 12, 2023 http://www.gowrikumar.comhttp://www.gowrikumar.com

Menu

InsertBeginInsertBegin

April 12, 2023April 12, 2023 http://www.gowrikumar.comhttp://www.gowrikumar.com

Menu

X

head

1 10 8 4 6 3 2 5

April 12, 2023April 12, 2023 http://www.gowrikumar.comhttp://www.gowrikumar.com

Menu

X

head

1 10 8 4 6 3 2 5

void InsertBegin(List head,int d)

{

Node* temp;

temp = (Node*)calloc(1,sizeof(Node));

temp->data = d;

temp->next = head->next;

head->next = temp;

}

April 12, 2023April 12, 2023 http://www.gowrikumar.comhttp://www.gowrikumar.com

Menu

X

head

1 10 8 4 6 3 2 5

void InsertBegin(List head,int d)

{

Node* temp;

temp = (Node*)calloc(1,sizeof(Node));

temp->data = d;

temp->next = head->next;

head->next = temp;

}

1

April 12, 2023April 12, 2023 http://www.gowrikumar.comhttp://www.gowrikumar.com

Menu

X

head

1 10 8 4 6 3 2 5

1

April 12, 2023April 12, 2023 http://www.gowrikumar.comhttp://www.gowrikumar.com

Menu1 10 8 4 6 3 2 5

void InsertBegin(List head,int d)

{

Node* temp;

temp = (Node*)calloc(1,sizeof(Node));

temp->data = d;

temp->next = head->next;

head->next = temp;

}

X

head

1

April 12, 2023April 12, 2023 http://www.gowrikumar.comhttp://www.gowrikumar.com

Menu1 10 8 4 6 3 2 5

void InsertBegin(List head,int d)

{

Node* temp;

temp = (Node*)calloc(1,sizeof(Node));

temp->data = d;

temp->next = head->next;

head->next = temp;

}

X

head

1

April 12, 2023April 12, 2023 http://www.gowrikumar.comhttp://www.gowrikumar.com

Menu1 10 8 4 6 3 2 5

void InsertBegin(List head,int d)

{

Node* temp;

temp = (Node*)calloc(1,sizeof(Node));

temp->data = d;

head->next = temp;

temp->next = head->next;

}

X

head

1

April 12, 2023April 12, 2023 http://www.gowrikumar.comhttp://www.gowrikumar.com

Menu

X

head

1 10 8 4 6 3 2 5

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;

}

April 12, 2023April 12, 2023 http://www.gowrikumar.comhttp://www.gowrikumar.com

Menu

X

head

1 10 8 4 6 3 2 5

1

10void InsertBegin(List head,int d)

{

Node* temp;

temp = (Node*)calloc(1,sizeof(Node));

temp->data = d;

temp->next = head->next;

head->next = temp;

}

April 12, 2023April 12, 2023 http://www.gowrikumar.comhttp://www.gowrikumar.com

Menu

X

head

1 10 8 4 6 3 2 5

1

10void InsertBegin(List head,int d)

{

Node* temp;

temp = (Node*)calloc(1,sizeof(Node));

temp->data = d;

head->next = temp;

temp->next = head->next;

}

April 12, 2023April 12, 2023 http://www.gowrikumar.comhttp://www.gowrikumar.com

Menu

InsertEndInsertEnd

April 12, 2023April 12, 2023 http://www.gowrikumar.comhttp://www.gowrikumar.com

Menu

X

head

1 10 8 4 6 3 2 5

10 1

void InsertEnd(List head,int d){

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

}

tail

April 12, 2023April 12, 2023 http://www.gowrikumar.comhttp://www.gowrikumar.com

Menu1 10 8 4 6 3 2 5

void InsertEnd(List head,int d){

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

tail = tail->next;. . . . . . . .. . . . . . . .

}

X

head

10 1

tail

April 12, 2023April 12, 2023 http://www.gowrikumar.comhttp://www.gowrikumar.com

Menu1 10 8 4 6 3 2 5

void InsertEnd(List head,int d){

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

tail = tail->next;. . . . . . . .. . . . . . . .

}

X

head

10 1

tail

April 12, 2023April 12, 2023 http://www.gowrikumar.comhttp://www.gowrikumar.com

Menu

X

head

1 10 8 4 6 3 2 5

10 1

void InsertEnd(List head,int d){

. . . . . . . .

. . . . . . . . temp =

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

tail->next = temp;

}

8

tail

April 12, 2023April 12, 2023 http://www.gowrikumar.comhttp://www.gowrikumar.com

Menu

InsertInsert

April 12, 2023April 12, 2023 http://www.gowrikumar.comhttp://www.gowrikumar.com

Menu

X

head

1 10 8 4 6 3 2 5

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;

}

April 12, 2023April 12, 2023 http://www.gowrikumar.comhttp://www.gowrikumar.com

Menu

X

head

1 10 8 4 6 3 2 5

10 1 8

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

temp = (Node*)calloc(1,sizeof(Node));

temp->data = d;

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

}

April 12, 2023April 12, 2023 http://www.gowrikumar.comhttp://www.gowrikumar.com

Menu

X

head

1 10 8 4 6 3 2 5

10 1 4 8

April 12, 2023April 12, 2023 http://www.gowrikumar.comhttp://www.gowrikumar.com

Menu

FindFind

April 12, 2023April 12, 2023 http://www.gowrikumar.comhttp://www.gowrikumar.com

Menu

X

head

1 10 8 4 6 3 2 5

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;

}

April 12, 2023April 12, 2023 http://www.gowrikumar.comhttp://www.gowrikumar.com

Menu

X

head

1 10 8 4 6 3 2 5

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;

}

temp

April 12, 2023April 12, 2023 http://www.gowrikumar.comhttp://www.gowrikumar.com

Menu

DeleteDelete

April 12, 2023April 12, 2023 http://www.gowrikumar.comhttp://www.gowrikumar.com

Menu

X

head

1 10 8 4 6 3 2 5

10 41 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);

}}

April 12, 2023April 12, 2023 http://www.gowrikumar.comhttp://www.gowrikumar.com

Menu

X

head

1 10 8 4 6 3 2 5

10 41 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);

}}

temp del

April 12, 2023April 12, 2023 http://www.gowrikumar.comhttp://www.gowrikumar.com

Menu

X

head

10 8 4 6 3 2 5

10 4 8

April 12, 2023April 12, 2023 http://www.gowrikumar.comhttp://www.gowrikumar.com

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);

}

April 12, 2023April 12, 2023 http://www.gowrikumar.comhttp://www.gowrikumar.com

Menu

The EndThe End

Recommended