Anda di halaman 1dari 35

Linked Lists

CH Gowri Kumar gkumar007@gmail.com

struct node { int data; struct node* next; };

Menu

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

January 26, 2014

http://www.gowrikumar.com

Menu
Initialize InsertBegin InsertEnd Insert Find Delete

Menu

January 26, 2014

http://www.gowrikumar.com

Menu

Initialize

January 26, 2014

http://www.gowrikumar.com

Menu

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

January 26, 2014

http://www.gowrikumar.com

Menu

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

List head;
head = Initialize(); }
January 26, 2014

http://www.gowrikumar.com

Menu

InsertBegin

January 26, 2014

http://www.gowrikumar.com

Menu 1 head X 10 8 4 6 3 2 5

January 26, 2014

http://www.gowrikumar.com

Menu 1 head X void InsertBegin(List head,int d) { Node* temp; temp = (Node*)calloc(1,sizeof(Node)); temp->data = d; temp->next = head->next; head->next = temp; }
January 26, 2014 http://www.gowrikumar.com

10

Menu 1 head X 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; }
January 26, 2014 http://www.gowrikumar.com

10

Menu 1 head X 1 10 8 4 6 3 2 5

January 26, 2014

http://www.gowrikumar.com

Menu 1 head X 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; }
January 26, 2014 http://www.gowrikumar.com

10

Menu 1 head X 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; }
January 26, 2014 http://www.gowrikumar.com

10

Menu 1 head X 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; }
January 26, 2014 http://www.gowrikumar.com

10

Menu 1 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; }
January 26, 2014 http://www.gowrikumar.com

10

Menu 1 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; }
January 26, 2014 http://www.gowrikumar.com

10

Menu 1 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; }
January 26, 2014 http://www.gowrikumar.com

10

Menu

InsertEnd

January 26, 2014

http://www.gowrikumar.com

Menu 1 head X tail void InsertEnd(List head,int d) { Node *tail,*temp; tail = head; ........ ........ } 10 1 10 8 4 6 3 2 5

January 26, 2014

http://www.gowrikumar.com

Menu 1 head X 10 tail void InsertEnd(List head,int d) { Node *tail,*temp; tail = head; while(tail->next != NULL) tail = tail->next; ........ ........ }
January 26, 2014 http://www.gowrikumar.com

10

Menu 1 head X 10 1 tail void InsertEnd(List head,int d) { Node *tail,*temp; tail = head; while(tail->next != NULL) tail = tail->next; ........ ........ }
January 26, 2014 http://www.gowrikumar.com

10

Menu 1 head X 10 10 8 4 6 tail 1 3 2 5

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


tail->next = temp; }

January 26, 2014

http://www.gowrikumar.com

Menu

Insert

January 26, 2014

http://www.gowrikumar.com

Menu 1 head X 10 1 8 10 8 4 6 3 2 5

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


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

January 26, 2014

http://www.gowrikumar.com

Menu 1 head X 10 1 8 10 8 4 6 3 2 5

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


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

January 26, 2014

http://www.gowrikumar.com

Menu 1 head X 10 1 4 8 10 8 4 6 3 2 5

January 26, 2014

http://www.gowrikumar.com

Menu

Find

January 26, 2014

http://www.gowrikumar.com

Menu 1 head X 10 1 4 8 10 8 4 6 3 2 5

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; }
January 26, 2014 http://www.gowrikumar.com

Menu 1 head temp X 10 1 4 8 10 8 4 6 3 2 5

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; }
January 26, 2014 http://www.gowrikumar.com

Menu

Delete

January 26, 2014

http://www.gowrikumar.com

Menu 1 head X 10 1 4 8 10 8 4 6 3 2 5

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); } }
January 26, 2014 http://www.gowrikumar.com

Menu 1 head X 10 8 4 6 del 1 4 8 3 2 5

temp 10

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); } }
January 26, 2014 http://www.gowrikumar.com

Menu 10 head X 10 4 8 8 4 6 3 2 5

January 26, 2014

http://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); }
January 26, 2014 http://www.gowrikumar.com

Menu

The End

January 26, 2014

http://www.gowrikumar.com

Anda mungkin juga menyukai