#include "stdio.h"
#include "stdlib.h"
typedef int DataType;
typedef struct node
{
DataType data;
struct node *next;
}ListNode;
typedef ListNode *LinkList;
LinkList CreateList(void)//单链表的建立,从后向前生成
{
int m;
ListNode *q = NULL;
ListNode *s,*r;
printf("输入一组数(0结束):");
scanf("%d",&m);
while(m != 0)
{
s = (ListNode *)malloc(sizeof(ListNode));
if(q == NULL)
{
q = r = s;
q->data = m;
scanf("%d",&m);
continue;
}
s->data = m;
r->next = s;
r = s;
scanf("%d",&m);
}
r->next = NULL;
return q;
}
void PrintList(LinkList head)
{
ListNode *p;
for(p = head; p; p = p->next)
printf("%d ",p->data);
printf("\n");
}
LinkList GetNode(LinkList head,int i)//单链表的查找,查找第i个结点
{
int j = 1;
ListNode *p;
p = head;
while(p && j < i)
{
p = p->next;
j++;
}
if(j == i && p)
return p;
else
return NULL;
}
void InsertList(LinkList head,DataType x,int i)
{
ListNode *p,*s;
if(1 == i)
{
s = (ListNode *)malloc(sizeof(ListNode));
s->data = x;
s->next = head;
head = s;
}
else
{
p = GetNode(head,i - 1);
if(NULL == p)
{
printf("插入位置非法\n");
return head;
}
s = (ListNode *)malloc(sizeof(ListNode));
s->data = x;
s->next = p->next;
p->next = s;
}
return head;
}
void DeleteList(LinkList head,int i)
{
ListNode *p,*r;
if(1 == i)
{
r = head;
head = r->next;
free(r);
}
else
{
p = GetNode(head,i - 1);
if(p == NULL || p->next == NULL)
{
printf("删除位置非法\n");
exit(0);
}
r= p->next;
p->next = r->next;
free(r);
}
return head;
}
void main()
{
int i;
DataType key,x;
// LinkList head;
ListNode *p,*head;
head = CreateList();
PrintList(head);
printf("输入要查找的值的位置:");
scanf("%d",&i);
p = GetNode(head,i);
if(p == NULL)
printf("未找到\n");
else
printf("要查找的值为:%d \n",p->data);
printf("输入欲插入元素的位置:");
scanf("%d",&i);
printf("请输入欲插入的元素:");
scanf("%d",&x);
InsertList(head,x,i);
PrintList(head);
printf("请输入欲删除结点的位置:");
scanf("%d",&i);
DeleteList(head,i);
PrintList(head);
}
#include "stdlib.h"
typedef int DataType;
typedef struct node
{
DataType data;
struct node *next;
}ListNode;
typedef ListNode *LinkList;
LinkList CreateList(void)//单链表的建立,从后向前生成
{
int m;
ListNode *q = NULL;
ListNode *s,*r;
printf("输入一组数(0结束):");
scanf("%d",&m);
while(m != 0)
{
s = (ListNode *)malloc(sizeof(ListNode));
if(q == NULL)
{
q = r = s;
q->data = m;
scanf("%d",&m);
continue;
}
s->data = m;
r->next = s;
r = s;
scanf("%d",&m);
}
r->next = NULL;
return q;
}
void PrintList(LinkList head)
{
ListNode *p;
for(p = head; p; p = p->next)
printf("%d ",p->data);
printf("\n");
}
LinkList GetNode(LinkList head,int i)//单链表的查找,查找第i个结点
{
int j = 1;
ListNode *p;
p = head;
while(p && j < i)
{
p = p->next;
j++;
}
if(j == i && p)
return p;
else
return NULL;
}
void InsertList(LinkList head,DataType x,int i)
{
ListNode *p,*s;
if(1 == i)
{
s = (ListNode *)malloc(sizeof(ListNode));
s->data = x;
s->next = head;
head = s;
}
else
{
p = GetNode(head,i - 1);
if(NULL == p)
{
printf("插入位置非法\n");
return head;
}
s = (ListNode *)malloc(sizeof(ListNode));
s->data = x;
s->next = p->next;
p->next = s;
}
return head;
}
void DeleteList(LinkList head,int i)
{
ListNode *p,*r;
if(1 == i)
{
r = head;
head = r->next;
free(r);
}
else
{
p = GetNode(head,i - 1);
if(p == NULL || p->next == NULL)
{
printf("删除位置非法\n");
exit(0);
}
r= p->next;
p->next = r->next;
free(r);
}
return head;
}
void main()
{
int i;
DataType key,x;
// LinkList head;
ListNode *p,*head;
head = CreateList();
PrintList(head);
printf("输入要查找的值的位置:");
scanf("%d",&i);
p = GetNode(head,i);
if(p == NULL)
printf("未找到\n");
else
printf("要查找的值为:%d \n",p->data);
printf("输入欲插入元素的位置:");
scanf("%d",&i);
printf("请输入欲插入的元素:");
scanf("%d",&x);
InsertList(head,x,i);
PrintList(head);
printf("请输入欲删除结点的位置:");
scanf("%d",&i);
DeleteList(head,i);
PrintList(head);
}