Linked list.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Linked Lists.
Singly Linked List BTECH, EE KAZIRANGA UNIVERSITY.
Data Structure Lecture-5
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
CCSB364 Data Structures & Algorithms Pointer & Linked List.
Self Referential Structure. A structure may not contain a member of its own type. struct check { int item; struct check n; // Invalid };
Implementation of Linked List For more notes and topics visit: eITnotes.com.
D ATA S TRUCTURE Ali Abdul Karem Habib MSc.IT. P OINTER A pointer is a variable which represents the location of a data item. We can have a pointer to.
A first look an ADTs Solving a problem involves processing data, and an important part of the solution is the careful organization of the data In order.
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
1. Circular Linked List In a circular linked list, the last node contains a pointer to the first node of the list. In a circular linked list,
Circular linked list A circular linked list is a linear linked list accept that last element points to the first element.
By Anand George SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)
Linked list: a list of items (nodes), in which the order of the nodes is determined by the address, called the link, stored in each node C++ Programming:
1 Midterm 1 on Friday February 12 Closed book, closed notes No computer can be used 50 minutes 4 questions Write a function Write program fragment Explain.
Linked List. LINKED LIST Link is collection of similar type of elements. There are two ways of maintaining a list in memory. The first way is store the.
Data Structure & Algorithms
1 Linked List. 2 List A list refers to a sequence of data items  Example: An array The array index is used for accessing and manipulation of array elements.
Linked List. Insert a node at the beginning #include struct node { int data; struct node* next; }; struct node* head; void Insert(int x) { node *temp.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
STACKS & QUEUES for CLASS XII ( C++).
CSE 1342 Programming Concepts
Unit – I Lists.
Cpt S 122 – Data Structures Abstract Data Types
Unit-1 Data Structures – Introduction to Data Structures, abstract data types, Linear list – singly linked list implementation, insertion, deletion and.
Chapter 12 – Data Structures
Linked List :: Basic Concepts
CSCI-255 LinkedList.
Lectures linked lists Chapter 6 of textbook
Program based on queue & their operations for an application
UNIT – I Linked Lists.
CMSC202 Computer Science II for Majors Lecture 12 – Linked Lists
Linked List.
UNIT-3 LINKED LIST.
Queue data structure.
Stacks.
Stack and Queue APURBO DATTA.
Linked Lists head One downside of arrays is that they have a fixed size. To solve this problem of fixed size, we’ll relax the constraint that the.
Lists.
Tree data structure.
Linked List Sudeshna Sarkar.
Programmazione I a.a. 2017/2018.
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
CMSC 341 Lecture 5 Stacks, Queues
Lists.
List Objectives Describe a list
Tree data structure.
Arrays and Linked Lists
Linked List Lesson xx   In this presentation, we introduce you to the basic elements of a linked list.
Tree data structure.
Pointers and dynamic memory
Tree data structure.
Further Data Structures
Review & Lab assignments
Data structure types. Linear and Non-Linear Data Structures: Linked lists, stacks and queues.
Linked Lists Chapter 4.
Linked List.
Chapter 17: Linked Lists.
Chapter 16 Linked Structures
LINKED LIST.
Lists.
General List.
Linked Lists.
Linked Lists.
BY PROF. IRSHAD AHMAD LONE.
Data Structures.
Variable Storage Memory Locations (Logical) Variable Classes Stack
Presentation transcript:

Linked list

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

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

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

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

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

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

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

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

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

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

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

Insert a node at the beginning

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

Insert a node at nth position

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

Delete a node at nth position

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;

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? http://www.thelearningpoint.net/computer-science/data- structures-doubly-linked-list-with-c-program-source-code

Study Reference Stack and Queue http://www.thelearningpoint.net/computer-science/data- structures-stacks--with-c-program-source-code http://www.thelearningpoint.net/computer-science/data- structures-queues--with-c-program-source-code