2010-11-27 82 views
1
#include<stdio.h> 
#include<conio.h> 
#include<alloc.h> 

struct node 
{ 
    int data; 
    struct node*link; 

}; 

void push(struct node*,int); 
int pop(struct node*); 
void delstack(struct node*); 

void main() 
{ struct node*s = NULL; 
    int i ; 

    clrscr(); 

    push(s,14); 
    push(s,-3); 
    push(s,18); 
    push(s,29); 
    push(s,31); 
    push(s,16); 

    i=pop(s); 
    printf("\n Item popped :%d",i); 

    i=pop(s); 
    printf("\n Item popped :%d",i); 

    i=pop(s); 
    printf("\n Item popped :%d",i); 

    delstack(s); 

    getch(); 

} 

// Add a new node to the stack at the top of the linked list 

void push(struct node*top,int item) 
{ struct node*temp; 
    temp=malloc(sizeof(struct node)); 

    if(temp==NULL) 
    printf("\n Stack is full"); 


    temp->data=item; 
    temp->link=top; 
    top=temp; 

} 

// Pops an element from the stack 

int pop(struct node*top) 
{ struct node*temp; 
    int item; 
    if(top==NULL) 
    { 
    printf("\n Stack is empty "); 
    return NULL; 
    } 
    temp=top; 
    item=temp->data; 
    top=top->link; 

    free(temp); 
    return item; 
} 

// Deallocates memory 

void delstack(struct node*top) 
{ 
    struct node*temp; 

    if(top==NULL) 
    return; 


    while(top!=NULL) 
    { 
    temp=top; 
    top=top->link; 
    free(temp); 

    } 
} 

我的問題主要在於指針的使用。這是錯誤的程序,正確的用法是使用雙指針頂部。 但我不明白的是'是否有可能實現它沒有雙指針。如果不是,那麼爲什麼?爲什麼我們需要一個指針指向這裏的指針。實現堆棧作爲鏈表。問題在指針

正確的程序是

#include<stdio.h> 
#include<conio.h> 
#include<alloc.h> 

struct node 
{ 
    int data; 
    struct node*link; 

}; 

void push(struct node**,int); 
int pop(struct node**); 
void delstack(struct node**); 

void main() 
{ struct node*s = NULL; 
    int i ; 

    clrscr(); 

    push(&s,14); 
    push(&s,-3); 
    push(&s,18); 
    push(&s,29); 
    push(&s,31); 
    push(&s,16); 

    i=pop(&s); 
    printf("\n Item popped :%d",i); 

    i=pop(&s); 
    printf("\n Item popped :%d",i); 

    i=pop(&s); 
    printf("\n Item popped :%d",i); 

    delstack(&s); 

    getch(); 

} 

// Add a new node to the stack at the top of the linked list 

void push(struct node**top,int item) 
{ struct node*temp; 
    temp=malloc(sizeof(struct node)); 

    if(temp==NULL) 
    printf("\n Stack is full"); 


    temp->data=item; 
    temp->link=*top; 
    *top=temp; 

} 

// Pops an element from the stack 

int pop(struct node**top) 
{ struct node*temp; 
    int item; 
    if(*top==NULL) 
    { 
    printf("\n Stack is empty "); 
    return NULL; 
    } 
    temp=*top; 
    item=temp->data; 
    *top=(*top)->link; 

    free(temp); 
    return item; 
} 

// Deallocates memory 

void delstack(struct node**top) 
{ 
    struct node*temp; 

    if(*top==NULL) 
    return; 


    while(*top!=NULL) 
    { 
    temp=*top; 
    *top=(*top)->link; 
    free(temp); 

    } 
} 
+0

小建議,工作在你的編碼風格,這意味着,使用更多的空間,不要把東西與塊分隔符同一行。 – 2010-11-27 19:58:27

回答

4

如果你沒有通過一個指針的指針,這將永遠無法創建或刪除列表中的第一個節點。這需要改變列表指針的值。

一個常見的做法是創建一個結構,該結構包含一個始終存在的列表頭,即使該列表爲空也是如此。

struct Node { 
    int data; 
    struct Node *next; 
}; 

struct List { 
    struct Node *first; 
}; 

現在您只需創建一個List結構並將其傳遞給push和pop。你也可以投入一些方便的元素,如彈出和推送更新的列表長度。

試試看看是否有幫助。它將在「之前」打印。參數被複制,所以在函數中改變它並不會在調用函數中改變它。指向你的列表的頭指針也是一樣的。

#include <stdio.h> 

void func(char *s) { 
    s="after"; 
} 

int main(int argc, char *argv[]) { 
    char *s="before"; 
    func(s); 
    printf("%s\n", s); 
} 
+0

請詳細說明。 Iam只是不能明白:( – YuNo 2010-11-27 20:11:58

0

在pop或push操作之後的堆棧中,隨着堆棧頂部的元素髮生更改,我們需要更改堆棧頂部指針的值。

通過您的代碼節點* s表示堆棧的頂部。

考慮的推送功能,

void push (node * top , int val) 
{ 
    // formal parameter : node * top , int val (let us assume the names) 

    // Here we should create a new node (say new_node) as 

    node * new_node = (node *) malloc (sizeof(node)) ; 

    new_node->data = val ; 
    new_node->link = top ; 

    // now we need to set the top of the stack to this newly created node. 

    top = new_node ; 

} 

但是頂部是形式參數這種變化不會反映在實際的參數s和儘快功能範圍退出死亡。

因此,如果我們已經通過堆棧頭的位置(如& s),則更改可能已在此位置進行並因此反映出來。

類似的邏輯適用於使用pop()

:)