// List.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
//定义链表结构
typedef struct tagNode{
int data;
struct tagNode *next;
}Node, *pNode;
pNode CreateList();
void printList(pNode head);
pNode reverseList(pNode head);
void deleteList(pNode head, int data);
void insertList(pNode head, pNode ihead, int pos);
void insertList2(pNode head, int data, int pos);
void del(pNode head, int pos);
int main(int argc, char* argv[])
{
pNode head = CreateList();
printList(head);
reverseList(head);
printList(head);
//deleteList(head, 5);
//printList(head);
pNode q = (pNode)malloc(sizeof(struct tagNode));
q->data = 100;
q->next = NULL;
insertList(head, q, 2);
printList(head);
insertList2(head, 200, 3);
printList(head);
del(head, 2);
printList(head);
return 0;
}
//创建一个链表,输入-1时结束
pNode CreateList()
{
pNode head = (pNode)malloc(sizeof(struct tagNode));
head->data = -1;
head->next = NULL;
pNode p=head, q=NULL;
int temp;
scanf("%d", &temp);
while (temp!=-1)
{
q = (pNode)malloc(sizeof(struct tagNode));
q->data = temp;
q->next = NULL;
p->next = q;
p = q;
scanf("%d", &temp);
}
return head;
}
//链表遍历
void printList(pNode head)
{
pNode pTemp = head->next;
while (NULL != pTemp)
{
printf("%d ", pTemp->data);
pTemp = pTemp->next;
}
printf("\n");
}
//链表逆序
pNode reverseList(pNode head)
{
if (NULL == head->next || NULL == head->next->next)//链表为空或者只有一个结点
{
return head;
}
pNode p=head->next, q=head->next->next, temp;
while (q != NULL)
{
temp = q->next;
q->next = p;
p = q;
q = temp;
}
head->next->next = NULL;//设置新的链表尾
head->next = p;//调整新的链表头
return head;
}
//删除指定数据的第一个结点
void deleteList(pNode head, int data)
{
pNode currNode = head;//首结点
pNode backNode = head;
while (currNode)
{
if (currNode->data == data)
{
backNode->next = currNode->next;
free(currNode);
currNode = NULL;
break;
}
else
{
backNode = currNode;
currNode = currNode->next;
}
}
}
/**************************************************************************************
del
*函数功能:删除链表中指定位置的元素
*输入:head链表头指针,pos被删除元素位置
*返回值:被删除元素中的数据域.如果删除失败返回-1
**************************************************/
void del(pNode head, int pos)
{
pNode pTemp = head->next, lTemp=NULL;
for (int i=0; i<pos; i++)
{
if (pTemp == NULL)return;
lTemp = pTemp;
pTemp = pTemp->next;
}
lTemp->next = pTemp->next;
free(pTemp);
}
//从指定位置插入结点,如果指定位置大于结点个数,则在链表尾部插入
void insertList(pNode head, pNode ihead, int pos)
{
pNode pTemp = head->next, lTemp=NULL;
while (pTemp)
{
if (--pos==0)
{
ihead->next = pTemp->next;
pTemp->next = ihead;
return;
}
lTemp = pTemp;
pTemp = pTemp->next;
}
lTemp->next = ihead;
}
/**************************************************************************************
insertList2
*函数功能:在链表中插入元素.
*输入:head链表头指针,data新元素中的数据域内容,pos新元素插入位置
*************************************************************************************/
void insertList2(pNode head, int data, int pos)
{
pNode pTemp = head->next;
for (int i=0; i<pos; i++)
{
if (pTemp==NULL)return;
if (i<pos-2)
pTemp = pTemp->next;
}
pNode pTemp2 = (pNode)malloc(sizeof(struct tagNode));
pTemp2->data = data;
pTemp2->next = pTemp->next;
pTemp->next = pTemp2;
}