Presentation is loading. Please wait.

Presentation is loading. Please wait.

Linked list.

Similar presentations


Presentation on theme: "Linked list."— Presentation transcript:

1 Linked list

2 Linked List Linked lists are type of data structure, which is a way to represent data in memory. Memory is requested from the operating system as needed, with each additional piece of allocated memory added to our list. For example, if we had the numbers 5, 6, 9, 2, our linked list might look like this: 6 5 9 2

3 Creating Linked List In order for us to navigate our list, we must keep track of the address of each piece of allocated memory. We do this by creating a structure that contains a variable for storing our data as well as a pointer variable for storing the address of the next node in the list. In the simplest case, a node in the linked list will have this form: struct node { int value; struct node *next; };

4 Arrays vs Linked Lists Linked lists are an alternative to using arrays to store multiple, related values. Why not just use arrays? Arrays Pros Contiguous memory allows easy navigation and the use of row offsets Easy on programmer Cons Can't easily insert/delete items - time complexity Can't free after use if statically allocated arrays

5 Arrays vs Linked Lists Linked List Pros Cons
Don't need to know the number of elements in advance Can insert new elements without moving other elements Add/delete new elements Can release unneeded memory Cons Not easy to understand and manage Not easy to access a specific node

6 Linked List implementation in C
struct node { int data; struct node *next; }; A 200 300 400 200 2 300 4 400 6 NULL Pointer to head node data next

7 Linked List implementation in C
struct node { int data; struct node *next; }; node *A; A = NULL; //empty list A 200 300 400 200 2 300 4 400 6 NULL Pointer to head node data next int node A NULL

8 Linked List implementation in C
struct node { int data; struct node *next; }; node *A; A = NULL; //empty list node *temp = (node *) malloc(sizeof(node)); (*temp).data = 2; (*temp).next = NULL; A = temp; 200 2 300 4 400 6 A NULL data next Pointer to head node int 200 2 A NULL temp

9 Linked List implementation in C
struct node { int data; struct node *next; }; node *A; A = NULL; //empty list node *temp = (node *) malloc(sizeof(node)); (*temp).data = 2; (*temp).next = NULL; A = temp; 200 2 300 4 400 6 A NULL data next Pointer to head node int 200 2 A NULL

10 Linked List implementation in C
struct node { int data; struct node *next; }; node *A; A = NULL; //empty list node *temp = (node *) malloc(sizeof(node)); temp -> data = 2; temp -> next = NULL; A = temp; 200 2 300 4 400 6 A NULL data next Pointer to head node int 200 2 A NULL

11 Linked List implementation in C
struct node { int data; struct node *next; }; node *A; A = NULL; //empty list node *temp = (node *) malloc(sizeof(node)); temp -> data = 2; temp -> next = NULL; A = temp; temp = (node *) malloc(sizeof(node)); temp -> data = 4; 300 200 2 A NULL 4 Traversal node *temp1 = A; while (temp1 -> next != NULL) { temp1 = temp1 -> next; printf (“temp1 -> data”); } 300 temp NULL temp1

12 Insert a node in the linked list
There are various ways to insert a node At the beginning At the end At any specific position

13 Insert a node at the beginning

14 #include<stdio. h> #include<stdlib
#include<stdio.h> #include<stdlib.h> struct node { int data; struct node* next; }; struct node* head; void Insert(int x) { node *temp = (node*)malloc(sizeof(struct node)); temp->data = x; temp->next=head; head=temp; } void Print(){ struct node*temp=head; printf("List is:"); while(temp!=NULL) { printf("%d ",temp->data); temp=temp->next; } printf("\n"); int main(){ head = NULL; //empty list int n, i, x; printf("How many numbers?\n"); scanf("%d",&n); for (i=0;i<n;i++) printf("Enter the number\n"); scanf("%d",&x); Insert(x); Print();

15 Insert a node at nth position

16 #include<stdio. h> #include<stdlib
#include<stdio.h> #include<stdlib.h> struct node { int data; struct node* next; }; struct node* head; void Insert(int data, int n) { node *temp1 = (node*)malloc(sizeof(struct node)); temp1->data = data; temp1->next=NULL; if(n==1){ temp1->next=head; head=temp1; return; } node* temp2=head; for(int i=0;i<n-2;i++) temp2=temp2->next; temp1->next=temp2->next; temp2->next=temp1; void Print(){ struct node*temp=head; printf("List is:"); while(temp!=NULL) { printf("%d ",temp->data); temp=temp->next; } printf("\n"); int main(){ head = NULL; //empty list Insert(2,1); //list:2 Insert(3,2);//list: 2,3 Insert(4,1);//list: 4,2,3 Insert(5,2); //list: 4,5,2,3 Print();

17 Delete a node at nth position

18 struct node*temp=head; printf("List is:"); while(temp!=NULL) {
void print(){ struct node*temp=head; printf("List is:"); while(temp!=NULL) { printf("%d ",temp->data); temp=temp->next; } printf("\n"); void delete1(int n) { struct node* temp1 = head; if (n==1){ head = temp1->next; //head now points to the second node free(temp1); return; } int i; for(i=0;i<n-2;i++) temp1=temp1->next; //temp1 points to (n-1)th node struct node* temp2 = temp1->next; //nth node temp1->next = temp2->next; //(n+1)th node free(temp2); //delete temp2 int main(){ head = NULL; //empty list Insert(2, 1); Insert(4, 2); Insert(6, 1); Insert(5, 2); //list: 2,4,6,5 print(); int n; printf("Enter a position\n"); scanf("%d",&n); delete1(n); #include<stdio.h> #include<stdlib.h> struct node { int data; struct node* next; }; struct node* head; void Insert(int data, int n) { node *temp1 = (node*)malloc(sizeof(struct node)); temp1->data = data; temp1->next=NULL; if(n==1){ temp1->next=head; head=temp1; return; } node* temp2=head; for(int i=0;i<n-2;i++) temp2=temp2->next; temp1->next=temp2->next; temp2->next=temp1;

19 Tutorial on Doubly Linked List
Can you create a doubly linked-list? Insert a node in or delete a node from the list? Search a node? structures-doubly-linked-list-with-c-program-source-code

20 Study Reference Stack and Queue
structures-stacks--with-c-program-source-code structures-queues--with-c-program-source-code


Download ppt "Linked list."

Similar presentations


Ads by Google