#include<stdio.h>
#include<stdlib.h>
typedef char DataType;
//单链表
typedef struct node {
DataType data;
struct node* next;
}ListNode;
typedef ListNode* LinkList;
//
//typedef struct node {
// DataType data;
// struct node* next;
//}*LinkList;
LinkList CreateListF() {
LinkList head;
ListNode* p;
char ch;
head = NULL;
ch = getchar();
while (ch != '\n')
{
p = (ListNode*)malloc(sizeof(ListNode));
p->data = ch;
p->next = head;
head = p;
ch = getchar();
}
return head;
}
LinkList CreateListR() {
LinkList head, rear;
ListNode* p;
char ch;
head = NULL; rear = NULL;
ch = getchar();
while (ch != '\n')
{
p = (ListNode*)malloc(sizeof(ListNode));
p->data = ch;
if (head == NULL)
head = p;
else
rear->next = p;
rear = p;//新的尾结点
ch = getchar();
}
if (rear != NULL) rear->next = NULL;//指针域置空
return head;
}
//插入带头结点的单链表尾
LinkList CreateListR1() {
LinkList head= (ListNode*) malloc(sizeof(ListNode));
ListNode* p,*r;
DataType ch;
r = head;
printf("输入abc");
while ((ch= getchar() )!='\n')
{
p = (ListNode*)malloc(sizeof(ListNode));
p->data = ch;
r->next = p;
r= p;//新尾
}
r->next = NULL;//更新最后空
return head;
}
//找某位置数据
ListNode* GetNode(LinkList head, int i) {
ListNode* p; int j;
p = head->next; j = 1;
while (p != NULL && j < i)
{
p = p->next; ++j;
}
if (j == i)
return p;
else
{
return NULL;
}
}
//找某值
ListNode* LocateNode(LinkList head, DataType k) {
ListNode* p;
p = head->next;
while (p && p->data != k)
{
p = p->next;
}
return p;
}
void InsertList(LinkList head, int i, DataType x) {
ListNode* p, * s;
int j;
p = head;//
j = 0;
while (p != NULL && j < i - 1)
{
p = p->next;
}
if (p == NULL) {
return;
}
else
{
s = (ListNode*)malloc(sizeof(ListNode));
s->data = x; s->next = p->next;
p->next = s;
}
}
//删除i
DataType DeleteList(LinkList head, int i) {
ListNode* p, * s;
DataType x; int j;
p = head; j = 0;
while (p != NULL && j < i - 1)
{
p = p->next; ++j;
}
if (p) {
s = p->next;
p->next = s->next;
x = s->data;
free(s);
return x;
}
exit(0);
}
//删除链表 某x
LinkList DeleteX(LinkList head, DataType x) {
ListNode* p, * q, * s;
p = head;
if (!p) {
printf("空链表");
exit(0);
}
q = p->next;
while (q != NULL)
{
if (q->data == x) {
s = q;
q = q->next;
free(s);
p->next = q;
}
else
{
p = q;
q = q->next;
}
}
return head;
}
//分离奇偶
void split(LinkList a, LinkList b) {
ListNode* p, * r, * s;
p = a->next;
r = a; s = b;
while (p != NULL)
{
r->next = p;
r = p;
p = p->next;
if (p != NULL) {
s->next = p;
s = p;
p = p->next;
}
}
r->next = s->next = NULL;
}
//合并两个
LinkList MergeList(LinkList a, LinkList b) {
LinkList c; ListNode* pa, * pb, * pc;
pa = a->next; pb = b->next;
c = pc = a;//a的头结点作为c 的头结点
while (pa != NULL && pb != NULL)
{
if (pa->data < pb->data) {
pc->next = pa; pc = pa;
pa = pa->next;
}
else
{
pc->next = pb; pc = pb;
pb = pb->next;
}
}
pc->next = pa != NULL ? pa : pb;
free(b);//释放b头结点
return c;
}
void PrintList(LinkList head,bool h) {
ListNode* p ;
if (h) {//包含头
p = head->next;
}
else {
p = head;
}
while (p != NULL)
{
printf("%c", p->data);
p = p->next;
}
printf("\n");
}
void PrintList(LinkList head) {
ListNode* p = head;
while (p!=NULL)
{
printf("%c", p->data);
p = p->next;
}
printf("\n");
}
int main() {
LinkList l = CreateListR1();// CreateListF();//创建链表
PrintList(l,true);
l=DeleteX(l, 'b');
PrintList(l,true);
}