Module III. Linear Data Structures and their Linked Storage Representation

Size: px
Start display at page:

Download "Module III. Linear Data Structures and their Linked Storage Representation"

Transcription

1 Module III Linear Data Structures and their Linked Storage Representation 3.1 Linked List Linked list is the collection of inter connected nodes with a head node representing the first node and a tail node representing the last node of it. A node can be viewed as a block consisting of two major fields: a data field and a pointer field. The pointer field is used to interconnect nodes of a list. Below diagram depicts the linked list setup. Tail 3.2 Linked list types There are few variants of linked lists. Below diagram shows a brief classification of Linked Lists. Linked List NULL Singly LL Circular LL Doubly LL Singly Circular LL Doubly Circular LL Regarding these variants of LL, explanations and examples are given in subsequent sections of the notes. 3.2 Linked List Representation A simple representation of singly linked list would look like the below: struct node int data; struct node *next; ;

2 struct node *newnode; With the above declaration we can create a node of the pattern below: data *next newnode Initially, the pointer field of the first node in singly linked list would contain NULL value. Instruction that makes it is: newnode->next = NULL; Note: Structure variables fields of self referential type are accessed through -> (arrow operator) Now, let us try to understand few conventions w.r.t linked lists. Consider below given list h h h 40 NULL head (1000h) (1008h) (1016h) tail (1024h) In this list, head node contains data = 10 and tail node contains data = 40. This list has 4 nodes that are interconnected. Head node is connected to the next node since the pointer value contains the address of next node. Tail node is not linked to any node since its pointer value is NULL. struct node *head, *tail, *newnode; // variables declared of type struct node head = (struct node *) malloc (1 * sizeof(struct node)); This instruction creates one node of type struct. Memory gets allocated using DMA function malloc(). Created node in the above instruction is referred to as head. It can be any other name as well. head->data = 10; // assigns head node s data field with 10 head->next = NULL; //assigning pointer value to NULL 10 NULL head; tail tail = head; // assigning the reference of head to tail. This means both head and tail are referring to the same node. Now, let us add a new node to this list. newnode = (struct node *) malloc (1 * sizeof(struct node)); newnode->data = 20; newnode->next = NULL; 10 NULL 10 NULL Mr. Head; Nischaykumar tail Hegde, newnode CSE, VCET, Puttur

3 The above nodes can be connected using the following instructions: head->next = newnode; // assigning head node s pointer value with address of newnode h 20 NULL head; tail (1000h) newnode (1008h) Now, if we execute the instruction, tail = newnode; tail & newnode both refer to the node with value 20. In this way, linked lists can be created. 3.3 Operations on Linked Lists Major operations performed on linked lists are inserting a node, deleting a node, searching a node and traversing the list to display all of its nodes. a) Inserting a node: Node can be inserted at the beginning, at the end or anywhere in the linked list h h h 40 NULL head (1000h) (1008h) (1016h) tail (1024h) Consider the above list. Suppose we wish to insert a node at the beginning, following set of instruction are to be executed. newnode = (struct node *) malloc (1 * sizeof(struct node)); newnode->data = 50; newnode->next = head; h head = newnode; h h h 40 NULL (1000h) (1008h) (1016h) tail (1024h) h head; newnode Suppose we wish to insert a node at the end, following set of instruction are to be executed h h h h head (1000h) (1008h) (1016h) tail (1024h) newnode = (struct node *) malloc (1 * sizeof(struct node)); 80 NULL newnode->data = 80; newnode->next = NULL; 80 NULL (1032h)

4 Take a temporary variable of type struct node, traverse the list until you reach the last node (i.e tail ) while(temp->next!=null) temp=temp->next; temp->next = newnode; tail = newnode; Now let us see how to insert a node at a particular position in the list. For this, we need to ask user to specify after which node in the list he would want to insert. Call it as key node. This can be realized as below: temp h h h h head (1000h) (1008h) (1016h) tail (1024h) Key = h Newnode (1040h) 80 NULL (1032h) Take a temporary variable of type struct node, traverse the list until you reach the desired node (i.e 30) while(temp->data!=key) temp=temp->next; 100 NULL newnode (1040h) newnode->next = temp->next; temp->next = newnode; b) Deleting a node: A node in the list can be deleted using the standard function free ( ). Let us see an example to understand delete operations on linked list h h 30 NULL 40 NULL head (1000h) temp2 (1008h) temp(1016h) tail (1024h) Let us try to delete node with 30 data value. Take two temporary variables and position them to key node and the previous node of it.

5 Now, key =30. While(temp->data!=key) temp=temp->next; While(temp2->next!=key) temp2=temp2->next; temp2->next = temp->next; temp->next = NULL; free(temp); Below C program creates a singly linked list and performs few operations like insert, delete, display, search, insert and delete at specific positions in the list. #include <stdio.h> #include <stdlib.h> struct node int data; struct node *next; ; struct node *head, *tail,*temp, *temp2; void create(); void delet(); void dele_spec(); void display(); void spec_ins(); int search(int); int num,con=1,ch,key,f; void main()

6 clrscr(); head=null; printf("1.create\n2.delete\n3.delete Specific\n4.Insert After Specific node\n5.display\n6. Search\n7.Exit\n"); while(1) printf("enter choice\n"); scanf("%d",&ch); switch(ch) case 1: printf("enter number\n"); scanf("%d",&num); create(); case 2: delet(); case 3: dele_spec(); case 4: spec_ins(); case 5: display(); case 6: printf("enter the node data to be searched\n"); scanf("%d",&key); f=search(key); if(f==1) printf("node FOUND\n"); printf("node NOT FOUND\n"); case 7: exit(0);

7 void create() if(head==null) head = (struct node*)malloc(1*sizeof(struct node)); head->data=num; head->next=null; tail=head; temp= (struct node*)malloc(1*sizeof(struct node)); temp->data=num; tail->next=temp; temp->next=null; tail=temp; void delet() if(head==null) printf("list is empty\n"); temp=head; temp = temp->next; free(head); head=temp;

8 void dele_spec() int key; printf("enter the node to be deleted\n"); scanf("%d",&key); temp=head; temp2=head; while(temp->data!=key) temp=temp->next; while(temp2->next!=temp) temp2=temp2->next; if(temp->next==null) temp2->next=null; free(temp); tail=temp2; temp2->next=temp->next; free(temp); void spec_ins()

9 int key; printf("enter the node info next to which to be inserted\n"); scanf("%d",&key); printf("enter data for newnode\n"); scanf("%d",&num); temp2= (struct node*)malloc(1*sizeof(struct node)); temp2->data=num; temp=head; while(temp->data!=key) temp=temp->next; temp2->next=temp->next; temp->next=temp2; void display() if(head==null) printf("list empty\n"); temp=head; while(temp!=null) printf("%d\t",temp->data); temp=temp->next;

10 int search(int key) int flag=0; temp=head; while(temp!=null) if(temp->data==key) flag=1; return flag; temp=temp->next; return flag; Advantages of Linked Lists They are a dynamic in nature which allocates the memory when required. Insertion and deletion operations can be easily implemented. Stacks and queues can be easily executed. Linked List reduces the access time. Disadvantages of Linked Lists The memory is wasted as pointers require extra memory for storage. No element can be accessed randomly; it has to access each node sequentially. Reverse Traversing is difficult in linked list. Applications of Linked Lists Linked lists are used to implement stacks, queues, graphs, etc. Linked lists let you insert elements at the beginning and end of the list. In Linked Lists we don t need to know the size in advance. 3.4 Types of Linked Lists

11 Singly Linked List : Singly linked lists contain nodes which have a data part as well as an address part i.e. next, which points to the next node in sequence of nodes. The operations we can perform on singly linked lists are insertion, deletion and traversal. Doubly Linked List : In a doubly linked list, each node contains two links the first link points to the previous node and the next link points to the next node in the sequence. Circular Linked List : Singly Circular LL: In the circular linked list the last node of the list contains the address of the first node and forms a circular chain. Doubly Circular LL: In the doubly circular linked list the last node s next ptr field will have the address of start (head) node and the start (head) node s previous ptr field will have the address of last (tail) node. Below diagram shows its pictorial representation.

12 3.5 Header Linked List Head 3 Header Header linked list is similar to Singly linked List but the difference is that in this type of list, there exists a special node called header, which either contains information regarding total nodes in the list or the address of the last node in the whole list. C Program to implement header linked list is shown below (For execution purpose) #include <stdio.h> #include <conio.h> #include <stdlib.h> struct node int data; struct node *next; ; struct node *head, *header,*last,*temp, *newnode; void create(); void delet(); void display(); int num,count=0,ch; void main() clrscr(); head=null;

13 printf("1.create\n2.delete\n3.display\n4.exit\n"); while(1) printf("enter choice\n"); scanf("%d",&ch); switch(ch) case 1: if(head==null) header = (struct node*)malloc(1*sizeof(struct node)); header->data=count; printf("enter number\n"); scanf("%d",&num); create(); case 2: delet(); case 3: display(); case 4: exit(0); void create() if (head == NULL) head = (struct node*)malloc(1*sizeof(struct node)); head->data=num; head->next = NULL; header->next = head;

14 tail =head; count++; header->next = count; newnode = (struct node*)malloc(1*sizeof(struct node)); newnode->data=num; newnode->next=null; tail->next=newnode; tail=newnode; count++; header->data=count; void delet() if(head->data==0) printf("list is empty\n"); temp=head; while(temp->next!=last) temp=temp->next; free(tail); tail =temp; tail->next=null; count--;

15 header->data=count; void display() if(head==null) printf("list empty\n"); temp=head; while(temp!=null) printf("%d\t",temp->data); temp=temp->ptr; printf("\nheader Information:%d nodes\n",header->data); C Program to implement header linked list is shown below (For Exam purpose) #include <stdio.h> #include <stdlib.h> struct node int data; struct node *next; ; struct node *head,*header,*tail,*temp,*newnode; void create(int); void delet(); void display(); int num,count=0,ch;

16 void main() clrscr(); header=(struct node *)malloc(1*sizeof(struct node)); header->data=count; header->next=null; create(10); create(20); create(30); delet(); display(); void create(int num) if(head==null) head=(struct node*)malloc(1*sizeof(struct node)); head->data=num; head->next=null; tail=head; count++; newnode = (struct node*)malloc(1*sizeof(struct node)); newnode->data=num; newnode->next=null; tail->next=newnode; tail=newnode; count++; header->data=count;

17 void delet() if(header==null) printf("list is empty\n"); temp=head; while(temp->next!=tail) temp=temp->next; free(tail); tail =temp; tail->next=null; count--; header->data=count; void display() if(head==null) printf("list empty\n");

18 temp=head; while(temp!=null) printf("%d\t",temp->data); temp=temp->next; printf("\nheader Information:%d\n",header->data); 3.5 Linked implementation of Stack #include <stdio.h> #include <stdlib.h> struct Node int Data; struct Node *next; *top; void popstack() struct Node *temp, *var=top; if(var==top) top = top->next; free(var); printf("\nstack Empty"); void push(int value) struct Node *temp;

19 temp=(struct Node *)malloc(sizeof(struct Node)); temp->data=value; if (top == NULL) top=temp; top->next=null; temp->next=top; top=temp; void display() struct Node *var=top; if(var!=null) printf("\nelements are as:\n"); while(var!=null) printf("\t%d\n",var->data); var=var->next; printf("\n"); printf("\nstack is Empty"); int main(int argc, char *argv[])

20 int i=0; top=null; printf(" \n1. Push to stack"); printf(" \n2. Pop from Stack"); printf(" \n3. Display data of Stack"); printf(" \n4. Exit\n"); while(1) printf(" \nchoose Option: "); scanf("%d",&i); switch(i) case 1: int value; printf("\nenter a valueber to push into Stack: "); scanf("%d",&value); push(value); display(); case 2: popstack(); display(); case 3: display();

21 case 4: struct Node *temp; while(top!=null) temp = top->next; free(top); top=temp; exit(0); default: printf("\nwrong choice for operation"); 3.6 Merging of two lists #include<stdio.h> #include<stdlib.h> struct node int data; struct node *next; ; void insert_list1(); void insert_list2(); void display(); void merge_list();

22 struct node *list1, *list2,*list3,*temp1,*temp2,*temp3,*temp4; void main() int ch; clrscr(); printf("1 for insert in first linklist\n"); printf("2 for insert in second linklist\n"); printf("3 for display first & second linklist\n"); printf("4 for merge linklist\n"); printf("5 for exit\n"); while(1) printf("\nenter ur choice:"); scanf("%d",&ch); switch(ch) case 1: insert_list1(); case 2: insert_list2(); case 3: display(); case 4: merge_list(); case 5: exit(0); void merge_list() temp1=list1;

23 temp2=list2; while(temp1!=null temp2!=null) temp3=(struct node*)malloc(1*sizeof(struct node)); if(list3==null) list3=temp3; temp4=temp3; if(temp1->data<temp2->data) temp3->data=temp1->data; temp4->next=temp3; temp4=temp3; temp1=temp1->next; temp3->data=temp2->data; temp4->next=temp3; temp4=temp3; temp2=temp2->next; if(temp2==null) while(temp1!=null) temp3=(struct node*)malloc(1*sizeof(struct node)); temp3->data=temp1->data; temp4->next=temp3; temp4=temp3;

24 temp1=temp1->next; if(temp1==null) while(temp2!=null) temp3=(struct node*)malloc(1*sizeof(struct node)); temp3->data=temp2->data; temp4->next=temp3; temp4=temp3; temp2=temp2->next; temp3->next=null; void insert_list1() struct node *new; if(list1==null) list1=(struct node *)malloc(sizeof(struct node)); printf("enter the info"); scanf("%d",&list1->data); list1->next=null; temp1=list1;

25 new=(struct node *)malloc(sizeof(struct node)); printf("enter the info"); scanf("%d",&new->data); new->next=null; temp1->next=new; temp1=temp1->next; void insert_list2() struct node *new; if(list2==null) list2=(struct node *)malloc(sizeof(struct node)); printf("enter the info"); scanf("%d",&list2->data); list2->next=null; temp2=list2; new=(struct node *)malloc(sizeof(struct node)); printf("enter the info"); scanf("%d",&new->data); new->next=null; temp2->next=new; temp2=temp2->next;

26 void display() temp1=list1; temp2=list2; printf("\nthe Information of First linklist:"); while(temp1!=null) printf("%d ",temp1->data); temp1=temp1->next; printf("\nthe Information of Second linklist:"); while(temp2!=null) printf("%d ",temp2->data); temp2=temp2->next; temp3=list3; printf("\nmerged List:"); while(temp3!=null) printf("%d ",temp3->data); temp3=temp3->next;

Module III. Linear Data Structures and their Linked Storage Representation

Module III. Linear Data Structures and their Linked Storage Representation Module III Linear Data Structures and their Linked Storage Representation 3.1 Linked List Linked list is the collection of inter connected nodes with a head node representing the first node and a tail

More information

Data Structures and Applications (17CS33)

Data Structures and Applications (17CS33) 3.7 Reversing a Singly Linked List #include #include struct node int data; struct node *next; ; void insert_list(int); void display(); void revers(); struct node *head,*newnode,*tail,*temp,*temp1,*temp2;

More information

Linked List in Data Structure. By Prof. B J Gorad, BECSE, M.Tech CST, PHD(CSE)* Assistant Professor, CSE, SITCOE, Ichalkaranji,Kolhapur, Maharashtra

Linked List in Data Structure. By Prof. B J Gorad, BECSE, M.Tech CST, PHD(CSE)* Assistant Professor, CSE, SITCOE, Ichalkaranji,Kolhapur, Maharashtra Linked List in Data Structure By Prof. B J Gorad, BECSE, M.Tech CST, PHD(CSE)* Assistant Professor, CSE, SITCOE, Ichalkaranji,Kolhapur, Maharashtra Linked List Like arrays, Linked List is a linear data

More information

Solution for Data Structure

Solution for Data Structure Solution for Data Structure May 2016 INDEX Q1 a 2-3 b 4 c. 4-6 d 7 Q2- a 8-12 b 12-14 Q3 a 15-18 b 18-22 Q4- a 22-35 B..N.A Q5 a 36-38 b N.A Q6- a 39-42 b 43 1 www.brainheaters.in Q1) Ans: (a) Define ADT

More information

AIM:- To write a C program to create a singly linked list implementation.

AIM:- To write a C program to create a singly linked list implementation. SINGLY LINKED LIST AIM:- To write a C program to create a singly linked list implementation. ALGORITHM:- 1. Start the program. 2. Get the choice from the user. 3. If the choice is to add records, get the

More information

Linked Lists and other Dynamic Data Structures

Linked Lists and other Dynamic Data Structures Linked Lists and other Dynamic Data Structures Arrays Fixed in size Allocated in advance within a contiguous memory block Look-up is fast Resizing and Deleting is hard (reallocate and copy) Dynamic Data

More information

Computer Systems and Networks

Computer Systems and Networks University of the Pacific LECTURE 5: C PROGRAMMING Computer Systems and Networks Dr. Pallipuram (vpallipuramkrishnamani@pacific.edu) Today s Class o Pointer basics o Pointers and mul;- dimensional arrays

More information

UNIT 4: Linked List Programs demonstrated in class. Tojo Mathew Asst. Professor CSE Dept., NIE Mysuru.

UNIT 4: Linked List Programs demonstrated in class. Tojo Mathew Asst. Professor CSE Dept., NIE Mysuru. UNIT 4: Linked List Programs demonstrated in class. Tojo Mathew Asst. Professor CSE Dept., NIE Mysuru. 1 Table of Contents 1. Array Implementation of Linked List...3 2. Queue using Array Implementation

More information

Data accessing is faster because we just need to specify the array name and the index of the element to be accssed.. ie arrays are simple to use

Data accessing is faster because we just need to specify the array name and the index of the element to be accssed.. ie arrays are simple to use Module 3 Linear data structures and linked storage representation Advantages of arrays Linear data structures such as stacks and queues can be represented and implemented using sequential allocation ie

More information

POLYNOMIAL ADDITION. AIM:- To write a C program to represent a polynomial as a linked list and write functions for polynomial addition.

POLYNOMIAL ADDITION. AIM:- To write a C program to represent a polynomial as a linked list and write functions for polynomial addition. POLYNOMIAL ADDITION AIM:- To write a C program to represent a polynomial as a linked list and write functions for polynomial addition. ALGORITHM:- 1. Start the program 2. Get the coefficients and powers

More information

PESIT Bangalore South Campus

PESIT Bangalore South Campus TEST - 2 Date : 29-9-2014 Marks : 50 Subject & Code : DS(10CS35) Class : III CSE &ISE Name of faculty : Dr.JSP/Mrs.Vandana/Mrs.Renuka Time : 11.30AM to 1 PM Note: Answer any 5 Questions 1 Write a C program

More information

STACKS 3.1 INTRODUCTION 3.2 DEFINITION

STACKS 3.1 INTRODUCTION 3.2 DEFINITION STACKS 3 3.1 INTRODUCTION A stack is a linear data structure. It is very useful in many applications of computer science. It is a list in which all insertions and deletions are made at one end, called

More information

ESc101: (Linear, Circular, Doubly) Linked Lists, Stacks, Queues, Trees. Introduction to Linked Lists

ESc101: (Linear, Circular, Doubly) Linked Lists, Stacks, Queues, Trees. Introduction to Linked Lists ESc101: (Linear, Circular, Doubly) Linked Lists, Stacks, Queues, Trees Instructor: Krithika Venkataramani Semester 2, 2011-2012 1 Introduction to Linked Lists Each bead connected to the next through a

More information

COP Programming Concepts Spring 1999 CLOSED BOOK Exam #1 100 Points NAME

COP Programming Concepts Spring 1999 CLOSED BOOK Exam #1 100 Points NAME CLOSED BOOK Exam #1 100 Points NAME 1. The following program has (at least) 10 syntax errors. Circle each error. Write the corrected program in the blank space below. 2 points for each error you find.

More information

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Computer Science and Engineering

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Computer Science and Engineering USN 1 P E PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Computer Science and Engineering SOLUTION FOR INTERNAL ASSESSMENT TEST 2 Date : 6/10/2017 Marks:

More information

Application of Stack (Backtracking)

Application of Stack (Backtracking) Application of Stack (Backtracking) Think of a labyrinth or maze How do you find a way from an entrance to an exit? Once you reach a dead end, you must backtrack. But backtrack to where? to the previous

More information

Single linked list. Program: #include<stdio.h> #include<conio.h> #include<alloc.h> struct node. int info; struct node *next; void main()

Single linked list. Program: #include<stdio.h> #include<conio.h> #include<alloc.h> struct node. int info; struct node *next; void main() Single linked list Program: #include #include #include struct node int info; struct node *next; ; void main() struct node *s,*start,*prev,*new1,*temp,*temp1,*ptemp; int cho,i,j,x,n,p;

More information

DS Lab Manual -17CS33

DS Lab Manual -17CS33 Chethan Raj C Assistant Professor Dept. of CSE 6. Design, Develop and Implement a menu driven Program in C for the following operations on Circular QUEUE of Characters (Array Implementation of Queue with

More information

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified)

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified) WINTER 18 EXAMINATION Subject Name: Data Structure using C Model wer Subject Code: 22317 Important Instructions to examiners: 1) The answers should be examined by key words and not as word-to-word as given

More information

Vivekananda College of Engineering & Technology. Laboratory Manual

Vivekananda College of Engineering & Technology. Laboratory Manual Vivekananda College of Engineering & Technology [Sponsored by Vivekananda Vidyavardhaka Sangha, Puttur ] Affiliated to Visvesvaraya Technological University Approved by AICTE New Delhi & Govt of Karnataka

More information

Computer Systems and Networks

Computer Systems and Networks LECTURE 6: C PROGRAMMING Computer Systems and Networks Dr. Pallipuram (vpallipuramkrishnamani@pacific.edu) University of the Pacific Today s Class o Recap from last class o File I/O o What happens during

More information

Now consider the following situation after deleting three elements from the queue...

Now consider the following situation after deleting three elements from the queue... Scheme of valuvation -1I Subject & Code : Data Structure and Application (15CS33) NOTE: ANSWER All FIVE QUESTIONS 1 Explain the diadvantage of linear queue and how it i olved in circular queue. Explain

More information

Ashish Gupta, Data JUET, Guna

Ashish Gupta, Data JUET, Guna Categories of data structures Data structures are categories in two classes 1. Linear data structure: - organized into sequential fashion - elements are attached one after another - easy to implement because

More information

BITS PILANI, DUBAI CAMPUS DUBAI INTERNATIONAL ACADEMIC CITY, DUBAI FIRST SEMESTER

BITS PILANI, DUBAI CAMPUS DUBAI INTERNATIONAL ACADEMIC CITY, DUBAI FIRST SEMESTER BITS PILANI, DUBAI CAMPUS DUBAI INTERNATIONAL ACADEMIC CITY, DUBAI FIRST SEMESTER 2017-2018 COURSE : COMPUTER PROGRAMMING (CS F111) COMPONENT : Tutorial# 7 (SOLUTIONS) DATE : 06-DEC-2017 Answer 1 enum

More information

MODULE V: POINTERS & PREPROCESSORS

MODULE V: POINTERS & PREPROCESSORS MODULE V: POINTERS & PREPROCESSORS INTRODUCTION As you know, every variable is a memory-location and every memory-location has its address defined which can be accessed using ampersand(&) operator, which

More information

Darshan Institute of Engineering & Technology for Diploma studies Unit 4

Darshan Institute of Engineering & Technology for Diploma studies Unit 4 Pointer A pointer is a variable that contains address or location of another variable. Pointer is a derived data type in C. Pointers contain memory address as their values, so they can also be used to

More information

Actually, C provides another type of variable which allows us to do just that. These are called dynamic variables.

Actually, C provides another type of variable which allows us to do just that. These are called dynamic variables. When a program is run, memory space is immediately reserved for the variables defined in the program. This memory space is kept by the variables until the program terminates. These variables are called

More information

Linked-List Basic Examples. A linked-list is Linear collection of self-referential class objects, called nodes Connected by pointer links

Linked-List Basic Examples. A linked-list is Linear collection of self-referential class objects, called nodes Connected by pointer links Linked-List Basic Examples A linked-list is Linear collection of self-referential class objects, called nodes Connected by pointer links Accessed via a pointer to the first node of the list Subsequent

More information

DATA STRUCTURE. o DYNAMIC MEMORY. ALLOCATION o SELF-REFERENTIAL STRUCTURE o TYPEDEF LINK LIST STACK QUEUE TREE

DATA STRUCTURE. o DYNAMIC MEMORY. ALLOCATION o SELF-REFERENTIAL STRUCTURE o TYPEDEF LINK LIST STACK QUEUE TREE DATA STRUCTURE TOPICS PAGE o DYNAMIC MEMORY 2-5 ALLOCATION o SELF-REFERENTIAL STRUCTURE o TYPEDEF LINK LIST 5-16 STACK 17-28 o APPLICATION OF STACK 28-38 QUEUE 39-43 TREE o BINARY SEARCH TREE o HEAP GRAPH

More information

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified) WINTER 16 EXAMINATION Model Answer Subject Code:

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified) WINTER 16 EXAMINATION Model Answer Subject Code: Important Instructions to examiners: 1) The answers should be examined by key words and not as word-to-word as given in the model answer scheme. 2) The model answer and the answer written by candidate

More information

That means circular linked list is similar to the single linked list except that the last node points to the first node in the list.

That means circular linked list is similar to the single linked list except that the last node points to the first node in the list. Leaning Objective: In this Module you will be learning the following: Circular Linked Lists and it operations Introduction: Circular linked list is a sequence of elements in which every element has link

More information

DC104 DATA STRUCTURE JUNE Q.2 a. If you are using C language to implement the heterogeneous linked list, what pointer type will you use?

DC104 DATA STRUCTURE JUNE Q.2 a. If you are using C language to implement the heterogeneous linked list, what pointer type will you use? Q.2 a. If you are using C language to implement the heterogeneous linked list, what pointer type will you use? The heterogeneous linked list contains different data types in its nodes and we need a link

More information

Vivekananda College of Engineering & Technology. Laboratory Manual

Vivekananda College of Engineering & Technology. Laboratory Manual Vivekananda College of Engineering & Technology [Sponsored by Vivekananda Vidyavardhaka Sangha, Puttur ] Affiliated to Visvesvaraya Technological University Approved by AICTE New Delhi & Govt of Karnataka

More information

The combination of pointers, structs, and dynamic memory allocation allow for creation of data structures

The combination of pointers, structs, and dynamic memory allocation allow for creation of data structures Data Structures in C C Programming and Software Tools N.C. State Department of Computer Science Data Structures in C The combination of pointers, structs, and dynamic memory allocation allow for creation

More information

Information Technology Association Department of Information Technology MIT Campus, Anna University Chennai

Information Technology Association Department of Information Technology MIT Campus, Anna University Chennai Algorithm Puzzles - Week1 Solutions 1. Swap two integer variables without using temporary variable. Give at least two different solutions. The first solution is a=a+b; b=a-b; a=a-b; Hope most of you are

More information

CP2 Revision. theme: dynamic datatypes & data structures

CP2 Revision. theme: dynamic datatypes & data structures CP2 Revision theme: dynamic datatypes & data structures structs can hold any combination of datatypes handled as single entity struct { }; ;

More information

DEV BHOOMI INSTITUTE OF TECHNOLOGY

DEV BHOOMI INSTITUTE OF TECHNOLOGY DEV BHOOMI INSTITUTE OF TECHNOLOGY Department of Computer Science and Engineering Year: 2nd Semester: 3rd Data Structures- PCS-303 LAB MANUAL Prepared By: HOD(CSE) DEV BHOOMI

More information

C PROGRAMMING Lecture 5. 1st semester

C PROGRAMMING Lecture 5. 1st semester C PROGRAMMING Lecture 5 1st semester 2017-2018 Program Address Space The Stack The stack is the place where all local variables are stored a local variable is declared in some scope Example int x; //creates

More information

Fundamentals of Programming & Procedural Programming

Fundamentals of Programming & Procedural Programming Universität Duisburg-Essen PRACTICAL TRAINING TO THE LECTURE Fundamentals of Programming & Procedural Programming Session Eight: Math Functions, Linked Lists, and Binary Trees Name: First Name: Tutor:

More information

Linked Lists. .. and other linked structures. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR

Linked Lists. .. and other linked structures. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR 1 Linked Lists.. and other linked structures Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Dynamic memory allocation: review typedef struct { int hitemp;

More information

The time and space are the two measure for efficiency of an algorithm.

The time and space are the two measure for efficiency of an algorithm. There are basically six operations: 5. Sorting: Arranging the elements of list in an order (either ascending or descending). 6. Merging: combining the two list into one list. Algorithm: The time and space

More information

19-Nov CSCI 2132 Software Development Lecture 29: Linked Lists. Faculty of Computer Science, Dalhousie University Heap (Free Store)

19-Nov CSCI 2132 Software Development Lecture 29: Linked Lists. Faculty of Computer Science, Dalhousie University Heap (Free Store) Lecture 29 p.1 Faculty of Computer Science, Dalhousie University CSCI 2132 Software Development Lecture 29: Linked Lists 19-Nov-2018 Location: Chemistry 125 Time: 12:35 13:25 Instructor: Vlado Keselj Previous

More information

Guide for The C Programming Language Chapter 5

Guide for The C Programming Language Chapter 5 1. Differentiate between primitive data type and non-primitive data type. Primitive data types are the basic data types. These data types are used to represent single values. For example: Character, Integer,

More information

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0)

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0) CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0) The queue ADT A queue is like a "natural" queue of elements. It is an ordered list in which all insertions occur at one end called

More information

CS61, Fall 2012 Section 2 Notes

CS61, Fall 2012 Section 2 Notes CS61, Fall 2012 Section 2 Notes (Week of 9/24-9/28) 0. Get source code for section [optional] 1: Variable Duration 2: Memory Errors Common Errors with memory and pointers Valgrind + GDB Common Memory Errors

More information

This document can be downloaded from with most recent updates. 1 Data Structures using C: Module 4 (16MCA11) LINKED LISTS

This document can be downloaded from   with most recent updates. 1 Data Structures using C: Module 4 (16MCA11) LINKED LISTS 1 Data Structures using C: Module 4 (16MCA11) LINKED LISTS 4.1 MEMORY MANAGEMENT 4.1.1 Basics about Memory When a C program is compiled, the compiler translates the source code into machine code. Now the

More information

Linked List. April 2, 2007 Programming and Data Structure 1

Linked List. April 2, 2007 Programming and Data Structure 1 Linked List April 2, 2007 Programming and Data Structure 1 Introduction head A linked list is a data structure which can change during execution. Successive elements are connected by pointers. Last element

More information

Dynamic Memory Allocation

Dynamic Memory Allocation Dynamic Memory Allocation The process of allocating memory at run time is known as dynamic memory allocation. C does not Inherently have this facility, there are four library routines known as memory management

More information

MC9217 / Programming and Data Structures Lab MC9217 PROGRAMMING AND DATA STRUCTURES LAB L T P C

MC9217 / Programming and Data Structures Lab MC9217 PROGRAMMING AND DATA STRUCTURES LAB L T P C MC9217 PROGRAMMING AND DATA STRUCTURES LAB L T P C 0 0 3 2 1.Create a Stack and do the following operations using arrays and linked lists (i)push (ii) Pop (iii) Peep 2.Create a Queue and do the following

More information

Programming. Lists, Stacks, Queues

Programming. Lists, Stacks, Queues Programming Lists, Stacks, Queues Summary Linked lists Create and insert elements Iterate over all elements of the list Remove elements Doubly Linked Lists Circular Linked Lists Stacks Operations and implementation

More information

Model Solution for QP CODE : ( 3 Hours )

Model Solution for QP CODE : ( 3 Hours ) Model Solution for QP CODE : 24788 ( 3 Hours ) All answers are for reference only. Any alternate answer that solves the problem should be considered eqully valid. 1. (a) Define data structure? Give its

More information

Chapter 8 STRUCTURES IN C

Chapter 8 STRUCTURES IN C Chapter 8 STRUCTURES IN C 1 Structures Introduction Collections of related variables (aggregates) under one name Can contain variables of different data types Commonly used to define records to be stored

More information

Procedural Programming

Procedural Programming Exercise 6 (SS 2016) 04.07.2015 What will I learn in the 6. exercise Math functions Dynamic data structures (linked Lists) Exercise(s) 1 Home exercise 4 (3 points) Write a program which is able to handle

More information

SINGLE LINKED LIST. Algorithm for allocating memory for the new node

SINGLE LINKED LIST. Algorithm for allocating memory for the new node SRI CHANDRASEKHARENDRA SARASWATHI VISWA MAHAVIDYALAYA SCSVMV UNIVERSITY DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING Faculty: S.Gokulakrishnan AP/CSE SINGLE LINKED LIST Aim:- Write a C program to implement

More information

BBM 201 DATA STRUCTURES

BBM 201 DATA STRUCTURES BBM 201 DATA STRUCTURES Lecture 10: Implementation of Linked Lists (Linked stacks and queues, Circular linked lists) 2015-2016 Fall Linked list implementation of stacks The cost of insert and delete at

More information

Frequently asked Data Structures Interview Questions

Frequently asked Data Structures Interview Questions Frequently asked Data Structures Interview Questions Queues Data Structure Interview Questions How is queue different from a stack? The difference between stacks and queues is in removing. In a stack we

More information

Spring 2008 Data Structures (CS301) LAB

Spring 2008 Data Structures (CS301) LAB Spring 2008 Data Structures (CS301) LAB Objectives The objectives of this LAB are, o Enabling students to implement Singly Linked List practically using c++ and adding more functionality in it. o Enabling

More information

Stack & Queue on Self-Referencing Structures

Stack & Queue on Self-Referencing Structures C Programming 1 Stack & Queue on Self-Referencing Structures C Programming 2 Representation of Stack struct stack { int data ; struct stack *next ; ; typedef struct stacknode node, *stack ; C Programming

More information

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified)

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified) WINTER 17 EXAMINATION Subject Name: Data Structure Using C Model Answer Subject Code: 17330 Important Instructions to examiners: 1) The answers should be examined by key words and not as word-to-word as

More information

MODULE 5: Pointers, Preprocessor Directives and Data Structures

MODULE 5: Pointers, Preprocessor Directives and Data Structures MODULE 5: Pointers, Preprocessor Directives and Data Structures 1. What is pointer? Explain with an example program. Solution: Pointer is a variable which contains the address of another variable. Two

More information

UNIT-2 Stack & Queue

UNIT-2 Stack & Queue UNIT-2 Stack & Queue 59 13. Stack A stack is an Abstract Data Type (ADT), commonly used in most programming languages. It is named stack as it behaves like a real-world stack, for example a deck of cards

More information

Outline. Briefly review the last class Pointers and Structs Memory allocation Linked lists

Outline. Briefly review the last class Pointers and Structs Memory allocation Linked lists Outline Briefly review the last class Pointers and Structs Memory allocation Linked lists C Structures and Memory Allocation A struct is a data structure that comprises multiple types, each known as a

More information

Assignment 6. Q1. Create a database of students using structures, where in each entry of the database will have the following fields:

Assignment 6. Q1. Create a database of students using structures, where in each entry of the database will have the following fields: Assignment 6 Q1. Create a database of students using structures, where in each entry of the database will have the following fields: 1. a name, which is a string with at most 128 characters 2. their marks

More information

INSERT AS A FIRST NODE

INSERT AS A FIRST NODE CIRCULAR LINKED LIST 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 Traverse INSERT AS A FIRST NODE void insertf() struct

More information

What is recursion. WAP to find sum of n natural numbers using recursion (5)

What is recursion. WAP to find sum of n natural numbers using recursion (5) DEC 2014 Q1 a What is recursion. WAP to find sum of n natural numbers using recursion (5) Recursion is a phenomenon in which a function calls itself. A function which calls itself is called recursive function.

More information

DC54 DATA STRUCTURES JUNE 2013

DC54 DATA STRUCTURES JUNE 2013 Q 2 (a) Define storage class and its functions. Explain in detail scope, storage allocation and purpose of each storage class. 'Storage' refers to the scope of a variable and memory allocated by compiler

More information

Variation of Pointers

Variation of Pointers Variation of Pointers A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like any variable or constant, you must declare a pointer before

More information

Self-referential Structures and Linked List. Programming and Data Structure 1

Self-referential Structures and Linked List. Programming and Data Structure 1 Self-referential Structures and Linked List Programming and Data Structure 1 Linked List :: Basic Concepts A list refers to a set of items organized sequentially. An array is an example of a list. The

More information

Stack & Queue on Self-Referencing Structures

Stack & Queue on Self-Referencing Structures PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 1 Stack & Queue on Self-Referencing Structures PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 2 Representation of Stack struct stack { int data ; struct

More information

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of MCA

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of MCA USN 1 P E PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of MCA INTERNAL ASSESSMENT TEST 2 (Scheme and Solution) Data Structures Using C (16MCA11) 1) A.

More information

Linked List Practice Questions

Linked List Practice Questions Linked List Practice Questions 1. The following function reverse() is supposed to reverse a singly linked list. There is one line missing at the end of the function. /* head_ref is a double pointer which

More information

Pointers. Array. Solution to the data movement in sequential representation

Pointers. Array. Solution to the data movement in sequential representation 1 LISTS Pointers Array sequential representation some operation can be very time-consuming (data movement) size of data must be predefined static storage allocation and deallocation Solution to the data

More information

CA341 - Comparative Programming Languages

CA341 - Comparative Programming Languages CA341 - Comparative Programming Languages David Sinclair Dynamic Data Structures Generally we do not know how much data a program will have to process. There are 2 ways to handle this: Create a fixed data

More information

Singly linked lists in C.

Singly linked lists in C. Singly linked lists in C http://www.cprogramming.com/tutorial/c/lesson15.html By Alex Allain Linked lists are a way to store data with structures so that the programmer can automatically create a new place

More information

Chapter 19: Program Design. Chapter 19. Program Design. Copyright 2008 W. W. Norton & Company. All rights reserved.

Chapter 19: Program Design. Chapter 19. Program Design. Copyright 2008 W. W. Norton & Company. All rights reserved. Chapter 19 Program Design 1 Introduction Most full-featured programs are at least 100,000 lines long. Although C wasn t designed for writing large programs, many large programs have been written in C.

More information

CSE2301. Dynamic memory Allocation. malloc() Dynamic Memory Allocation and Structs

CSE2301. Dynamic memory Allocation. malloc() Dynamic Memory Allocation and Structs Warning: These notes are not complete, it is a Skelton that will be modified/add-to in the class. If you want to us them for studying, either attend the class or get the completed notes from someone who

More information

CT11 (ALCCS) DATA STRUCTURE THROUGH C JUN 2015

CT11 (ALCCS) DATA STRUCTURE THROUGH C JUN 2015 Solutions Q.1 a. What is a pointer? Explain how it is declared and initialized. (4) Different from other normal variables which can store values, pointers are special variables that can hold the address

More information

/****************************************** Application: Linked List Example Compiled on: Borland Turbo C++ 3.0

/****************************************** Application: Linked List Example Compiled on: Borland Turbo C++ 3.0 /****************************************** Application: Linked List Example Compiled on: Borland Turbo C++ 3.0 ******************************************/ #include #include /* Structure

More information

INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Stamp / Signature of the Invigilator

INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Stamp / Signature of the Invigilator INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Stamp / Signature of the Invigilator EXAMINATION ( End Semester ) SEMESTER ( Spring ) Roll Number Section Name Subject Number C S 1 0 0 0 1 Subject Name Programming

More information

Dynamic Data Structures

Dynamic Data Structures Dynamic Data Structures We have seen that the STL containers vector, deque, list, set and map can grow and shrink dynamically. We now examine how some of these containers can be implemented in C++. To

More information

Write a program that creates in the main function two linked lists of characters and fills them with the following values:

Write a program that creates in the main function two linked lists of characters and fills them with the following values: Write a program that creates in the main function two linked lists of characters and fills them with the following values: The first list will have 3 nodes with the following characters: A,B, and C. The

More information

Cpt S 122 Data Structures. Data Structures

Cpt S 122 Data Structures. Data Structures Cpt S 122 Data Structures Data Structures Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Topics Introduction Self Referential Structures Dynamic Memory Allocation

More information

LINKED LIST IMPLEMENTATION USING C LANGUAGE: A REVIEW

LINKED LIST IMPLEMENTATION USING C LANGUAGE: A REVIEW LINKED LIST IMPLEMENTATION USING C LANGUAGE: A REVIEW Ekta Nehra Assistant Professor (Extn.), C.R.M jat college, Hisar, Haryana, (India) ABSTRACT This paper describes about linear data structure i.e. linked

More information

Data Structure Series

Data Structure Series Data Structure Series This series is actually something I started back when I was part of the Sweet.Oblivion staff, but then some things happened and I was no longer able to complete it. So now, after

More information

A. Year / Module Semester Subject Topic 2016 / V 2 PCD Pointers, Preprocessors, DS

A. Year / Module Semester Subject Topic 2016 / V 2 PCD Pointers, Preprocessors, DS Syllabus: Pointers and Preprocessors: Pointers and address, pointers and functions (call by reference) arguments, pointers and arrays, address arithmetic, character pointer and functions, pointers to pointer,initialization

More information

Subject: Fundamental of Computer Programming 2068

Subject: Fundamental of Computer Programming 2068 Subject: Fundamental of Computer Programming 2068 1 Write an algorithm and flowchart to determine whether a given integer is odd or even and explain it. Algorithm Step 1: Start Step 2: Read a Step 3: Find

More information

ECE 264 Exam 2. 6:30-7:30PM, March 9, You must sign here. Otherwise you will receive a 1-point penalty.

ECE 264 Exam 2. 6:30-7:30PM, March 9, You must sign here. Otherwise you will receive a 1-point penalty. ECE 264 Exam 2 6:30-7:30PM, March 9, 2011 I certify that I will not receive nor provide aid to any other student for this exam. Signature: You must sign here. Otherwise you will receive a 1-point penalty.

More information

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-0-0)

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-0-0) CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-0-0) Teacher: Sourangshu Bha@acharya sourangshu@gmail.com h@p://cse.iitkgp.ac.in/~sourangshu/ Department of Computer Science and Engineering

More information

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION Important Instructions to examiners: 1) The answers should be examined by key words and not as word-to-word as given in themodel answer scheme. 2) The model answer and the answer written by candidate may

More information

Lists (Section 5) Lists, linked lists Implementation of lists in C Other list structures List implementation of stacks, queues, priority queues

Lists (Section 5) Lists, linked lists Implementation of lists in C Other list structures List implementation of stacks, queues, priority queues (Section 5) Lists, linked lists Implementation of lists in C Other list structures List implementation of stacks, queues, priority queues By: Pramod Parajuli, Department of Computer Science, St. Xavier

More information

Class Information ANNOUCEMENTS

Class Information ANNOUCEMENTS Class Information ANNOUCEMENTS Third homework due TODAY at 11:59pm. Extension? First project has been posted, due Monday October 23, 11:59pm. Midterm exam: Friday, October 27, in class. Don t forget to

More information

Chapter 4. LISTS. 1. Pointers

Chapter 4. LISTS. 1. Pointers Chap 4: LIST (Page 1) Chapter 4. LISTS TABLE OF CONTENTS 1. POINTERS 2. SINGLY LINKED LISTS 3. DYNAMICALLY LINKED STACKS AND QUEUES 4. POLYNOMIALS 5. ADDITIONAL LIST OPERATIONS 6. EQUIVALENCE RELATIONS

More information

Procedural programming with C

Procedural programming with C Procedural programming with C Dr. C. Constantinides Department of Computer Science and Software Engineering Concordia University Montreal, Canada August 11, 2016 1 / 77 Functions Similarly to its mathematical

More information

MODULE 3: LINKED LIST

MODULE 3: LINKED LIST MODULE 3: LINKED LIST DEFINITION A linked list, or one-way list, is a linear collection of data elements, called nodes, where the linear order is given by means of pointers. That is, each node is divided

More information

4) In C, if you pass an array as an argument to a function, what actually gets passed?

4) In C, if you pass an array as an argument to a function, what actually gets passed? Paytm Programming Sample paper: 1) What will be the output of the program? #include int main() } int y=128; const int x=y; printf("%d\n", x); return 0; a. 128 b. Garbage value c. Error d. 0 2)

More information

Introduction to Data Structures. Systems Programming

Introduction to Data Structures. Systems Programming Introduction to Data Structures Systems Programming Intro to Data Structures Self-referential Structures Dynamic Memory Allocation A Simple malloc Example Linear Lists Linked Lists Insertion Example Linked

More information

Lecture 7: Data Structures. EE3490E: Programming S1 2017/2018 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology

Lecture 7: Data Structures. EE3490E: Programming S1 2017/2018 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology Lecture 7: Data Structures 1 Introduction: dynamic array Conventional array in C has fix number of elements Dynamic array is array with variable number of elements: actually a pointer and a variable indicating

More information

ENEE150 Final Exam Review

ENEE150 Final Exam Review ENEE150 Final Exam Review Topics: Pointers -- pointer definitions & initialization -- call-by-reference and Call-by-value -- pointer with arrays -- NULL pointer, void pointer, and pointer to pointer Strings

More information

Elementary Data Structures: Part 1: Arrays, Lists. CSE 2320 Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington

Elementary Data Structures: Part 1: Arrays, Lists. CSE 2320 Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington Elementary Data Structures: Part 1: Arrays, Lists CSE 2320 Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1 Basic Types Types like integers, real numbers, characters.

More information

! A data structure representing a list. ! A series of dynamically allocated nodes. ! A separate pointer (the head) points to the first

! A data structure representing a list. ! A series of dynamically allocated nodes. ! A separate pointer (the head) points to the first Linked Lists Introduction to Linked Lists A data structure representing a Week 8 Gaddis: Chapter 17 CS 5301 Spring 2014 Jill Seaman A series of dynamically allocated nodes chained together in sequence

More information