0% found this document useful (0 votes)
70 views20 pages

Link List Implementation in C

This document provides an overview of a lecture on data structures and algorithms using linked lists as an example. The lecture objectives are to apply knowledge of linked lists to solve problems, propose algorithms for various linked list operations, and explain algorithmic decomposition of problems. The document then discusses what data structures are, the types of data structures, and defines linked lists. It provides examples of implementing linked list operations through algorithmic decomposition, including pseudocode and partial C code. Finally, it provides an assignment on various linked list problems and references self-learning materials.

Uploaded by

Abhigyan Prakash
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views20 pages

Link List Implementation in C

This document provides an overview of a lecture on data structures and algorithms using linked lists as an example. The lecture objectives are to apply knowledge of linked lists to solve problems, propose algorithms for various linked list operations, and explain algorithmic decomposition of problems. The document then discusses what data structures are, the types of data structures, and defines linked lists. It provides examples of implementing linked list operations through algorithmic decomposition, including pseudocode and partial C code. Finally, it provides an assignment on various linked list problems and references self-learning materials.

Uploaded by

Abhigyan Prakash
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Data Structure And Algorithm

(EC-594B)

A. K. Siromoni

Department of Information Technology


Lecture 1

Lecture Objective 1 : Apply knowledge to solve a problem using Link List

Lecture Objective 2 : Propose algorithm for doing various operation of


Link List

Lecture Objective 3 : Explain the procedure of algorithmic decomposition


of a problem
When we have done programming in C
• Our knowledge was more language centered.

Now when we are learning DATA STRUCTURE


We are more concerned about

ALGORITHM
A problem can have many correct solutions….
Thus for a given problem there may be many
algorithms suitable in different situations.

•Exchange (a,b) when a and b are •Exchange (a,b) when a and b are
integer number integer number

1. Keep a in a temporary variable t 1. Add a and b and store in a


2. Assign the value b to a 2. Subtract b from a and store in b
3. Assign the value t to b 3. Subtract b from a and store in a
What is DATA STRUCTURE ?
• Data Type
• Organized Storage
• Related Operation

Data Type : 1. Build in or Primitive Data Type


2. Derived or Abstract Data Type

Organized Storage : 1. Array 2. List 3. Tree etc

Related Operation :
Traverse
Search
Insert
Delete
Sort etc.
Types of Data Structure :

Linear Data Structure : Linear Search, Stack, Queue etc


Non Linear Data Structure : Tree, Binary Search etc

LINK LIST IS A LINEAR DATA STRUCTURE


Link List
Node Tail

Null

Advantage : 1. Dynamically allocates memory if required


2. Can be used both as Stack or Queue
3. Operations are relatively easy

Disadvantage : 1. Can not be traversed randomly


2. Extra memory required for storage of address
Implementation through Algorithmic Decomposition
Data Type :
A heterogeneous array to keep Data and Address of the Next Node
Organise Storage :
1. Create a node
2. Keep the address in head
3. Put the data in head
4. Put the next address part of head to be null
5. Keep the head address in node1
6. Create next node
7. Keep the address of the node in next of node1
8. Now change the value of node1 to be next of node1
9. Put the data in node1
10.Keep the next address of node1 to null
11.Ask user any data left
12.If yes go to 6
Related Operation :
Show the data from head node to next node till it reaches null
Implementation through Algorithmic Decomposition
Data Type :
A heterogeneous array to keep Data and Address of the Next Node

Refinement of previous algorithm

1. Create a node and Keep the address in head


2. Put the data in head and put the next address part of head to be null
3. Keep the head address in node
4. Create next node and Keep the address of the node in next of node
5. Now change the value of node to be next of node
6. Put the data in node and Keep the next address of node1 to null
7. Ask user any data left
8. If yes go to 4
9. Show the data from head node to next node till it reaches null
Implementation through Algorithmic
Decomposition contd.

Implementation in pseudo code

1. Link { Data; Next Link Address} //Data atype


2. Link_address Head, Node // Start Storage
3. Head =createnode()
4. Putdata(Head)
5. Node=Head
6. Node_next = createnode()
7. Node= Node_next
8. Putdata(Node)
9. Ask user if answer = yes coninue, go to 6 //End Storage
10.Showdata(Head) // Related Operation
Implementation through Algorithmic
Decomposition contd.
Partial implementation in C language
/* Link List in C Language */
#include<stdio.h>

struct link { int data;


struct link *next; } ;

main() { struct link *head , *node;


head = createnode();
putdata(head);
node=head;
while(getans()){
node->next = createnode();
node=node->next;
putdata(node);}
showdata(head);}
Implementation through Algorithmic
Decomposition contd.
Now the problem is decomposed into four reusable functions
1. createnode
2. putdata
3. getans
4. showdata

•Analysis of calling of the function will


determine the prototype of the function

•Then the algorithm will tell the definition of


the function.
Implementation through Algorithmic
Decomposition contd.

• Analysis of createnode
Head = createnode()
Node = createnode()

Decision : Return data type struct link *


Argument passed – none

Prototype : struct link * createnode();

• Definition of createnode
struct link * createnode()
{ struct link * node;
node = (struct link *) malloc (sizeof(struct link));
return node; }
Implementation through Algorithmic
Decomposition contd.
•Analysis of putdata
putdata(head);
putdata(node);

Decision : Return data type void


Argument passed – one
Argument data type – struct link *

Prototype : void putdata(struct link *);

•Definition of putdata
void putdata(struct link * node){ int data;
scanf(“%d”, &data);
node->data = data;
node->next = NULL; }
Implementation through Algorithmic
Decomposition contd.
•Analysis of getans
while(getans()){…..}

Decision : Return data type integer


( 0 if false , non zero if true)
Argument passed - None

Prototype : int getans();

•Definition of getans

int getans(){int ans;


printf(“put 0 for stop , non zero to continue”);
scanf(“%d”, &ans); return ans;}
Implementation through Algorithmic
Decomposition contd.
•Analysis of showdata
showdata(head);

Decision : Return data type – void


Argument passed – one
Argument data type – struct link *

Prototype : void showdata( struct link *);

•Definition of showdata

void showdata(struct link * node){


while (node != NULL){
printf (“%d \n”, node->data);
node = node->next;}
}
• So the final program for creation and traversal of link list

#include<stdio.h>
struct link { int data;
struct link *next; } ; main() { struct link *head , *node;
head = createnode();
void showdata(sruct link * node){ putdata(head);
while (node != NULL){ node=head;
printf (“%d \n”, node->data); while(getans()) {
node = node->next; } } node->next = createnode();
node=node->next;
int getans(){int ans; putdata(node);
printf(“put 0 for stop , non zero to }
continue”); showdata(head);
scanf(“%d”, &ans); return ans;} }

void putdata(struct link * node){ int data;


scanf(“%d”, &data);
node->data = data;
node->next = NULL; }

struct link * createnode()


{ struct link * node;
node = (struct link *) malloc (sizeof(struct
link));
return node; }
Assignment 1

1. Create a link list where head will always be


the last added node.

2. Create a link list in sorted order.

3. Take an integer number from user, search


in a link list whether that data is available or
not in any node. If available delete the node
otherwise print a proper message.

( Every Program should be properly documented )


• Covers LO1, LO2 and LO3
Self Learning
For Assignment 1

1. Data Structure Using C & C++ : Langsum, Augenstein , Tanenbaum ,


Second Edition, Prentice Hall of India
Page 186 – 191, 195 – 200, 211 - 213

2. Data Structures : Seymour Lipschutz , Indian Adapted Edition


2006, Tata McGaw Hill Education Pvt Ltd
Page 5.10 – 5.12, 5.17 – 5.31

You might also like