350 likes | 364 Views
Learn how to initialize, insert, find, and delete nodes in a linked list using C language code snippets. Perfect for beginners.
E N D
Linked Lists CH Gowri Kumar gkumar007@gmail.com
struct 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); http://www.gowrikumar.com
Initialize InsertBegin InsertEnd Insert Find Delete Menu http://www.gowrikumar.com
Initialize http://www.gowrikumar.com
List Initialize() { Node* temp; temp = (Node*)calloc(1,sizeof(Node)); return temp; } http://www.gowrikumar.com
head X List Initialize() { Node* temp; temp = (Node*)calloc(1,sizeof(Node)); return temp; } main() { List head; head = Initialize(); } http://www.gowrikumar.com
InsertBegin http://www.gowrikumar.com
1 10 8 4 6 3 2 5 head X http://www.gowrikumar.com
1 10 8 4 6 3 2 5 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; } http://www.gowrikumar.com
1 1 10 8 4 6 3 2 5 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; } http://www.gowrikumar.com
1 1 10 8 4 6 3 2 5 head X http://www.gowrikumar.com
1 10 8 4 6 3 2 5 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; } 1 http://www.gowrikumar.com
1 10 8 4 6 3 2 5 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; } 1 http://www.gowrikumar.com
1 10 8 4 6 3 2 5 head X void InsertBegin(List head,int d) { Node* temp; temp = (Node*)calloc(1,sizeof(Node)); temp->data = d; head->next = temp; temp->next = head->next; } 1 http://www.gowrikumar.com
1 1 10 8 4 6 3 2 5 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; } http://www.gowrikumar.com
1 10 1 10 8 4 6 3 2 5 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; } http://www.gowrikumar.com
1 10 1 10 8 4 6 3 2 5 head X void InsertBegin(List head,int d) { Node* temp; temp = (Node*)calloc(1,sizeof(Node)); temp->data = d; head->next = temp; temp->next = head->next; } http://www.gowrikumar.com
InsertEnd http://www.gowrikumar.com
tail 10 1 1 10 8 4 6 3 2 5 head X void InsertEnd(List head,int d) { Node *tail,*temp; tail = head; . . . . . . . . . . . . . . . . } http://www.gowrikumar.com
tail 10 1 1 10 8 4 6 3 2 5 head X void InsertEnd(List head,int d) { Node *tail,*temp; tail = head; while(tail->next != NULL) tail = tail->next; . . . . . . . . . . . . . . . . } http://www.gowrikumar.com
tail 10 1 1 10 8 4 6 3 2 5 head X void InsertEnd(List head,int d) { Node *tail,*temp; tail = head; while(tail->next != NULL) tail = tail->next; . . . . . . . . . . . . . . . . } http://www.gowrikumar.com
tail 8 10 1 1 10 8 4 6 3 2 5 head X void InsertEnd(List head,int d) { . . . . . . . . . . . . . . . . temp = (Node*)calloc(1,sizeof(Node)); temp->data = d; tail->next = temp; } http://www.gowrikumar.com
Insert http://www.gowrikumar.com
10 1 8 1 10 8 4 6 3 2 5 head X void Insert(List head,Node* p,int d) { temp = (Node*)calloc(1,sizeof(Node)); temp->data = d; temp->next = p->next; p->next = temp; } http://www.gowrikumar.com
4 10 1 8 1 10 8 4 6 3 2 5 head X void Insert(List head,Node* p,int d) { temp = (Node*)calloc(1,sizeof(Node)); temp->data = d; temp->next = p->next; p->next = temp; } http://www.gowrikumar.com
10 1 4 1 10 8 4 6 3 2 5 head X 8 http://www.gowrikumar.com
Find http://www.gowrikumar.com
10 1 4 1 10 8 4 6 3 2 5 head X 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; } http://www.gowrikumar.com
temp 4 1 10 1 10 8 4 6 3 2 5 head X 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; } http://www.gowrikumar.com
Delete http://www.gowrikumar.com
10 4 1 1 10 8 4 6 3 2 5 head X 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); } } http://www.gowrikumar.com
del temp 1 4 10 1 10 8 4 6 3 2 5 head X 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); } } http://www.gowrikumar.com
10 4 8 10 8 4 6 3 2 5 head X http://www.gowrikumar.com
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); } http://www.gowrikumar.com
The End http://www.gowrikumar.com