棧實現反序讀檔案

wzm10455發表於2012-12-13

這個程式時是要在終端下面執行的,編譯程式之後:輸入: ./a.out  /etc/passwd,就能將passwd檔案倒著輸出來
/*
 * func.c
 *
 *  Created on: 2012-12-12
 *      Author: wzm
 */
#include"head.h"
void Initialize(Pstack myStack)
{
    bom=NULL;
    top=NULL;
    ssize=0;
}
int Pushstack(Pstack myStack,char *a)//客棧的遍歷最好從上為往下遍歷
{
    Pnode new_node=(Pnode)malloc(sizeof(Pnode));//無論是連結串列還是客棧,首先做的是分配記憶體
    strcpy(new_node->a,a);
    new_node->next=NULL;//它總是指向客棧的頂端
    if(ssize==0)
    {
        bom=new_node;
        top=new_node;

    }
    else
    {
        new_node->next=top;//這個的意思是向下指
        top=new_node;
    }
    ssize+=1;
    return ssize;
}
void Popstack(Pstack myStack)
{
    Pnode node=top;
    if(node==bom)
    {
        top=NULL;
        bom=NULL;
        puts("客棧為空,無需彈出");
        printf("ssize=%d\n",ssize);
        return;
    }
    else
    {
        top=node->next;
        //free(node->data);
        //free(node->d);
        free(node);
        printf("ssize=%d\n",ssize);
        ssize--;
    }
}
void Display(Pstack myStack)
{
    Pnode tmp=top;
    if(ssize==0)
    {
        puts("空的!");
        return;
    }
    else
    {
        do
        {
            printf("data=%s\n",tmp->a);
            tmp=tmp->next;//從上往下執行的
        }while(tmp!=bom);
    }
}
void Destorystack(Pstack myStack)
{
    Pnode node=top;
    if(ssize==0)
    {
        top=NULL;
        bom=NULL;
        puts("客棧為空,無需銷燬");
        printf("ssize=%d",ssize);
        return;
    }
    else
    {
        while(ssize!=0)
        {
            Popstack(myStack);
        }
    }
    return;
}




#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define bom myStack->bottom
#define top myStack->ttop
#define ssize myStack->stack_size
typedef struct element
{
    char a[1024];
    struct element *next;
}*Pnode;

typedef struct stack_element
{
    struct stack_element *bottom;
    unsigned int stack_size;//記錄元素個數
    struct stack_element *ttop;
}*Pstack;
void Initialize(Pstack);
int Pushstack(Pstack,char *);
void Popstack(Pstack);
void Display(Pstack);
void Destorystack(Pstack);

/*
 ============================================================================
 Name        : stack.c
 Author      : 
 Version     :
 Copyright   : Your copyright notice
 Description : Hello World in C, Ansi-style
 ============================================================================
 */

#include <stdio.h>
#include <stdlib.h>
#include"head.h"
int tac(FILE *ffp,Pstack myStack)
{
	int sum=ssize;
	char s[1024];
	while(fgets(s,1024,ffp)!=NULL)
	{
		Pushstack(myStack,s);
	}
	Display(myStack);
	fclose(ffp);
	return sum;
}
int main(int argc,char *argv[]) {
	//Pstack my=(Pstack)malloc(sizeof(Pstack)*2);//這裡如果用的是指標,則先得先分配空間
	struct stack_element my;//如果不是指標,就不需要
	Initialize(&my);
	if(argc!=2)
	{
		fprintf(stderr,"Usage conmmand\n");
		return -1;
	}
	FILE *fp;
	fp=fopen(argv[1],"r");
	if(fp==NULL)
	{
		//perror也是可以顯示檔案錯誤的
		fprintf(stderr,"Usage conmmand %s\n",argv[1]);
	}
	else
	{
		tac(fp,&my);
	}
	return EXIT_SUCCESS;
}
 /*
實現結果:
data=jetty:x:110:110::/usr/share/jetty:/sbin/nologin
data=wzm:x:1000:1
data=tcpdump:x:72
data=chrony:x:993
data=sshd:x:74:74
data=smmsp:x:51:5
data=mailnull:x:4
data=nm-openconne
data=gdm:x:42:42:
data=saslauth:x:9
data=openvpn:x:99
data=colord:x:997
data=pulse:x:998:
data=rtkit:x:172:
data=avahi:x:70:7
data=abrt:x:173:1
data=dbus:x:81:81
data=avahi-autoip
data=smolt:x:999:
data=usbmuxd:x:11
data=nobody:x:99:
data=ftp:x:14:50:
data=gopher:x:13:
data=games:x:12:1
data=operator:x:1
data=uucp:x:10:14
data=mail:x:8:12:
data=halt:x:7:0:h
data=shutdown:x:6
data=sync:x:5:0:s
data=lp:x:4:7:lp:
data=adm:x:3:4:ad
data=daemon:x:2:2
data=bin:x:1:1:bi */


相關文章