Free Essay

Linked List Programs

In:

Submitted By chot
Words 251
Pages 2
#include

#include

struct node

{

int info;

struct node *link;

};

struct node *start=NULL,*n,*temp,*temp1;

void create();

void display();

void insbeg();

void inspos();

void delbeg();

void delend();

void search();

int main()

{ int i;

printf("------linked list----------\n");

n=(struct node *)malloc(sizeof(struct node));

printf("enter the element:--->");

scanf("%d",&n->info);

n->link=NULL;

start=n;

for(i=0;iinfo);

n->link=start;

start=n;

}

void create()

{

n=(struct node *)malloc(sizeof(struct node));

printf("enter the element--->");

scanf("%d",&n->info);

temp=start;

while(temp->link!=NULL)

{

temp=temp->link;

}

temp->link=n;

n->link=NULL;

}

void display()

{

printf("The list is:\n");

temp=start;

while(temp!=NULL)

{

printf("%d\n",temp->info);

temp=temp->link;

}

}

void inspos()

{

int pos, count=0;

temp=start;

if(temp==NULL)

count=0;

else

count=1;

printf("enter position.");

scanf("%d",&pos);

if(pos==1)

{

insbeg();

return;

}

n=(struct node *)malloc(sizeof(struct node));

printf("enter the element--->");

scanf("%d",&n->info);

while(temp->link!=NULL)

{

if(count==pos-1)

break;

temp=temp->link;

count++;

}

n->link=temp->link;

temp->link=n;

}

void delbeg()

{

temp=start;

start=start->link;

free(temp);

}

void delend()

{

temp=start;

while(temp->link!=NULL)

{

temp1=temp;

temp=temp->link;

}

temp1->link=NULL;

free(temp);

}

void search()

{

int item,count=0;

printf("enter the item to be search");

scanf("%d",&item);

temp=start;

count=1;

while(temp!=NULL)

{

if(temp->info==item)

{

printf("item found at location %d\n",count);

break;

}

else

{

temp=temp->link;

count++;

}

}

}

Similar Documents

Free Essay

Hallo World

...file pair. The main program is the driver of the whole exercise and uses (includes) all of the .h files of the different linked lists. The main program performs the following steps: 1. It begins by generating N (see below) random integers between 0 and 9 and stores them in a binary file called insert.bin. It should let the user know what numbers were generated and written. 2. It then generates another N random integers between 0 and 9 and stores them in another binary file called delete.bin. It should let the user know what numbers were generated and written. 3. It reads insert.bin one integer at a time and stores the value read into each one of the lists, keeping each list sorted in increasing order. If a number occurs more than once in the list, instead of creating a new node, it increments a count variable on the node. It should let the user know what value it read from the file and it should show the four lists with the new value inserted, identifying each type of list. To display a node, show key and count. 4. Once done reading the insert.bin file, it starts reading the delete.bin file one value at a time, deleting that value from each of the linked lists. If the value occurs in the list more than once (the count member on the node is greater than 1), it decrements the count. It is only when the count is 1 and deleting the node would make it 0 that the node is actually removed from the list and freed. It should let the user know what value it read from the file and...

Words: 290 - Pages: 2

Free Essay

Dont Have Papers Yet

...A linked list is made up of a series of objects, called the nodes of the list. Because a list node is a distinct object. Linked lists are among the simplest and most common data structures. The principal benefit of a linked list over a conventional array is that the list elements can easily be inserted or removed without reallocation or reorganization of the entire structure because the data items need not be stored contiguously in memory or on disk, while an array has to be declared in the source code, before compiling and running the program. Linked lists allow insertion and removal of nodes at any point in the list, and can do so with a constant number of operations if the link previous to the link being added or removed is maintained during list traversal. Singly Linked List: Singly linked lists contain nodes which have a data field as well as a next field, which points to the next node in line of nodes. Operations that can be performed on singly linked lists include insertion, deletion and traversal. Finding and Deleting Specified Link: Finding algorithm Beginning from the head, 1. Check, if the end of a list hasn't been reached yet; 2. Do some actions with the current node, which is specific for particular algorithm; 3. Current node becomes previous and next node becomes current. Deleting 4. Save reference to link 5. Delete it, save first to old next 6. Return deleted link Operation on Singly Link List: * Search * Insert ...

Words: 818 - Pages: 4

Free Essay

Khvji

...STRUCTURES, POINTERS, AND LINKED DATA STRUCTURES • The following slides provide the C-code around which we will structure our discussion tomorrow. • Please note that this code by itself is incomplete. • Class attendance is necessary to understand the theme. • Those who do not attend the class must read the Text book and come to consultations, if help is needed. Structures and Lists #include #include struct Student { char *name ; int age; }; // Note: char *name ="xxxxxxxxxxxxxx"; is // not permitted in structures. Why? int main(void){ struct Student s; // allocates memory. s.name = malloc(20*sizeof(char)); scanf("%s",s.name); s.age = 20; printf("1:%s %d\n",s.name, s.age); struct Student *ps; ps = &s; // Dot has higher priority over & scanf("%s%d",(*ps).name,&(*ps).age); printf("2:%s %d\n", (*ps).name, (*ps).age); printf("3:%s %d\n",ps->name,ps->age+3); struct Student *ps1; // allocate memory by malloc() dynamically. ps1 = malloc(sizeof(struct Student)); ps1->name = malloc(20*sizeof(char)); scanf("%s%d",ps1->name,&(ps1->age)); printf("4:%s %d\n",ps1->name,ps1->age+3); printf("5:%s %d\n", (*ps1).name, (*ps1).age); } Student s 0028FF08 malloc 003C1110 name name age age ps malloc 0028FF00 003C1188 ps1 malloc #include #include struct S1 { int a1; char b1; }; struct S2{ int a2; char b2; struct S1 *p2; }; struct S3{ int a3; char b3; struct S1 *p3a; struct S2 *p3b; }; Linked structures // Self...

Words: 1984 - Pages: 8

Premium Essay

It 265 Data Structures Phase 5

...IT 265 Data Structures for Problem Solving Data Structures and Methods 9/20/2014 Phase 5 Contents Executive Summary 4 Phase 1 4 Phase 2 4 Phase 3 4 Phase 4 4 Phase 5 5 Section 1: Lists, Stacks, and Queues 6 Stacks 6 Queues 10 Section 2: Hashing, Heaps and Trees 14 Section 3: Sorting Algorithms 20 Insertion sort 20 Bubble Sort 20 Selection sort 21 Section 4: Searching 22 Array 22 Linked Lists 23 Section 5: Recursion 30 References 33 Executive Summary Phase 1          A list is a collection of items in which the items have a position (Weiss, 2010).  A linked list allows data to be input or removed easily because each of the data items in the list is connected to its neighbor by a pointer that can be quickly and easily modified to accommodate new or removed items (CTU M.U.S.E. 2014). The Phase 1 portion of this document will be demonstrating the implementation of Stacks and Queues. Phase 2 Hash tables can be viewed as an array of lists and are used to speed up a search for data by creating a situation that does not require the search to start at the beginning and go through every item. The identifying value is the key and the associated record is the data, thus a hash table is a collection of (key, value) pairs. Phase 3 In order to efficiently use a database, the data must be stored in some sort of order. There are a number of different sorting algorithms; a programmer would choose which one to use depending on the amount...

Words: 3704 - Pages: 15

Free Essay

Assigment

...CS301 – Data Structures ___________________________________________________________________ Data Structures 1 CS301 – Data Structures ___________________________________________________________________ Data Structures..........................................................................................................1 Lecture No. 01 ............................................................................................................3 Lecture No. 02 ..........................................................................................................12 Lecture No. 03 ..........................................................................................................21 Lecture No. 04 ..........................................................................................................34 Lecture No. 05 ..........................................................................................................49 Lecture No. 06 ..........................................................................................................59 Lecture No. 07 ..........................................................................................................66 Lecture No. 08 ..........................................................................................................73 Lecture No. 09 ..........................................................................................................84 Lecture No. 10 ....................................

Words: 13571 - Pages: 55

Free Essay

Programming

...TeamLRN Robert Lafore Teach Yourself Data Structures and Algorithms in 24 Hours 201 West 103rd St., Indianapolis, Indiana, 46290 USA Sams Teach Yourself Data Structures and Algorithms in 24 Hours Copyright © 1999 by Sams Publishing All rights reserved. No part of this book shall be reproduced, stored in a retrieval system, or transmitted by any means, electronic, mechanical, photocopying, recording, or otherwise, without written permission from the publisher. No patent liability is assumed with respect to the use of the information contained herein. Although every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions. Neither is any liability assumed for damages resulting from the use of the information contained herein. International Standard Book Number: 0-672-31633-1 Library of Congress Catalog Card Number: 98-83221 Printed in the United States of America First Printing: May 1999 01 00 99 4 3 2 1 EXECUTIVE EDITOR Brian Gill DEVELOPMENT EDITOR Jeff Durham MANAGING EDITOR Jodi Jensen PROJECT EDITOR Tonya Simpson COPY EDITOR Mike Henry INDEXER Larry Sweazy PROOFREADERS Mona Brown Jill Mazurczyk TECHNICAL EDITOR Richard Wright Trademarks All terms mentioned in this book that are known to be trademarks or service marks have been appropriately capitalized. Sams Publishing cannot attest to the accuracy of this information. Use of a term in this...

Words: 10065 - Pages: 41

Premium Essay

Articles

...Question 5 Evaluate the following postfix expression using a stack and show the contents of stack after execution of each operation : 50,40,+,18, 14,-, *,+ Question 6 Evaluate the following postfix expression using a stack and show the contents of stack after execution of each operation : TRUE, FALSE, TRUE, FALSE, NOT, OR, TRUE, OR, OR, AND Question 7 Write a program for creating polynomial using linked list? Question 8 Each node of a STACK contains the following information, in addition to required pointer field : i) Roll number of the student ii) Age of the student Give the structure of node for the linked stack in question TOP is a pointer which points to the topmost node of the STACK. Write the following functions. i) PUSH() - To push a node to the stack which is allocated dynamically ii) POP() - To remove a node from the stack and release the memory. Question 9 Write a function in C to perform a DELETE operation in a dynamically allocated link list considering the following description : struct Node {...

Words: 357 - Pages: 2

Free Essay

Mfrkkwl

...An array is defined as a sequence of objects of the same data type. All the elements of an array are either of type int (whole numbers), or all of them are of type char, or all of them are of floating decimal point type, etc. An array cannot have a mixture of different data types as its elements. Also, array elements cannot be functions; however, they may be pointers to functions. In computer memory, array elements are stored in a sequence of adjacent memory blocks. Since all the elements of an array are of same data type, the memory blocks allocated to elements of an array are also of same size. Each element of an array occupies one block of memory. The size of memory blocks allocated depends on the data type and it is same as for different data types. Often, we have to deal with groups of objects of same type such as names of persons, instrument readings in an experiment, roll numbers of students, and so on. These groups can be conveniently represented as elements of arrays. The declaration of array includes the type of array that is the type of value we are going to store in it, the array name and maximum number of elements. Examples: short val[200]; val[12] = 5;   Declaration & Data Types Arrays have the same data types as variables, i.e., short, long, float etc. They are similar to variables: they can either be declared global or local. They are declared by the given syntax: Datatype array_name [dimensions] = {element1,element2,….,element} The declaration form...

Words: 7409 - Pages: 30

Premium Essay

Data Structure

... Question 5 Evaluate the following postfix expression using a stack and show the contents of stack after execution of each operation : 50,40,+,18, 14,-, *,+ Question 6 Evaluate the following postfix expression using a stack and show the contents of stack after execution of each operation : TRUE, FALSE, TRUE, FALSE, NOT, OR, TRUE, OR, OR, AND Question 7 Write a program for creating polynomial using linked list? Question 8 Each node of a STACK contains the following information, in addition to required pointer field : i) Roll number of the student ii) Age of the student Give the structure of node for the linked stack in question TOP is a pointer which points to the topmost node of the STACK. Write the following functions. i) PUSH() - To push a node to the stack which is allocated dynamically ii) POP() - To remove a node from the stack and release the memory. Question 9 Write a function in C to perform a DELETE operation in a dynamically allocated link list considering the following description : struct Node { float...

Words: 308 - Pages: 2

Free Essay

First Upload

... | |2. |A program to receive data that is to be saved and processed in the reverse order | | |A | |3. |An electronic address book ordered by name | | |D | |4. |A word processor to have a PF key that causes the preceding command to be redisplayed. | | |Every time the PF key is pressed, the program is to show the command that preceded the | | |one currently displayed | | |A | |5. |A dictionary of words used by a spelling checker to be built and maintained. | | |D | |6. |A program to keep track of patients as they check into a medical clinic, assigning | | |patients to doctors on a first-come, first-served basis. | | |B | |7. |A program keeping track of...

Words: 2128 - Pages: 9

Free Essay

Data Structures

... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1 1 1 3 4 4 6 6 7 7 7 I Data Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . reverse order . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 9 9 10 10 11 12 13 13 15 15 16 17 19 20 21 22 24 24 25 26 26 2 Linked Lists 2.1 Singly Linked List . . . . . 2.1.1...

Words: 23014 - Pages: 93

Free Essay

Ds Java

...Structures 1.1.1 The Need for Data Structures 1.1.2 Costs and Benefits 1.2 Abstract Data Types and Data Structures 1.3 Design Patterns 1.3.1 Flyweight 1.3.2 Visitor 1.3.3 Composite 1.3.4 Strategy 1.4 Problems, Algorithms, and Programs 1.5 Further Reading 1.6 Exercises 3 4 4 6 8 12 13 14 15 16 17 19 21 2 Mathematical Preliminaries 2.1 Sets and Relations 2.2 Miscellaneous Notation 2.3 Logarithms 2.4 Summations and Recurrences 25 25 29 31 33 iii iv Contents 2.5 2.6 2.7 2.8 2.9 3 II 4 Recursion Mathematical Proof Techniques 2.6.1 Direct Proof 2.6.2 Proof by Contradiction 2.6.3 Proof by Mathematical Induction Estimating Further Reading Exercises Algorithm Analysis 3.1 Introduction 3.2 Best, Worst, and Average Cases 3.3 A Faster Computer, or a Faster Algorithm? 3.4 Asymptotic Analysis 3.4.1 Upper Bounds 3.4.2 Lower Bounds 3.4.3 Θ Notation 3.4.4 Simplifying Rules 3.4.5 Classifying Functions 3.5 Calculating the Running Time for a Program 3.6 Analyzing Problems 3.7 Common Misunderstandings 3.8 Multiple Parameters 3.9 Space Bounds 3.10 Speeding Up Your Programs 3.11 Empirical Analysis 3.12 Further Reading 3.13 Exercises 3.14 Projects Fundamental Data Structures Lists, Stacks, and Queues 36 39 40 40 41 47 49 50 57 57 63...

Words: 30587 - Pages: 123

Free Essay

Gdhdgfrdes

...space Complexity and capacity Data and space 3) a) b) c) d) A doubly nested “for loop” typically takes time in: Θ(n2) Θ(n) Θ(log n) Θ(log n2) 4) a) b) c) d) Arrays are best data structures: For relatively permanent collections of data For the size of the structure and the data in the structure are constantly changing For both of above situation For none of above situation 5) The elements of an array are stored successively in memory cells because: a) The architecture of computer memory does not allow arrays to store other than serially b) By this way computer can keep track only the address of the first element and the addresses of other elements can be calculated c) Both of above d) None of above 6) A program P reads in 500 integers in the range [0..100] presenting the scores of 500 students. It then prints the frequency of each score above 50. What would be the best way for P to...

Words: 1212 - Pages: 5

Free Essay

Non Linear Data Structure

...efficiently that the free contiguous memory in not an requirement for allocating data items • The length of the data items is not necessary to be known prior to allocation Cons • Overhead of the link to the next data item Linked list: linked list a data structure which stores data in the form of nodes.It does not require linear memory as arrays. Each node contains a data part and a pointer part(a pointer to the next data in the list) link or node is object of a class.there are so many types of linked list 1) single linked list 2)doubly linked list 3)circular linked list. single linked list: here links contains pointer to first data and last data in the list.As said earlier a pointer to the next data. example of a linked list: class node{// all nodes will be the objects of this class public int data; public link next_node;//a pointer to next data } public node(int data){ this.data=data; }//end of constructor public void showdata(){ System.out.println("data= "+data); } }//end of class node After defining class for each node we need to define a class for link list. Link list contains a pointer to the first node in the list,Which should be initialized to null in the beginning. All the operations to be performed on the link list is defined as functions in this class. For example insert first,Delete,insert search etc. class linkedlist{ private node first_node;//pointer to the first node is defined public linkedlist(){ first_node=null; } public void...

Words: 475 - Pages: 2

Free Essay

Linkedlist

...Linked List 1 List vs Arrays Two built-in data structures that can be used to organize data, or to create other data structures: • Lists • Arrays Lists A list is an ordered set of data. It is often used to store objects that are to be processed sequentially. Arrays An array is an indexed set of variables, such as dancer[1], dancer[2], dancer[3],… It is like a set of boxes that hold things. A list is a set of items. An array is a set of variables that each store an item. Arrays and Lists You can see the difference between arrays and lists when you delete items. Arrays and Lists In a list, the missing spot is filled in when something is deleted. Arrays and Lists In an array, an empty variable is left behind when something is deleted. What’s wrong with Array and Why lists? • Disadvantages of arrays as storage data structures: – slow searching in unordered array – slow insertion in ordered array – Fixed size • Linked lists solve some of these problems • Linked lists are general purpose storage data structures and are versatile. Linked Lists A Head B C  • A linked list is a series of connected nodes • Each node contains at least – A piece of data (any type) – Pointer to the next node in the list • Head: pointer to the first node • The last node points to NULL node A data pointer The composition of a Linked List • A linked list is called "linked" because each node in the series has a pointer that points...

Words: 2375 - Pages: 10