单链表操作1

#include <iostream>  
#include <stack>  
#include <vector>  
#include <deque> 
//#include <string.h>  
//#include <conio.h>  
//#include <stdio.h>
//#include <cstdlib>  
//#include <ctype.h>
//#include "stack.h"
//#include "stdafx.h" 
//#include <cstdlib>
using namespace std;




typedef struct student{
int num;
int score;
student *next;
}student;


//创建一个单向链表
student *creatstudent(){
student *p1, *p2, *head;
int num = 0, score = 0, n = 0;
head = NULL;
p1 = p2 = (student*)malloc(sizeof(student));
cout << "Input num:" << endl;
cin >> p1->num;
cout << "Input score:" << endl;
cin >> p1->score;
while (p1->num != 0 && p1->score != 0){
n++;
p2 = p1;
if (n == 1){
head = p1;
}
else{
p1 = (student*)malloc(sizeof(student));
p2->next = p1;
cout << "Input num:" << endl;
cin >> p1->num;
cout << "Input score:" << endl;
cin >> p1->score;
}
}
p2->next = NULL;
return head;
delete p1, p2;
}


// 打印单向链表
void print(student *head){
student *p = head;
while (p != NULL){
cout << p->num << endl;
cout << p->score << endl;
p = p->next;
}
}


//查找到某一个数并删除
student *deleteNodes(student *head, int n){


student *p1, *p2;
p1 = p2 = head;
if (head == NULL){
return head;
}
else{
if (head->num == n){
head = head->next;
delete p1;
return head;
}
else{
p1 = p1->next;
while (p1->num != n && p1->next != NULL){
p2 = p1;
p1 = p1->next;
}
if (p1->num == n){
p2->next = p1->next;
delete p1;
return head;
}
else{
cout << "Not found!" << endl;
return head;
}
}


}


}




//链表按学号从小到大排好,现在插入一个数据
student *insertNodes(student *head, int n, int score){
student *p1, *p0, *p2;
p1 = p2 = head;
p0 = (student *)malloc(sizeof(student));
p0->num = n;
p0->score = score;
if (head == NULL){
head = p0;
head->next = NULL;
return head;
}
else{


if (p1->num >= n){
p0->next = head;
head = p0;
return head;
}
p1 = p1->next;
while (p1 != NULL && p1->num < n){
p2 = p1;
p1 = p1->next;
}


if (p1->num >= n)
{
p0->next = p1;
p2->next = p0;
return head;
}
else
{
p2->next = p0;
p0->next = NULL;
return head;
}
}


}


//打印反转链表并不反转链表
void printInveserly(student *head){


stack<student *> nodes;
student *p = head;
while ( p!=NULL)          
{
nodes.push(p);
p = p->next;
}
while (!nodes.empty())
{
p = nodes.top();
cout << p->num << endl;
cout << p->score<<endl;

nodes.pop();
}
}


//反转链表
student * Inveserly(student *head){
stack<student *> nodes;
student *p,*p1;
p= head;
while (p != NULL)
{
nodes.push(p);
p = p->next;
}
if (!nodes.empty()){
head=p= nodes.top();
nodes.pop();
}
while (!nodes.empty())
{
p1 = nodes.top();
p->next = p1;
p = p1;
nodes.pop();
}
p->next = NULL;
return head;
}


void main(){
student *list = creatstudent();
cout << "链表:" << endl;
print(list);
list = deleteNodes(list, 5);
cout << "删除后的链表:" << endl;
print(list);
list = insertNodes(list, 2, 20);
cout << "插入后的链表:" << endl;
print(list);
cout << "反转打印链表:" << endl;
printInveserly(list);
cout << "反转链表:" << endl;
Inveserly(list);
print(list);
system("pause");
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值