单链表代码

 

#include<bits/stdc++.h> 

using namespace std;

typedef struct Node{
	int data;
	struct Node* next;
}*Linklist,Node;
int length;//表长 

void PrintList(Linklist L){//输出链表 
	while(L->next != NULL){
		cout << L->next->data << " ";
		L = L->next; 
	}
	cout << endl;
}

bool CreateHeadInsert(Linklist& L){//头插建立链表法 
	length = 0;
	L = (Node*)malloc(sizeof(Node));//创建头节点 
	if(L == NULL) return false;
	L->next = NULL;
	int c;
	scanf("%d",&c);
	while(c != -1){
		Node *p = (Node*)malloc(sizeof(Node));//申请一个结点 
		p->data = c;  
		p->next = L->next; //先把头结点后面的结点和p结点连接起来(第一次插入则指向空) 
		L->next = p;//连接头结点和p 
		length ++; 
		scanf("%d",&c); 
	}
	return true;
	
} 

bool CreateTailInsert(Linklist& L){//尾插建立链表法 
	length = 0;
	L = (Node*)malloc(sizeof(Node));
	if(L == NULL) return false;
	L->next = NULL;
	Node* r = L;
	int c;
	scanf("%d",&c);
	while(c != -1){
		Node *p = (Node*)malloc(sizeof(Node));//申请一个结点 
		p->data = c;
	//	p->next = NULL; 指针置空 ,最后一个置空即可 
		r->next = p;
		r = p;
		scanf("%d",&c);
	}
	r->next = NULL; 
	return true; 
} 
Node* GetElem(Linklist L, int side){//查找指定位序号的值,并返回其结点地址; 
	int i = 0;
	Node* s = L;
	while(s->next !=NULL && i < side){ //查找到side-1这个位置 并判断是否非法(s->next !=NULL判断最大位置 i < side-1判断最小位置) 
		s = s->next;	
		i++;
	}
	if(i == side) return s;
	else return NULL; 
} 

Node* LocatedElem(Linklist L, int data){//按值查找 
	Node* s = L;
	while(s->next !=NULL && s->data != data){ //查找到值为data这个结点  
		s = s->next;	
	}
	return s; //不用判断是否 s->data == data 因为最后一个指针为空 

} 

bool ListInsert(Linklist& L, int side, int data){//按位序插入 
	Node* s = GetElem(L, side-1);
	if(s){
		Node* p = (Node*)malloc(sizeof(Node));
		p->data = data;
		p->next = s->next;//先把p和第i个结点连接起来 
		s->next = p; //把i-1个结点和p连接起来 
		length++;
		return true;
	}
	else return false;
	
}

bool ListFrontInsert(Node* S, int data) {//在已知节点S前插入一个结点 方法1 找到S前驱结点再插入O(n) 方法2 改为尾插;
	 Node* p = (Node*)malloc(sizeof(Node));
	 if(p == NULL) return false;
	 p->next = S->next; //再S后面插入p结点 
	 S->next = p;
	 p->data = S->data;// 把S结点的值放置p结点 
	 S->data = data;// S结点存放要插入的值 
}

bool ListDelete(Linklist& L, int side){//删除指定位序结点 
	int i = 0;
	Node* s = GetElem(L, side-1);
	if(s){
		Node* p = s->next; 
		s->next = s->next->next;
		free(p);
		length--; 
		return true; 
	}
	else return false;
	
}  

bool ListFrontDelete(Node* S) {//删除已知结点 S    方法1 找到S结点再删除O(n) 方法2 改为尾删(对于最后一个结点会出错);

   	 Node* p = S->next; //如果是最后一个元素  S->next是NULL 那么无下一条语句 下下句的p->data就会出现空指针错误 
   	 if(p == NULL) return false;
 	 S->data = p->data;//把后继结点的值赋给S结点  从而等同把S结点的值删除    
	 S->next = S->next->next;//删除S结点的后继结点 
	 free(p);
	 length--; 
	 return true; 

}



int main(){
	Linklist L;
//	if (CreateHeadInsert(L)) PrintList(L); 
	if (CreateTailInsert(L)) PrintList(L); 
//	cout << ListInsert(L, 1, 999) << endl;
//	PrintList(L);
//	cout << ListDelete(L, 4) << endl;
//	PrintList(L);
//	Node* p = GetElem(L, 3);
//	ListFrontDelete(p);
//	PrintList(L);
	cout << LocatedElem(L, 1)->data << endl;
	 
}

/*
1 2 3 -1
*/

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值