code:
#include <stdio.h>
#include <time.h>
#include <conio.h>
#include <stdlib.h>
#define INITIA 10
typedef int ElemType;
typedef int Status;
typedef struct Node
{
ElemType data;
struct Node * next;
}node;
node * head = NULL, *p, *q;
Status GetElem();
Status LinkListInsert();
Status LinkListDelete();
//获取某个地址的元素
Status GetElem(int i, ElemType e)
{
int j;
p = head;
j = 1;
while(p && j < i - 1)
{
p = p -> next;
++j;
}
if(!p || j > i)
return 0;
e = p -> data;
printf("%d
",p -> data);
return 1;
}
//向链表某位置插入节点
Status LinkListInsert(int i)
{
int j;
node * s;
p = head;
j = 1;
while(p && j < i - 1)
{
p = p ->next;
++j;
}
if(!p || j > i)
return 0;
s = ( node * ) malloc ( sizeof ( node ) );
s -> data = rand()%100 + 1;
s ->next = p -> next, p -> next = s ;
return 1;
}
//删除链表某节点
Status LinkListDelete(int i, ElemType e)
{
int j;
node * s;
p = head;
j = 1;
while(p && j < i - 1)
{
p = p -> next;
++j;
}
if(!p || j > i)
return 0;
s = p -> next;
p -> next = p -> next -> next;
e = s -> data;
free(s);
s = NULL;
return 1;
}
int main()
{
char str;
int i;
ElemType e = 0;
srand ( time( 0 ) );
for(i = 0; i < INITIA; i ++)
{
p = ( node * ) malloc ( sizeof ( node ) );
if(head == NULL)
head = p;
else
q ->next = p;
p -> next = NULL;
p -> data = rand()%100 + 1;
q = p;
}
p = head;
while(p)
{
printf("%d ",p -> data);
p = p -> next;
}
printf("
查找 请按 1 插入数据 请按 2 删除数据 请按 3");
str = getch();
if(str == '1')
{
printf("
请输入要查找的数的位置:");
scanf("%d
",&i);
GetElem(i, e);
}
if(str == '2')
{
printf("
请输入要插入的数的位置:"); //插在原本该位置上数据的前面
scanf("%d",&i);
LinkListInsert(i);
p = head;
while(p)
{
printf("%d ",p -> data);
p = p -> next;
}
}
if(str == '3')
{
printf("
请输入要删除的数的位置:");
scanf("%d",&i);
LinkListDelete(i, e);
p = head;
while(p)
{
printf("%d ",p -> data);
p = p -> next;
}
}
while(head)
{
p = head;
head = head -> next;
free(p);
}
p = NULL;
return 0;
}
# 2017.8.8
前面的测试并不完整,它有一个BUG,比如插入第一位和删除第一位时都不对。
解决:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>
#include<conio.h>
#define N 5
typedef char Element;
typedef int Status;
typedef struct Node
{
Element data [20];
struct Node*next;
}Node;
Node*head,*tail,*current;
void Swap();
void Sort();
int length();
Status GetData();
Status InsertNode();
Status DeleteNode();
void Swap(Element*p, Element*q)
{
Element swap[20];
strcpy(swap, p),strcpy(p, q),strcpy(q, swap);
}
void Sort(Node*p)
{
Node*q;
for(p = head->next; p != NULL; p = p->next)
for(q = p->next; q != NULL; q = q->next)
if(*(p->data) > *(q->data))
Swap(p->data, q->data);
}
int length(Node*p) //链表长度
{
int i = 1;
p = head->next;
while(p)
{
p = p ->next;
i++;
}
return i;
}
Status GetData(Node*p, int i) //获取某节点数据
{
int j = 0;
Element Elem [20];
p = head->next;
if(i < 1 || i > length(p))
return 0;
while(p && j < i - 1)
{
p = p->next;
j++;
}
if(!p || j > i)
return 0;
printf("%s
",strcpy(Elem, p->data));
return 1;
}
Status InsertNode(Node*p, int i, Element Elem [])
{
Node*New;
int j = 1;
p = head->next;
if(i == 1)
{
New = (Node *)malloc(sizeof(Node));
strcpy(New->data, Elem);
New->next = head->next;
head->next = New;
}
if(i < 1 || i > length(p))
return 0;
if(i > 1)
{
while(p && j < i - 1)
{
p = p->next;
j++;
}
if(!p || j > i)
return 0;
New = (Node *)malloc(sizeof(Node));
strcpy(New->data, Elem);
New->next = p->next;
p->next = New;
}
return 1;
}
Status DeleteNode(Node*p, int i)
{
int j = 1;
Node*h;
p = head->next;
if(i == 1)
{
h = head->next;
head->next = p->next;
free(h);
}
if(i < 1 || i > length(p))
return 0;
if(i > 1)
{
while(p && j < i - 1)
{
p = p->next;
j++;
}
if(!p || j > i)
return 0;
h = p->next;
p->next = p->next ->next;
free(h);
}
return 1;
}
int main()
{
int i, Pos;
char key;
Element Elem[20];
Node*p;
head = (Node *)malloc(sizeof(Node));
tail = head;
for(i = 0; i < N; i++)
{
current = (Node *)malloc(sizeof(Node));
gets(current->data);
tail-> next = current;
tail = current;
}
tail->next = NULL;
puts("显示数据:");
p = head->next;
while(p)
{
printf("%s ",p->data);
p = p->next;
}
putchar('
');
puts("按 1 由大到小排序 按 2 插入数据");
puts("按 3 删除数据 按 4 查看某位置的数据");
puts("按 q 退出程序");
while((key = getch()) != 'q')
{
if(key == '1')
{
p = head->next;
Sort(p);
p = head->next;
while(p)
{
printf("%s ", p->data);
p = p->next;
}
putchar('
');
}
if(key == '2')
{
p = head->next;
puts("
");
printf("请输入要插入的位置/数据:");
scanf("%d %s",&Pos, Elem);
InsertNode(p, Pos, Elem);
puts("插入完毕!
显示数据:");
p = head->next;
while(p)
{
printf("%s ", p->data);
p = p->next;
}
}
if(key == '3')
{
p = head->next;
puts("
");
printf("请输入要删除的数据的位置:");
scanf("%d",&Pos);
DeleteNode(p, Pos);
puts("删除完毕!
显示数据:");
p = head->next;
while(p)
{
printf("%s ",p->data);
p = p->next;
}
}
if(key == '4')
{
p = head->next;
printf("请输入要查看的数据的位置:");
scanf("%d", &Pos);
puts("查看数据:");
GetData(p, Pos);
}
}
return 0;
}