Linked List Insert as a first node Insert as a last node

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Singly Linked List BTECH, EE KAZIRANGA UNIVERSITY.
Data Structure Lecture-5
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
§3 The Stack ADT 1. ADT A stack is a Last-In-First-Out (LIFO) list, that is, an ordered list in which insertions and deletions are.
CSCE 3110 Data Structures & Algorithm Analysis Stacks and Queues Reading: Chap.3 Weiss.
CS Data Structures ( 資料結構 ) Chapter 3: Stacks and Queues Spring 2012.
Main Index Contents 11 Main Index Contents Stacks Further Stack Examples Further Stack Examples Pushing/Popping a Stack Pushing/Popping a Stack Class StackClass.
CS Data Structures Chapter 3 Stacks and Queues.
Chapter 3 Stacks and Queues  The Stack Abstract Data Type  The Queue Abstract Data Type  A Mazing Problem  Evaluation of Expressions.
The Stack and Queue Types Lecture 10 Hartmut Kaiser
CSC 205 Programming II Postfix Expressions. Recap: Stack Stack features Orderly linear structure Access from one side only – top item Stack operations.
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
UNIT-2. Singly Linked List Doubly Linked List Circular Linked List Representing Stack with Linked List. Representing Queue with Linked List.
Data Structures. The Stack: Definition A stack is an ordered collection of items into which new items may be inserted and from which items may be deleted.
Trees.ppt1 Introduction Many data structures are linear –unique first component –unique last component –other components have unique predecessor and successor.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
CSC 211 Data Structures Lecture 23
EENG212 Algorithms and Data Structures
Data Structures Stack Namiq Sultan 1. Data Structure Definition: Data structures is a study of different methods of organizing the data and possible operations.
CHAPTER 3 STACK CSEB324 DATA STRUCTURES & ALGORITHM.
Definition: A stack is an ordered collection of elements in which insertions(Push) and deletions(Pop) are restricted to one end. LIFO(Last In First Out)
Stacks & Queues. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy to use.
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.
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.
UNIT-1 UNIT – I Introduction to data structures: Abstract data type (ADT), Stacks and Queues circular queues and their implementation with arrays. Stack.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
 In general, Queue is line of person waiting for their turn at some service counter like ticket window at cinema hall, at bus stand or at railway station.
CSC 172 DATA STRUCTURES. A TALE OF TWO STRUCTURES.
STACKS & QUEUES for CLASS XII ( C++).
Chapter 4 Stacks
Computing with C# and the .NET Framework
Data Structure By Amee Trivedi.
Chapter 12 – Data Structures
Lectures linked lists Chapter 6 of textbook
Chapter 15 Lists Objectives
MEMORY REPRESENTATION OF STACKS
Lecture No.06 Data Structures Dr. Sohail Aslam
Objectives In this lesson, you will learn to: Define stacks
Queue data structure.
Stack and Queue APURBO DATTA.
CSC 172 DATA STRUCTURES.
Queues Mohammad Asad Abbasi Lecture 5
Data Structures – Week #3
Algorithms and Data Structures
STACK CHAPTER 03 Developed By :- Misha Ann Alexander Data Structures.
Visit for more Learning Resources
Stack.
Stack and Queue.
INSERTION INTO A LINEAR ARRAY Set J = N Repeat step 3 and 4 while J>= K Set LA[ J+1] = LA [ J ] Set J = J-1 Set LA [K] = ITEM Set N = N+1 Exit.
Stacks and Queues The Stack abstract data type
STACK By:- Rajendra ShakyawalP.G.T. Computer Science KV-No.1, AFS, Tambaram, Chennai.
Stacks, Queues, and Deques
ليست هاي پيوندي.
Stack and Queues Stack implementation using Array
Linked List Insert as a first node Insert as a last node
Stack and Queues Stack implementation using Array
UNIT-I Topics to be covere d 1.Introduction to data structures.
Data structure types. Linear and Non-Linear Data Structures: Linked lists, stacks and queues.
Stack A data structure in which elements are inserted and removed only at one end (called the top). Enforces Last-In-First-Out (LIFO) Uses of Stacks Evaluating.
Stacks and Queues 1.
Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
Queue Applications Lecture 31 Mon, Apr 9, 2007.
Stacks and Queues Prof. Michael Tsai 2017/02/21.
Stacks.
Stacks and Queues.
Queue Applications Lecture 31 Tue, Apr 11, 2006.
17CS1102 DATA STRUCTURES © 2016 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS.
Presented by : Aman Gupta PGT CS KV No.1, Narimedu, Madurai
LINEAR DATA STRUCTURES
Presentation transcript:

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 Traverse

Insert as a first node void insertf() { struct studinfo *newnode; newnode=(struct studinfo *) malloc(sizeof(struct studinfo)); printf("\nEnter a new record : marks and name "); scanf("%d%s",&newnode->marks,newnode->name); newnode->next=NULL; if(start==NULL) start = newnode; else newnode->next = start; }

Insert as a last node void insertl() { struct studinfo *newnode, *ptr; newnode=(struct studinfo *) malloc(sizeof(struct studinfo)); printf("\nEnter a new record : marks and name "); scanf("%d%s",&newnode->marks,newnode->name); newnode->next=NULL; if (start == NULL) start =newnode; else ptr =start; while (ptr->next != NULL) ptr= ptr->next; ptr->next = newnode; }

Delete first node void deletef() { struct studinfo *ptr; if (start == NULL) printf("\n List is empty"); return; } ptr=start; start=start->next; free(ptr);

Delete last node void deletel() { struct studinfo *ptr,*prevptr; ptr=start; if (ptr->next==NULL) start = NULL; else while(ptr->next!=NULL) prevptr=ptr; ptr=ptr->next; } prevptr->next =NULL; free(ptr);

Insert after a node void inserta() { int cnt=1, no; struct studinfo *ptr,*prevptr, *newnode; printf("\nEnter number ..."); scanf("%d",&no); ptr=start; while (cnt != no) ptr = ptr->next; cnt++; } newnode=(struct studinfo *) malloc(sizeof(struct studinfo)); printf("\nEnter a new record : marks and name "); scanf("%d%s",&newnode->marks,newnode->name); newnode->next =NULL; newnode->next = ptr->next; ptr->next = newnode;

Insert before a node void insertb() { int cnt=1, no; struct studinfo *ptr,*prevptr, *newnode; printf("\nEnter number ..."); scanf("%d",&no); ptr=start; if(no==1) insertf(); } else while(cnt !=no) prevptr=ptr; ptr = ptr->next; cnt++;

Continue… newnode=(struct studinfo *) malloc(sizeof(struct studinfo)); printf("\nEnter a new record : marks and name "); scanf("%d%s",&newnode->marks,newnode- >name); newnode->next=ptr; prevptr->next=newnode; }

Traverse void traverse() { struct studinfo *ptr; if (start == NULL) printf("\n List is empty"); return; } ptr= start; while (ptr !=NULL) printf("\nRecord: marks and name %d %s\n",ptr->marks, ptr->name); ptr = ptr->next; getch();

STACK Stack using array Push Pop display Stack using linked list Create Stack Application Towe of Hanoi

stack: a Last-In-First-Out (LIFO) list B A C B A D C B A E D C B A top D C B A top top top top top Inserting and deleting elements in a stack

Push void push() { if(top==9) printf("\nStack Full!!!!\n"); return; } printf("\nEnter element:\n"); scanf("%d",&num); top=top+1; arr[top]=num; printf("\n %d pushed,\n",num);

Pop void pop() { if(top==-1) printf("\nStack Empty!!!!\n"); return; } printf("\nThe pop value is %d.\n",arr[top]); top=top-1;

Display void display() { int i; if(top==-1) printf("\nStack Empty!!!!\n"); return; } printf("\nThe contents of the stack is:\n"); printf("\n==========================================\n"); for(i=top;i>=0;i--) printf("%d\t",arr[i]);

Create struct stack{ int items; struct stack *next; }*top;

push void push() { struct stack *newnode; newnode=(struct stack *) malloc(sizeof(struct stack)); printf("Enter a new items: "); scanf("%d",&newnode->items); newnode->next=NULL; if(top==NULL) top = newnode; else newnode->next = top; }

pop void pop() { struct stack *ptr; ptr=top; top=top->next; free(ptr); }

display void display() { struct stack *ptr; ptr= top; printf("\n Stack items: "); while (ptr !=NULL) printf("\n %d \n",ptr->items); ptr = ptr->next; } getch();

Evaluation of Expressions X = a / b - c + d * e - a * c a = 4, b = c = 2, d = e = 3 Interpretation 1: ((4/2)-2)+(3*3)-(4*2)=0 + 8+9=1 Interpretation 2: (4/(2-2+3))*(3-4)*2=(4/3)*(-1)*2=-2.66666… How to generate the machine instructions corresponding to a given expression? precedence rule + associative rule

1.The precedence column is taken from Harbison and Steele. 2.Postfix form 3.prefix form Precedence hierarchy for C

Postfix: no parentheses, no precedence user compiler Infix and postfix notation Postfix: no parentheses, no precedence

Postfix evaluation

Tower Of Hanoi void towers(char needle1, char needle2, char needle3, int n) { if( n <= 0) printf("\n Illegal entry "); if(n == 1) /*If only one disk, make the move and return */ printf("\n Move Disk 1 from needle %c to needle %c", needle1, needle2); return; } towers(needle1, needle3, needle2, n-1); /* Move top n -1 disks from A to B, using C as auxiliary */ printf("\n Move Disk %d from needle %c to needle %c",n, needle1, needle2); /* Move remaining disk from A to C */ towers(needle3, needle2, needle1, n-1); /* Move n -1 disks from B to C, using A as auxiliary */

Queue using array Insert Delete display Queue using linked list Create

Queue: a First-In-First-Out (FIFO) list rear front D C B A B A C B A D C B A rear front rear front rear front rear front Inserting and deleting elements in a queue

Application: Job scheduling *Figure 3.5: Insertion and deletion from a sequential queue (p.108)

Create struct queue{ int items[MAXSIZE]; int rear; int front; }q;

Insert void queueins(struct queue *q ,int x) { if (q->rear==MAXSIZE -1) printf("Queue full\n"); return; } else if (q->front == -1) q->front =0; q->rear = 0; q->rear = q->rear+1 ; q->items[q->rear] = x;

Delete int queuedel(struct queue *q) { int x; if (q->front== -1) printf(" Queue is empty\n"); x = q->items[q->front]; if (q->front == q->rear) q->front = -1; q->rear =-1; } else q->front = q->front +1 ; return x;

Display void display(struct queue *q) { int i; if (q->front != -1) if (q->front <= q->rear) for( i=q->front;i<=q->rear;i++) printf("%d ",q->items[i]); }

Create struct queue{ int items; struct queue *next; }*rear,*front;

Insert void insertion() { struct queue *newnode, *ptr; newnode=(struct queue *) malloc(sizeof(struct queue)); printf(" Enter items "); scanf("%d",&newnode->items); newnode->next=NULL; if (rear == NULL) rear =newnode; front = newnode; } else rear->next = newnode; rear = newnode;

Delete void deletion() { struct queue *ptr; if (front == NULL) printf("\n Queue is empty "); rear=NULL; return ; } ptr=front; front=front->next; free(ptr);

Display void display(struct queue *q) { int i; if (q->front != -1) if (q->front <= q->rear) for( i=q->front;i<=q->rear;i++) printf("%d ",q->items[i]); }