您的位置:首页 > 编程语言 > C语言/C++

C++链表的创建和插入删除的实现

2016-05-02 12:23 597 查看
使用C++创建链表一直是很头疼的事,创建链表为了后面的操作方便,我们需要返回头指针,有了头指针就可以做后面的操作了,比如显示列表,增删改查等等,在create函数里首先申明两个指针,一个是用来保存上一个节点的地址P2,一个用来保存新创建节点的地址P1,当我们只创建了一个节点是,那么此时head指针和p1, p2都指向了这一个节点,也就是他们保存的地址是一样的,如果一个节点也没创建呢,就是将新创建出节点的地址P1删除掉,p2设置为空,p2的next设置为空,head设置为空,并且返回head指针,此时相当于什么也没操作;当创建两个以上的节点时,就是将上一个节点的next指向新节点,p2->next
= p1; p2 = p1;这样循环往复的使用,知道出现图书编号为零的情况,那么此时将新创建的节点p1删掉,将p2->next = NULL即可,同时返回头指针,这样三种创建情况都有了。同时里面有个check字符串的函数,如果是字符串的那么直接就推出了,必须是数字才行,于是有用到了C++标准库函数atoi,atof,在code::blocks里要想用这两个函数必须#include <stdlib.h>和<stdio.h>这两个库才行。

创建好了,就要显示我们创建的列表了,只需要王函数里传递链表的头指针就行,以此head表示当前节点,head->next表示下一个节点,以此显示他们的标号和价格就行。

删除节点分两种情况,一种是删除头节点,那么将头指针指向下一个节点就行,如果是删除中间的节点,需要将要删除节点的前一个节点的next指向要删除节点的下一个节点就行了,然后再head = head->next这里的head表示的是当前节点,不是头节点。

插入节点,往头部插,尾部插,中间插几种情况,头部的话就是将head指针指向新节点,新节点的next指向原来的头节点,尾部的话就是将原来的尾节点的next指向新节点,新节点的next设置为NULL即可,关键是中间节点的插入,就是将前一个节点的next指向新节点,新节点的next指向原来节点的下一个节点,具体代码如下:

#include <iostream>
#include <string>
#include <stdlib.h>
#include <stdio.h>

using namespace std;

class Book
{
public:
int num;
float price;
Book *next;
};

Book *head = NULL;

bool check(string str)
{
for (int i = 0; i< int(str.length()); i++)
{
if((str[i] > '9'|| str[i] < '0')&&(str[i] != '.'))
return false;
}
return true;
}

Book *create()
{
Book *p1, *p2;
p1 = new Book;
head = p1;
p2 = p1;
cout << "input book num, end with 0" << endl;
string str;
cin >> str;
while(!check(str))
{
cout << "input number, end with 0" << endl;
cin >> str;
}
p1 -> num = atoi(str.c_str());
if(p1 -> num != 0)
{
cout << "input book price" << endl;
string str;
cin >> str;
while(!check(str))
{
cout << "input number, end with 0" << endl;
cin >> str;
}
p1 -> price = atof(str.c_str());
}
else
{
delete p1; p2 = NULL; p2 -> next = NULL; head = NULL; return head;
}
while(p1->num != 0)
{
p2 = p1;
p1 = new Book;
cout << "input book num, end with 0" << endl;
string str;
cin >> str;
while(!check(str))
{
cout << "input number, end with 0" << endl;
cin >> str;
}
p1 -> num = atoi(str.c_str());
if(p1 -> num != 0)
{
cout << "input book price" << endl;
string str;
cin >> str;
while(!check(str))
{
cout << "input number, end with 0" << endl;
cin >> str;
}
p1 -> price = atof(str.c_str());
}
p2 -> next = p1;

}
delete p1;
p2 -> next = NULL;
return head;
}

void showBook(Book *head)
{
cout << endl;
cout << "Book information:" << endl;
while(head)
{
cout << "book num: "<< head -> num <<"\t";
cout << "book price: " << head -> price << endl;
head = head -> next;
}
}

void deleteBook(Book *head, int num)
{
Book *p;
if(head -> num == num)
{
p = head;
head = head -> next;
::head = head;
delete p;
cout << "delete successfully" << endl;
}
while(head)
{
if(head -> next == NULL)
{
cout << "not found num" << endl;
return;
}
if(head -> next -> num == num)
{
p = head -> next;
head -> next = p -> next;
delete p;
cout << "delete successfully" << endl;
return;
}
head = head -> next;
}
cout << "not found num" << endl;
}

void insertBook(Book *head, int num, float price)
{
Book *p = NULL;
p = new Book;
Book *l;
p -> num = num;
p -> price = price;
while(head)
{
l = head;
head = head -> next;
}
l -> next = p;
p -> next = NULL;
}

void insertHeadBook(Book *head, int num, float price)
{
Book *p = new Book;
p -> num = num;
p -> price = price;
if(num < head -> num)
{
p -> next = head;
::head = p;
return;
}
Book *temp = NULL;
while((num > head -> num) && (head -> next != NULL))
{
temp = head;
head = head -> next;
}
if(num > head -> num)
{
head -> next = p;
}
else
{
temp -> next = p;
p -> next = head;
}

}

int main()
{
head = create();
showBook(head);
cout << "input delete book num" << endl;
int num;
cin >> num;
deleteBook(head, num);
cout << "after delete book list:" << endl;
showBook(head);
cout << "input new nook num and price" << endl;
int bookNum;
float bookPrice;
cin >> bookNum;
cin >> bookPrice;
insertHeadBook(head, bookNum, bookPrice);
cout << "after insert new book" << endl;
showBook(head);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: