#include<bits/stdc++.h>
#include<cstdlib>
using namespace std;
typedef int ElemType;
typedef struct Node
{
ElemType data;
struct Node *next;
}Node, *LinkedList;
//单链表的初始化
LinkedList LinkedListInit()
{
Node *L;
L = (Node *)malloc(sizeof(Node));
if (L == NULL)
cout << "申请内存空间失败" << endl;
L->next = NULL;
return L;
}
//单链表的建立,头插法
LinkedList LinkedListCreatH()
{
Node *L;
L = (Node *)malloc(sizeof(Node));
L->next = NULL;
ElemType x;
int temp = 1;
while (temp)
{
cin >> x;
if (x != 0)
{
Node *p;
p = (Node *)malloc(sizeof(Node));
p->data = x;
p->next = L->next;
L->next = p;
}
else
temp = 0;
}
return L;
}
//单链表的打印
void printList(Node *head)
{
if (head->next == NULL)
{
cout << "list is empty" << endl;
return;
}
Node *p = head->next;
int index = 0;
while (p != NULL)
{
cout << "第" << ++index << "个元素为:" << p->data << endl;
p = p->next;
}
}
//单链表的测长
int getListLength(Node *head)
{
int length = 0;
Node *p = head->next;
while (p != NULL)
{
++length;
p = p->next;
}
return length;
}
//单链表的建立2 尾插法
LinkedList LinkedListCreatT()
{
Node *L;
L = (Node *)malloc(sizeof(Node));
L->next = NULL;
Node *r;
r = L;
ElemType x;
int temp = 1;
while (temp)
{
cin >> x;
if (x != 0)
{
Node *p;
p = (Node *)malloc(sizeof(Node));
p->data = x;
r->next = p;
r = p;
}
else
temp = 0;
}
r->next = NULL;
return L;
}
//单链表的插入
Node *insertList(Node *head, int pos, int data)
{
Node * newNode = new Node;
newNode->data = data;
Node *p = head;
int index = 1;
while (p != NULL && index<pos)
{
p = p->next;
++index;
}
newNode->next = p->next;
p->next = newNode;
return head;
}
Node *deleteNode(Node *head, int pos)
{
Node *p = head;
if (p->next == nullptr)
{
cout << "链表为空" << endl;
return nullptr;
}
Node *node = nullptr;
int index = 1;
while (p != nullptr&&index < pos)
{
p = p->next;
++index;
}
if (p != nullptr&&p->next != nullptr)
{
node = p->next;
p->next = node->next;
delete node;
}
return head;
}
int main()
{
/* LinkedList list,start;
cout<<"请输入单链表的数据:";
list=LinkedListCreatH();
for(start=list->next;start!=NULL;start=start->next)
cout<<" "<<start->data;
cout<<endl;
cout<<"请输入单链表的数据:";
list=LinkedListCreatT();
for(start=list->next;start!=NULL;start=start->next)
cout<<" "<<start->data;
*/
LinkedList list;
list = LinkedListCreatH();
printList(list);
list = LinkedListCreatT();
printList(list);
int length = getListLength(list);
cout << "单链表的长度为: " << length << endl;
int pos, data;
cin >> pos >> data;
list = insertList(list, pos, data);
cout << "插入后的链表为: " << endl;
printList(list);
int pos1;
cin >> pos1;
list = deleteNode(list, pos1);
cout << "删除后的链表为: " << endl;
printList(list);
system("pause");
}
#include<cstdlib>
using namespace std;
typedef int ElemType;
typedef struct Node
{
ElemType data;
struct Node *next;
}Node, *LinkedList;
//单链表的初始化
LinkedList LinkedListInit()
{
Node *L;
L = (Node *)malloc(sizeof(Node));
if (L == NULL)
cout << "申请内存空间失败" << endl;
L->next = NULL;
return L;
}
//单链表的建立,头插法
LinkedList LinkedListCreatH()
{
Node *L;
L = (Node *)malloc(sizeof(Node));
L->next = NULL;
ElemType x;
int temp = 1;
while (temp)
{
cin >> x;
if (x != 0)
{
Node *p;
p = (Node *)malloc(sizeof(Node));
p->data = x;
p->next = L->next;
L->next = p;
}
else
temp = 0;
}
return L;
}
//单链表的打印
void printList(Node *head)
{
if (head->next == NULL)
{
cout << "list is empty" << endl;
return;
}
Node *p = head->next;
int index = 0;
while (p != NULL)
{
cout << "第" << ++index << "个元素为:" << p->data << endl;
p = p->next;
}
}
//单链表的测长
int getListLength(Node *head)
{
int length = 0;
Node *p = head->next;
while (p != NULL)
{
++length;
p = p->next;
}
return length;
}
//单链表的建立2 尾插法
LinkedList LinkedListCreatT()
{
Node *L;
L = (Node *)malloc(sizeof(Node));
L->next = NULL;
Node *r;
r = L;
ElemType x;
int temp = 1;
while (temp)
{
cin >> x;
if (x != 0)
{
Node *p;
p = (Node *)malloc(sizeof(Node));
p->data = x;
r->next = p;
r = p;
}
else
temp = 0;
}
r->next = NULL;
return L;
}
//单链表的插入
Node *insertList(Node *head, int pos, int data)
{
Node * newNode = new Node;
newNode->data = data;
Node *p = head;
int index = 1;
while (p != NULL && index<pos)
{
p = p->next;
++index;
}
newNode->next = p->next;
p->next = newNode;
return head;
}
Node *deleteNode(Node *head, int pos)
{
Node *p = head;
if (p->next == nullptr)
{
cout << "链表为空" << endl;
return nullptr;
}
Node *node = nullptr;
int index = 1;
while (p != nullptr&&index < pos)
{
p = p->next;
++index;
}
if (p != nullptr&&p->next != nullptr)
{
node = p->next;
p->next = node->next;
delete node;
}
return head;
}
int main()
{
/* LinkedList list,start;
cout<<"请输入单链表的数据:";
list=LinkedListCreatH();
for(start=list->next;start!=NULL;start=start->next)
cout<<" "<<start->data;
cout<<endl;
cout<<"请输入单链表的数据:";
list=LinkedListCreatT();
for(start=list->next;start!=NULL;start=start->next)
cout<<" "<<start->data;
*/
LinkedList list;
list = LinkedListCreatH();
printList(list);
list = LinkedListCreatT();
printList(list);
int length = getListLength(list);
cout << "单链表的长度为: " << length << endl;
int pos, data;
cin >> pos >> data;
list = insertList(list, pos, data);
cout << "插入后的链表为: " << endl;
printList(list);
int pos1;
cin >> pos1;
list = deleteNode(list, pos1);
cout << "删除后的链表为: " << endl;
printList(list);
system("pause");
}