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.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Linked Lists.
C and Data Structures Baojian Hua
LINKED LIST, STACKS AND QUEUES Saras M Srivastava, PGT – Comp. Sc. Kendriya Vidyalaya TengaValley.
Data Structures Linked Lists Linked List Basics. Array Disadvantages Arrays, although easy to understand have lots of disadvantages Contiguous Memory.
Singly Linked List BTECH, EE KAZIRANGA UNIVERSITY.
Linked List Variations
Data Structure Lecture-5
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Doubly-linked list library.
Computer Science C++ High School Level By Guillermo Moreno.
Linked Lists CSC 172 SPRING 2002 LECTURE 3 Data Structures?  We make distinctions in the level of abstraction  Abstract Data Type (ADT) - functionality/behavior.
CCSB364 Data Structures & Algorithms Pointer & Linked List.
The Template Class Chain Chain Linear list. Each element is stored in a node. Nodes are linked together using pointers.
Data Structures and Applications Hao Jiang Computer Science Department Boston College.
Circular List Next field in the last node contains a pointer back to the first node rather than null pointer From any point in such a list it is possible.
Linked List C and Data Structures Baojian Hua
Introduction to C Programming CE Lecture 19 Linear Linked Lists.
פוינטרים ומבנים. אתחול והדפסת מערך #include int * readArray(int size){ int *a, i; a= (int *) malloc (size* sizeof(int)); if (!a) return NULL; for (i=0;i
Self Referential Structure. A structure may not contain a member of its own type. struct check { int item; struct check n; // Invalid };
Complex Structures Nested Structures Self referential structures A structure may have Data variables Internal structures/unions Pointer links Function.
Linked Lists list elements are stored, in memory, in an arbitrary order explicit information (called a link) is used to go from one element to the next.
Queues : Queue is a first in first out data structure. Queue concept is used in printers, Operating systems for process scheduling. Queue concept is used.
UNIT 1 Data Structures Using C Linked List By Rohit Khokher Department of Computer Science, Vidya College of Engineering, Meerut, India.
1 CSE 1342 Programming Concepts Lists. 2 Basic Terminology A list is a finite sequence of zero or more elements. –For example, (1,3,5,7) is a list of.
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.
+ struct Node { + int x; + Node * next; + }; + Node *mylist; + Node *mylist = NULL; head 121.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.
Lecture 6 Feb 8, 2012 Goals: Linked list (Chapter 3) list class in STL (section 3.3) implementing with linked lists.
Binary Trees In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left.
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,
1 Objectives of these slides: to describe linked lists in C 6 – Lists.
Algorithms and Data Structures
LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.
Rray Programs. Insert and element in the given position of the Array rray.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.
CIRCULAR L INKED L IST Insert as a first node Insert as a last node Delete first node Delete last node Insert after a node Insert before a node Search.
Data Structures AZHAR MAQSOOD NUST Institute of Information Technology (NIIT) Lecture 6: Linked Lists Linked List Basics.
LINEAR LINKED LISTS The disadvantages of arrays: 1.The size of the array is fixed. 2.Large size of array??? 3. Inserting and deleting elements. If the.
 Memory from the heap  Dynamic memory allocation using the new operator  Build a dynamic linked list  Another way of traversing a linked list 
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
Doubly Linked List Exercises Sometimes it is useful to have a linked list with pointers to both the next and previous nodes. This is called a doubly linked.
 Review building a complete linked list  List traversal in main ( )  List traversal using a function.
CPT: Lists/ Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to describe linked lists in C 15. Lists.
 Last Lecture Reminder  Array Sorts  Exam Questions Examples  Open Questions ◦ Search Engines ◦ Operating Systems ◦ Multiple Files in Projects  Additional.
الطابور QUEUE (abstract data type) واحد من هياكل البيانات الخطية الشائعة الاستخدام داخل البرامج. يحتوي علي عناصر من نفس النوع. من أنواع البيانات الخطية.
 Structures typedef struct student{ char name[30]; long id; int grades[8]; } Student_type, *pStudent_type;
CSE 1342 Programming Concepts
Linked List.
Doubly Linked List Review - We are writing this code
UNIT-3 LINKED LIST.
Linked list.
Queue data structure.
Stack and Queue APURBO DATTA.
Sequences 9/18/ :20 PM C201: Linked List.
Tree data structure.
Linked List Yumei Huo Department of Computer Science
Programmazione I a.a. 2017/2018.
CSC215 Homework Homework 11 Due date: Dec 19, 2016.
Recursion.
Tree data structure.
Tree data structure.
Lists List: finite sequence of data elements
Tree data structure.
Review & Lab assignments
Data structure types. Linear and Non-Linear Data Structures: Linked lists, stacks and queues.
LINKED LIST.
CS148 Introduction to Programming II
Presentation transcript:

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 = (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 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

#include 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"); } 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: 6,5, 2,4 print(); int n; printf("Enter a position\n"); scanf("%d",&n); delete1(n); print(); }

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? science/data-structures-doubly-linked-list-with- c-program-source-code

Study Reference Stack and Queue science/data-structures-stacks--with-c- program-source-code science/data-structures-queues--with-c- program-source-code