The LinkStNode for Linked stack | Data

简介: The code in Data book (5th Edition) from the 83 page to 86 page

The code in Data book (5th Edition) from the 83 page to 86 page

Update completed
#define MaxSize 50
typedef int ElemType;

typedef struct linknode {
    ElemType data;//Data domain
    struct linknode *next;//Pointer domain
} LinkStNode;

//Initialize the stack
void InitStack(LinkStNode *&s) {
    s=(LinkStNode *)malloc(sizeof(LinkStNode));
    s->next=NULL;
}

//Create linked stack
void CreateLinkStack(LinkStackNode *&L, ElemType a[], int n) {
    LinkStackNode *s, *r;
    L = (LinkStackNode *)malloc(sizeof(LinkStackNode));   //Create head node
    r = L;  //r point to tail node
    for (int i = 0; i < n; i++) {   //Loop create data node
        s = (LinkStackNode *)malloc(sizeof(LinkStackNode));
        s->data = a[i]; //Create node s
        r->next = s; //Insert node s to end of node r
        r = s;
    }
    r->next = NULL; //Set tail node is NULL
}

//Destroy linked stack
void DestroyStack(LinkStNode *&s) {
    LinkStNode *p=s, *q=s->next;
    while (q!=NULL) {
        free(p);
        p=q;
        q=p->next;
    }
    free(p);    //Free up its space
}

//Into stack
void Push(LinkStNode *&s,ElemType e) {
    LinkStNode *p;
    p=(LinkStNode *)malloc(sizeof(LinkStNode));
    p->data=e; //Create node p of elements e
    p->next=s->next;
    s->next=p;
}

//Out stack
bool Pop(LinkStNode *&s,ElemType &e) {
    LinkStNode *p;
    if (s->next==NULL)    //When stack is NULL
    return false;
    p=s->next; //p point to start node
    e=p->data;
    s->next=p->next;
    free(p); //Release p
    return true;
}

//Top element of the stack
bool GetTop(LinkStNode *s,ElemType &e) {
    if (s->next==NULL)    //When stack is NULL
        return false;
    e=s->next->data;
    return true;
}

//Output linked stack
void ShowLinkStack(LinkStackNode* L) {
    LinkStackNode* p = L->next;  //p point to head node
    while (p != NULL) { //When p no NULL, output data of node p
        printf("%d ", p->data);
        p = p->next;    //Move p to next node
    }
    printf("\n");
}
如有侵权,请联系作者删除
目录
相关文章
LeetCode 203. Remove Linked List Elements
删除链表中等于给定值 val 的所有节点。
80 0
LeetCode 203. Remove Linked List Elements
Data Structures (三) - 栈Stack实现
Data Structures (三) - 栈Stack实现
Data Structures (三) - 栈Stack实现
|
算法
[LeetCode]--203. Remove Linked List Elements
Remove all elements from a linked list of integers that have value val. Example Given: 1 –&gt; 2 –&gt; 6 –&gt; 3 –&gt; 4 –&gt; 5 –&gt; 6, val = 6 Return: 1 –&gt; 2 –&gt; 3 –&gt; 4 –&gt;
1051 1
|
DataX 程序员 算法
Single linked list by pointer
其实本应该从一般性的表讲起的,先说顺序表,再说链表 。但顺序表的应用范围不是很广,而且说白了就是数组的高级版本,他的优势仅在于两点:1.逻辑直观,易于理解。2.查找某个元素只需要常数时间——O(1),而与此同时,因为每个单元的物理内存都是连续的,所以不便于移动,不便于精细化操作,每次插入和删除都会带来巨额的时间开销。
1045 0
|
算法
[LeetCode]--237. Delete Node in a Linked List
Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. Supposed the linked list is 1 -&gt; 2 -&gt; 3 -&gt; 4 and you are given the thi
1328 0
[LeetCode] Populating Next Right Pointers in Each Node
The idea is similar to a level-order traversal and remember to take full advantages of the prefect binary tree assumption in the problem statement.
794 0
[LeetCode] Populating Next Right Pointers in Each Node II
The problem becomes more difficult once the binary tree is not perfect. The idea is still similar to use a level-order traversal.
825 0