您的位置:首页 > 编程语言 > C语言/C++

c语言-链表实现

2014-02-14 17:36 447 查看
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void initList(struct Node **pHead);
void creatList(struct Node **pHead);
void insertHeadList(struct Node **pHead);
void insertTailList(struct Node **pHead);
void searchList(struct Node **pHead);
void deleteList(struct Node **pHead);
void printList(struct Node *pHead);

void sortinsertList(struct Node **pHead);

typedef struct Node{
int data;
struct Node *next;
}Node;

int main()
{
struct Node *lxm;
int id;
initList(&lxm);

while(1)
{
printf("请输入指令:\n");
printf("0、创建;1、头插入;2、尾插入;3、删除;\n");
printf("4、查询;5、排序插入;6、打印\n");

scanf("%d",&id);
system("cls");
switch(id)
{
case 0:creatList(&lxm);break;
case 1:insertHeadList(&lxm);break;
case 2:insertTailList(&lxm);break;
case 3:deleteList(&lxm);break;
case 4:searchList(&lxm);break;
case 5:sortinsertList(&lxm);break;
case 6:printList(lxm);
default:break;
}

}
return 0;
}

void initList(struct Node **pHead)
{
*pHead = NULL;
printf("初始化链表头成功\n");
}

//创建链表
void creatList(struct Node **pHead)
{
struct Node *p = NULL;
struct Node *q = NULL;

p = q = (struct Node *)malloc(sizeof(struct Node));
if(p == NULL || q == NULL)
{
printf("malloc failed\n");
exit(0);
}
memset(p,0,sizeof(struct Node));

printf("链表创建初始化值\n");
scanf("%d",&p->data);
p->next = NULL;

while(p->data > 0)
{
if(*pHead == NULL)
{
*pHead = p;
}
else
{
q->next = p;
q = p;
}

p = (struct Node *)malloc(sizeof(struct Node));
if(p == NULL)
{
printf("malloc failed\n");
exit(0);
}
memset(p,0,sizeof(struct Node));

scanf("%d",&p->data);
p->next = NULL;
}

printf("链表创建成功\n");
}

void sortinsertList(struct Node **pHead)
{
struct Node *p = *pHead;
struct Node *h = *pHead;
struct Node *temp = NULL;
int num = 0;

temp = (struct Node *)malloc(sizeof(struct Node));
if(temp == NULL)
{
printf("malloc failed\n");
exit(0);
}
memset(temp,0,sizeof(struct Node));

printf("请插入值\n");
scanf("%d",&temp->data);

while(p->next != NULL)
{
if(h->data > temp->data)
{
temp->next = h;
*pHead = temp;
}
else if(temp->data > p->data && temp->data < p->next->data)
{
temp->next = p->next;
p->next = temp;
}
p = p->next;
}
if(p->next == NULL)
{
temp->next = NULL;
p->next = temp;
}
}

void searchList(struct Node **pHead)
{
struct Node *p = *pHead;
int i = 1;
int num = 0;

printf("请输入需要查询的数值\n");
scanf("%d",&num);

while(p->data != num && p->next != NULL)
{
i++;
p = p->next;
}
printf("该数值在链表中的位置是: %d \n",i);
}

void insertHeadList(struct Node **pHead)
{
struct Node *p;

p = (struct Node *)malloc(sizeof(struct Node));
memset(p,0,sizeof(struct Node));
printf("请输入插入的数值\n");
scanf("%d",&p->data);

p->next = *pHead;
*pHead = p;
printf("向表头插入元素成功\n");
}

void insertTailList(struct Node **pHead)
{
struct Node *p = NULL;
struct Node *temp = *pHead;

p = (struct Node *)malloc(sizeof(struct Node));
memset(p,0,sizeof(struct Node));
printf("请输入插入的数值\n");
scanf("%d",&p->data);

while(temp->next != NULL)
{
temp = temp->next;
}
temp->next = p;

printf("向表头插入元素成功\n");
}

void deleteList(struct Node **pHead)
{
int num = 0;
struct Node *h = *pHead;
struct Node *p = *pHead;
struct Node *n = h->next;

printf("请输入需要删除的数值\n");
scanf("%d",&num);

while(p->next != NULL)
{
if(num == h->data)
{
struct Node *temp1 = h;
p = h = h->next;
n = n->next;
*pHead = h;
free(temp1);
break;
}
else if(num == n->data && num != h->data)
{
struct Node *temp2 = n;
n = n->next;
p->next = n;
free(temp2);
break;
}
p = p->next;
n = p->next;
}
printf("删除成功\n");
}

void printList(struct Node *pHead)
{
struct Node *p = pHead;

if(NULL == p)   //链表为空
{
printf("链表为空\n");
}
else
{
printf("链表元素为: ");
while(NULL != p)
{
printf("%d ",p->data);
p = p->next;
}
printf("\n");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: