栈的插入,删除,遍历操作

#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
typedef struct Node{
	int date;
	struct Node* next;
}NODE,*PNODE;
typedef struct strack{
	PNODE ptop;
	PNODE pbottom;
}STACK,*PSTACK;
void init(PSTACK s){
	
	s->ptop=(PNODE)malloc(sizeof(NODE));
	if(s->ptop==NULL){
		printf("分配失败");
		exit(-1); 
	}
	else{
		s->pbottom=s->ptop;
		s->pbottom->next=NULL;
	}
}
void push(PSTACK s,int x){
	 PNODE pNew=(PNODE)malloc(sizeof(NODE));
     pNew->date=x;
     pNew->next=s->ptop;
     s->ptop=pNew;
}
void pop(PSTACK s){
	s->ptop=s->ptop->next;
	
}
void traverse(PSTACK s){
	PNODE p=s->ptop;
	while(p!=s->pbottom){
		printf("%d ",p->date);
		p=p->next;
	}
	printf("\n");
	return;
//	for(PSTACK i=s->ptop;i!=NULL;i=i->ptop){
//		cout<<i->date;
	}
bool empty(PSTACK s){
		if(s->pbottom==s->ptop) return true;
		else return false;
	} 
	void clear(PSTACK s){
		//s->pbottom=s->ptop;
		PNODE p,q;
		p=s->ptop,q=NULL;
		while(p!=s->pbottom){
			q=p->next;
			free(p);
			p=q;
		}
		s->ptop=s->pbottom;
	}

int main(){
	STACK s;
	init(&s);  
    push(&s,3);
    push(&s,77);
    push(&s,55);
    pop(&s);
    traverse(&s);
    clear(&s);
    traverse(&s);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
链表是一种常见的数据结构,它由若干个节点组成,每个节点中包含了一个数据元素和一个指向下一个节点的指针。链表可以用来实现、队列等数据结构。 以下是链表的插入遍历、查找和删除操作的具体实现: 插入操作: 在链表中插入一个新的节点通常需要以下步骤: 1. 创建一个新的节点,赋值为要插入的数据。 2. 找到要插入的位置,即在哪两个节点之间插入新节点。 3. 将新节点的指针指向下一个节点,并将前一个节点的指针指向新节点。 代码实现: ```python class Node: def __init__(self, data=None): self.data = data self.next = None class LinkedList: def __init__(self): self.head = None def insert(self, data): new_node = Node(data) if self.head is None: self.head = new_node return current_node = self.head while current_node.next is not None: current_node = current_node.next current_node.next = new_node ``` 遍历操作遍历链表通常需要以下步骤: 1. 从头节点开始,依次遍历每个节点。 2. 对于每个节点,执行相应的操作,例如打印节点中的数据。 代码实现: ```python class LinkedList: def __init__(self): self.head = None ... def traverse(self): if self.head is None: print("Linked list is empty") return current_node = self.head while current_node is not None: print(current_node.data) current_node = current_node.next ``` 查找操作: 在链表中查找一个节点通常需要以下步骤: 1. 从头节点开始,依次遍历每个节点。 2. 对于每个节点,判断其包含的数据是否与要查找的数据相等。 3. 如果找到了相等的节点,返回该节点;否则,返回 None。 代码实现: ```python class LinkedList: def __init__(self): self.head = None ... def search(self, data): if self.head is None: return None current_node = self.head while current_node is not None: if current_node.data == data: return current_node current_node = current_node.next return None ``` 删除操作: 在链表中删除一个节点通常需要以下步骤: 1. 找到要删除的节点,即在哪两个节点之间删除节点。 2. 将前一个节点的指针指向后一个节点。 代码实现: ```python class LinkedList: def __init__(self): self.head = None ... def delete(self, data): if self.head is None: return if self.head.data == data: self.head = self.head.next return current_node = self.head while current_node.next is not None: if current_node.next.data == data: current_node.next = current_node.next.next return current_node = current_node.next ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值