+ struct Node { + int x; + Node * next; + }; + Node *mylist; + Node *mylist = NULL; head 121.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Data Structures Linked Lists Linked List Basics. Array Disadvantages Arrays, although easy to understand have lots of disadvantages Contiguous Memory.
CSE Lecture 12 – Linked Lists …
Doubly linked list concept Node structure Insertion sort Insertion sort program with a doubly linked list.
Data Structure Lecture-5
Derived data types Dealing with data –Where the information is stored –What value is kept there –What kind of information is stored Address operator Pointers.
 A queue is a waiting line…….  It’s in daily life:-  A line of persons waiting to check out at a supermarket.  A line of persons waiting.
Stacks. COMP104 Slide 2 Stacks * A stack, S, is a data structure that supports: n push(x) make x the top element in stack S n Pop Remove the top item.
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
Infix to postfix conversion Process the tokens from a vector infixVect of tokens (strings) of an infix expression one by one When the token is an operand.
Circular List Next field in the last node contains a pointer back to the first node rather than null pointer From any point in such a list it is possible.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
PUSH “MATT” MATT FRONTBACK PUSH “ANDREW” MATT FRONT ANDREW BACK.
Lecture Objectives To understand how Java implements a stack To learn how to implement a stack using an underlying array or linked list Implement a simple.
Linked Lists list elements are stored, in memory, in an arbitrary order explicit information (called a link) is used to go from one element to the next.
Lists ADT (brief intro):  Abstract Data Type  A DESCRIPTION of a data type  The data type can be anything: lists, sets, trees, stacks, etc.  What.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
 STACK STACK  BASIC STACK OPERATIONS BASIC STACK OPERATIONS  PUSH ALGORITHM PUSH ALGORITHM  POP ALGORITHM POP ALGORITHM  EVALUATING A POSTFIX EXPRESSION.
224 3/30/98 CSE 143 Recursion [Sections 6.1, ]
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 2 Overloaded Operators, Class Templates, and Abstraction Jeffrey S. Childs Clarion University.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 6: Stacks Data Abstraction & Problem Solving with C++
Tracing through E01, question 9 – step 1 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include using namespace std; void mysteryFunction(int.
Data Structures Stack Namiq Sultan 1. Data Structure Definition: Data structures is a study of different methods of organizing the data and possible operations.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
CHAPTER 3 STACK CSEB324 DATA STRUCTURES & ALGORITHM.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 2)
Lecture 7 : Intro. to STL (Standard Template Library)
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 11 – Data Structures.
Lecture Objectives  To understand how Java implements a stack  To learn how to implement a stack using an underlying array or linked list  Implement.
LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.
1 CSC 222: Computer Programming II Spring 2004 Stacks and recursion  stack ADT  push, pop, top, empty, size  vector-based implementation, library 
Fall 2006 METU EEEEE 441 S. Ece (GURAN) SCH MIDT EE 441 Data Structures Lecture 6 Stacks and Queues.
Circular Linked List Singly Circular Linked List Doubly Circular Linked List.
11/07/141 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: –Data stored –Operations on the.
1 Data Structures CSCI 132, Spring 2016 Notes 6 Applications using Stacks.
Data Structures AZHAR MAQSOOD NUST Institute of Information Technology (NIIT) Lecture 6: Linked Lists Linked List Basics.
 Memory from the heap  Dynamic memory allocation using the new operator  Build a dynamic linked list  Another way of traversing a linked list 
 Review building a complete linked list  List traversal in main ( )  List traversal using a function.
Applications of Stack Maitrayee Mukerji. Stacks Last In First Out (LIFO List) ◦ FILO? Insertions and Deletions from the same end called the Top Push(),
CSC 143 P 1 CSC 143 Recursion [Chapter 5]. CSC 143 P 2 Recursion  A recursive definition is one which is defined in terms of itself  Example:  Compound.
 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.
1 Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues Jung Soo (Sue) Lim Cal State LA.
ADT Stack & Queue - Case Studies TCP1201: 2013/2014.
Programming Circular Linked List.
Popping Items Off a Stack Using a Function Lesson xx
Infix to postfix conversion
Stacks.
Stacks Stack: restricted variant of list
Algorithms and Data Structures
Stack.
Stack and Queue.
Pointers and Linked Lists
CSC215 Homework Homework 11 Due date: Dec 19, 2016.
Popping Items Off a Stack Lesson xx
Stack and Queues Stack implementation using Array
Stack and Queues Stack implementation using Array
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
reverse.cpp #include “stack.h” int main() { Stack stack;
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
Queue Applications Lecture 31 Mon, Apr 9, 2007.
Stacks Data structure Elements added, removed from one end only
Lecture 8 : Intro. to STL (Standard Template Library)
Stacks.
Jordi Cortadella and Jordi Petit Department of Computer Science
Stacks LIFO C C B B B B A A A A A Push (A) Push (B) Push (C) Pop Pop.
Queue Applications Lecture 31 Tue, Apr 11, 2006.
List Iterator Implementation
5.3 Implementing a Stack Chapter 5 – The Stack.
Presentation transcript:

+ struct Node { + int x; + Node * next; + }; + Node *mylist; + Node *mylist = NULL; head 121

+ Node *temp = new Node; + temp->x = 1; // the new data. + temp->next = mylist; + mylist = temp;

+ Node *temp = mylist; + while (temp) { // != NULL + if (temp->x == target) + return temp; + temp = temp->next; + }

+ Node *tail = mylist; + while (tail) { + tail = tail->next; + } + Node *temp = new Node; + temp->x = 1; // the new data. + temp->next = NULL; + tail->next= temp; //wrong! tail points to NULL

+ Node *tail = mylist; + while (tail->next) { // empty list?? + tail = tail->next; + } + Node *temp = new Node; + temp->x = 1; // the new data. + temp->next = NULL; + tail->next= temp;

head 121 dummy

+ Node *mylist = NULL; + Node *temp = new Node; + temp->x = -1; // dummy + temp->next = NULL; + mylist = temp; head -1(dummy)

+ Node *temp = new Node; + temp->x = 1; // the new data. + temp->next = mylist->next; + mylist->next = temp; head -1(dummy) 1

+ Node *tail = mylist; + while (tail->next) { + tail = tail->next; + } + Node *temp = new Node; + temp->x = 1; // the new data. + temp->next = NULL; + tail->next= temp;

+ keep a pointer pointing to the last node of the list to speed up

+ Node *temp = mylist; + while (temp->next) { // != NULL + if (temp->next->x == target) { + node *to_del = temp->next; + temp->next = to_del->next; + delete to_del; + break; + } + temp = temp->next; + }

+ while (mylist != 0) { + Node *t = mylist ->next; + delete mylist ; + mylist = t; + }

+ struct Node { + int x; + Node *prev; + Node * next; + }; + linked_list linked_list + y-.2C_doubly-.2C_and_multiply-linked_lists y-.2C_doubly-.2C_and_multiply-linked_lists

+ tructure%29 tructure%29 + Use cpp library? + Can implement stack by array/linked list

+ #include + using namespace std; + int main(){ + stack mystack; + int x; + while (cin >> x) + mystack.push(x); + while (!mystack.empty()){ + cout << mystack.top() << endl; + mystack.pop(); + }

+ _structure%29 _structure%29 + Use cpp library? + Can implement queue by array/linked list

+ #include + using namespace std; + int main(){ + queue myqueue; + int x; + while (cin >> x) + myqueue.push(x); + while (!myqueue.empty()){ + cout << myqueue.front() << endl; + myqueue.pop(); + }

+ 514 Rails + ml ml Stacks of Flapjacks + ml ml Parentheses Balance + ml ml

+ a+(b+c) abc++ + (a+b)+cab+c+ + a-b*cabc*- + (a/b)*(c/d)ab/cd/* + a/(b+c*d-e)abcd*+e-/ + a-b*c+d/eabc*-de/+ + small exercise: Write a program to read in a postfix expression and calculate the result + See demo

+ 727 Equation + ml ml

+ Use a stack to store operators, left parentheses. + Read a char, check if cahr is…. + 1operand print it + 2 ( push onto stack.

+ 3operator + – pop and print the operators from the stack one by one whenever they have a greater or equal precedence than char + –push the operator char onto the stack. + 4) + – pop and print the operators from the stack one by one, stop when see a ( + – pop the (