Algorithm-linked list implementation stack

Source: Internet
Author: User

A linked list is a recursive data structure that is either empty (null), or just a reference to a node, a node that contains an object and a reference to another linked list, a node that may be an abstract corpse containing arbitrary data, and a node-only application that shows its role in the list. Compared to the array to have more flexibility, this article on a simple list to implement the stack, the biggest feature of the stack is LIFO, the queue is FIFO, the two are not the same, this article will be a simple implementation of OC stack.

Node Definition:

@interface node:nsobject@property  (strong,nonatomic)  nsstring  *value; @property  (Strong, nonatomic)  Node  *next; @end

Stack header file Definition:

@interface element @property  (strong,nonatomic) Node *first of stack:nsobject//stack top  , @property  (Assign, nonatomic) Nsinteger  count;-(BOOL) isempty;-(Nsinteger) size;-(void) Push: (NSString *) value;-(NSString *) pop;-( void) Remove: (NSString *) value; @end

There are three main implementation methods, into the stack (push), out of the stack (pop), delete (remove), it is important to note that the deletion of this article is a one-way list of the deletion, if the deletion of the last, time complexity and the length of the linked list is related, we can use a doubly linked list, interested can be studied.

Implementation code for the STACK.M:

@implementation stack-(BOOL) isempty{return self.count==0;} -(Nsinteger) size{return self.count;}    -(void) Push: (NSString *) value{Node *oldfirst=self.first;    Self.first=[[node Alloc]init];    Self.first.value=value;    Self.first.next=oldfirst; self.count=self.count+1;}    -(NSString *) pop{if (!self.first) {return [NSString stringwithformat:@ "-1"];    } NSString *value=self.first.value;    Self.first=self.first.next;    self.count=self.count-1; return value;}        -(void) Remove: (NSString *) value{if ([Self.first.value Isequaltostring:value]) {Node *node=self.first;        Node *nextnode=node.next;        Node.value=nextnode.value;        Node.next=nextnode.next;    self.count=self.count-1;        }else{Node *node=self.first;                    while (Node.next) {if ([Node.next.value Isequaltostring:value]) {if (Node.next.next) {                    Node *tempnode=node.next.next;                Node.next=tempnode; }else{Node.next=null;                } self.count=self.count-1;            Break            }else{Node=node.next; }}} @end

Test code:

        Stack  *stack=[[stack Alloc]init];        Node *first=[[node Alloc]init];        [Email protected] "iOS technology group: 228407086";        First.next=null;        Stack.first=first;        [Stack push:@ "flyelephant"];        [Stack push:@ "blog Park"];        [Stack push:@ "Keso"];        [Stack remove:@ "flyelephant"];        NSLog (@ "Out of stack:%@", stack.pop);        NSLog (@ "Out of stack:%@", stack.pop);        NSLog (@ "Out of stack:%@", stack.pop);        NSLog (@ "Out of stack:%@", Stack.pop);

The effect is as follows:

Algorithm-linked list implementation stack

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.