linked lists

35
Linked Lists Linked Lists CH Gowri Kumar CH Gowri Kumar [email protected] [email protected]

Upload: gowrikumar-chandramouli

Post on 11-May-2015

4.433 views

Category:

Education


0 download

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.

TRANSCRIPT

Page 1: Linked lists

Linked ListsLinked Lists

CH Gowri KumarCH Gowri Kumar

[email protected]@gmail.com

Page 2: Linked lists

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

Page 3: Linked lists

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

MenuMenuMenu

InitializeInitialize

InsertBeginInsertBegin

InsertEndInsertEnd

InsertInsert

FindFind

DeleteDelete

Page 4: Linked lists

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

Menu

InitializeInitialize

Page 5: Linked lists

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;

}

Page 6: Linked lists

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

}

Page 7: Linked lists

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

Menu

InsertBeginInsertBegin

Page 8: Linked lists

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

Menu

X

head

1 10 8 4 6 3 2 5

Page 9: Linked lists

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;

}

Page 10: Linked lists

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

Page 11: Linked lists

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

Menu

X

head

1 10 8 4 6 3 2 5

1

Page 12: Linked lists

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

Page 13: Linked lists

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

Page 14: Linked lists

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

Page 15: Linked lists

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;

}

Page 16: Linked lists

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;

}

Page 17: Linked lists

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;

}

Page 18: Linked lists

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

Menu

InsertEndInsertEnd

Page 19: Linked lists

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

Page 20: Linked lists

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

Page 21: Linked lists

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

Page 22: Linked lists

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

Page 23: Linked lists

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

Menu

InsertInsert

Page 24: Linked lists

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;

}

Page 25: Linked lists

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;

}

Page 26: Linked lists

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

Page 27: Linked lists

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

Menu

FindFind

Page 28: Linked lists

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;

}

Page 29: Linked lists

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

Page 30: Linked lists

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

Menu

DeleteDelete

Page 31: Linked lists

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

}}

Page 32: Linked lists

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

Page 33: Linked lists

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

Page 34: Linked lists

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

}

Page 35: Linked lists

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

Menu

The EndThe End