链表 栈 队列

链表

描述
给定单链表头结点,删除链表中倒数第k个结点。
输入
每组数据有2行:
第一行两个整数K,N表示有N个数据,请删除倒数第K个数据
第二行包含N个数据,每个数据之间包含一个空格。
以EOF结束。
输出
输出删除后的链表元素,数据之间包含一个空格。如果删除后链表为空,输出空行。
样例输入
1 3
1 2 3
样例输出
1 2

题解:链表的使用,记录一下

#include
#include

typedef struct node{
    int data;
    struct node* next;
}node;

node *creat(int n){
    node *p = (node *)malloc(sizeof (node));
    p->next = NULL;
    node *head = p;
    int i;
    for( i=0; i<n; i++){
        p->next = (node *)malloc(sizeof (node));
        p = p->next;
        scanf("%d",&p->data);
    }
    p->next = NULL;
    return head;
}

void print(node* head){
    head = head -> next;
    int flag = 0;
    while(head!=NULL){
        if(flag) printf(" ");
        flag = 1;
        printf("%d",head->data),head = head->next;
    }
    printf("\n");
}

void Delate(node *head,int n,int k){
    int i;
    for(i=0; i < (n-k); i++)
        head = head->next;
    node *p = head->next->next;
    head->next = p;
}

void Free(node *head){
    while(head != NULL){
        node *p = head;
        head = head->next;
        free(p);
    }
}
int main(){
    int k,n;
    while(scanf("%d%d",&k,&n)!=EOF){
        node *head = creat(n);
        Delate(head,n,k);
        print(head);
        Free(head);
    }
}

#include
#include
#include

#define Elemtype int

typedef struct SqStack{
    int size;
    int top;
    Elemtype *base;
}SqStack;

void InitStack(SqStack *s)
{
    s->size = 500;
    s->base = (Elemtype*)malloc(sizeof(Elemtype)*s->size);
    s->top = -1;
}

void Destroy(SqStack *s)
{
	free(s->base);
}

void Push(SqStack *s,int x){
    if(s->top == -1){
        s->base[0] = x;
        s->top = 1;
    }
    else{
        int i;
        for(i = s->top; i > 0; i--)
            s->base[i] = s->base[i-1];
        s->base[0] = x;
        s->top++;
    }
}

int GetTop(SqStack s,int *x){
    if(s.top == -1) return 0;
    else{
        *x = s.base[0];
        return 1;
    }
}

int Pop(SqStack *s,int *x){
    if(s->top == -1) return 0;
    else{
        s->top--;
        *x = s->base[0];
        int i;
        for(i=0; i < s->top ;i++)
            s->base[i] = s->base[i+1];
        if(s->top == 0) s->top = -1;
        return 1;
    }
}
int main()
{
	SqStack s;
	InitStack(&s);
	char cmd[10];
	int x, res;
	while(scanf("%s", cmd)!=EOF)
	{
		if(strcmp(cmd, "push")==0)
		{
			scanf("%d", &x);
			Push(&s, x);
		}
		else if(strcmp(cmd, "top")==0)
		{
			res = GetTop(s, &x);
			if(res==0)
				printf("EMPTY\n");
			else
				printf("%d\n", x);
		}
		else
		{
			res = Pop(&s, &x);
			if(res==0)
				printf("EMPTY\n");
			else
				printf("%d\n", x);
		}
	}
	Destroy(&s);
	return 0;
}

队列

#include
#include
#include

typedef struct SqQueue{
    int *data;
    int cap;
    int size;
}SqQueue;

void Destroy(SqQueue* q)
{
	free(q->data);
}

void InitQueue(SqQueue* q){
    q->cap = 1005;
    q->size = 0;
    q->data = (int *)malloc(sizeof (int) * q->cap);
}

void EnQueue(SqQueue* q,int x){
    q->data[q->size++] = x;
}

int Empty(SqQueue* q){
    if(q->size == 0) return 1;
    return 0;
}

int Front(SqQueue* q){
    return q->data[0];
}

int Back(SqQueue* q){
    return q->data[q->size-1];
}

int DeQueue(SqQueue* q){
    int x = q->data[0],i;
    for(i = 0; i < q->size-1; i++)
        q->data[i] = q->data[i+1];
    q->size--;

    return x;
}
int main()
{
	SqQueue q;
	InitQueue(&q);
	char cmd[10];
	int x, res;
	while(scanf("%s", cmd)!=EOF)
	{
		if(strcmp(cmd, "enq")==0)
		{
			scanf("%d", &x);
			EnQueue(&q, x);
		}
		else if(strcmp(cmd, "front")==0)
		{
			if(Empty(&q))
				printf("EMPTY\n");
			else
                printf("%d\n", Front(&q));
		}
		else if(strcmp(cmd, "back")==0)
		{
			if(Empty(&q))
				printf("EMPTY\n");
			else
                printf("%d\n", Back(&q));
		}
		else
		{
			if(Empty(&q))
				printf("EMPTY\n");
			else
                printf("%d\n", DeQueue(&q));
		}
	}
	Destroy(&q);
	return 0;
}

你可能感兴趣的:(数据结构)