传说中的数据结构(用的链表)
2014-01-16 10:20
302 查看
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node
{
int data;
struct node *next;
};
int main()
{
int n,i;
char ml[10];
struct node *head,*p,*q;
head=(struct node*)malloc(sizeof(struct node));
head->next=NULL;
while(scanf("%d",&n)!=EOF)
{
for(i=0;i<n;i++)
{
scanf("%s",ml);
if(strcmp(ml,"push")==0)
{
p=(struct node*)malloc(sizeof(struct node));
scanf("%d",&p->data);
p->next=head->next;
head->next=p;
}
else if(strcmp(ml,"pop")==0)
{
if(head->next==NULL)printf("erroe\n");
else
{
q=head->next;
head->next=q->next;
free(q);
p=head->next;
}
}
else if(strcmp(ml,"top")==0)
{
if(head->next==NULL)
printf("empty\n");
else printf("%d\n",p->data);
}
}
while(head->next!=NULL)
{
q=head->next;
head->next=q->next;
free(q);
}
p=head->next;
while(p)
{
printf("%d ",p->data);
p=p->next;
}
if(i==n)
printf("\n");
}
return 0;
}
#include <stdlib.h>
#include <string.h>
struct node
{
int data;
struct node *next;
};
int main()
{
int n,i;
char ml[10];
struct node *head,*p,*q;
head=(struct node*)malloc(sizeof(struct node));
head->next=NULL;
while(scanf("%d",&n)!=EOF)
{
for(i=0;i<n;i++)
{
scanf("%s",ml);
if(strcmp(ml,"push")==0)
{
p=(struct node*)malloc(sizeof(struct node));
scanf("%d",&p->data);
p->next=head->next;
head->next=p;
}
else if(strcmp(ml,"pop")==0)
{
if(head->next==NULL)printf("erroe\n");
else
{
q=head->next;
head->next=q->next;
free(q);
p=head->next;
}
}
else if(strcmp(ml,"top")==0)
{
if(head->next==NULL)
printf("empty\n");
else printf("%d\n",p->data);
}
}
while(head->next!=NULL)
{
q=head->next;
head->next=q->next;
free(q);
}
p=head->next;
while(p)
{
printf("%d ",p->data);
p=p->next;
}
if(i==n)
printf("\n");
}
return 0;
}
相关文章推荐
- C++实现数据结构中的单链表
- 数据结构基本算法:图的存储(以邻接链表为例)
- 数据结构--循环链表
- 数据结构--双向链表
- 算法导论 第三部分——基本数据结构——栈、队列、链表、散列表
- 数据结构 --- 链表
- 用邻接链表数据结构存储图 并实现Dijkstra算法
- C++ 数据结构应用——链表
- 数据结构第四周项目-建立单链表
- 15 API-集合(Collection(功能,迭代器),List(List特有迭代器,并发异常),常见数据结构图示(栈,队列,数组,链表))&对象数组
- Java数据结构复习——链表
- 【小镇的技术天梯】从头写数据结构,C语言实现双向链表
- (摘)面向对象c++数据结构之基本数据结构-链表-使用链表管理器(连载中)
- 数据结构---线性表----单链表结构与顺序存储结构的对比
- 数据结构系列之链表——双链表建立
- 数据结构之线性表--单链表(自带首尾转换)
- 数据结构-链表总结
- 不带头结点的单链表——数据结构课堂作业
- JavaScript数据结构——链表
- 数据结构学习笔记1-线性表(顺序表,单链表)