剑灵6人伤害修改:编写程序,实现链表的插入和删除

来源:百度文库 编辑:杭州交通信息网 时间:2024/05/14 01:54:21
程序中附带链表的创建,其余内容如标题```

#include <stdlib.h>
#include <memory.h>
#include <stdio.h>
#include <string.h>

struct node /*节点的数据结构*/
{
int num;
char str[20];
struct node *next;
};

struct node *creat(struct node *head);
struct node *insert(struct node *head, char *pstr, int n);
struct node *delet(struct node *head, char *pstr);
void print(struct node *head);

/* * * * * * * * * * * * * * * * * * * * * * * * * * * */
main()
{
/*函数声明*/
struct node *head;
char str[20];
int n;

head = NULL; /*做空表*/
head=creat(head); /*调用函数创建以head 为头的链表*/
print(head); /*调用函数输出节点*/
printf("\n input inserted num,name:\n");
gets(str); /*输入学号*/
n = atoi(str);
gets(str); /*输入姓名*/
head = insert(head, str, n); /*将节点插入链表*/
print(head); /*调用函数输出节点*/
printf("\n input deleted name:\n");
gets(str); /*输入被删姓名*/
head=delet(head,str); /*调用函数删除节点*/
print(head); /*调用函数输出节点*/
return;
}
/* * * * * * * * * * * * * * * * * * * * * */

/* * * 创建链表* * * * * * * * * * * */
struct node *creat(struct node *head)
{
char temp[30];
struct node *p1,*p2;
p1 = p2 = (struct node*) malloc(sizeof(struct node));
printf ("input num, name: \n") ;
printf("exit:double times Enter!\n");
gets(temp);
gets(p1->str);
p1->num = atoi(temp);
p1->next = NULL;
while (strlen(p1->str)>0)
{
if (head==NULL)head=p1;
else p2->next = p1;
p2 = p1;
p1 = (struct node *)malloc(sizeof(struct node));
printf ("input num, name: \n");
printf("exit:double times Enter!\n");
gets(temp) ;
gets(p1->str);
p1->num=atoi(temp);
p1->next=NULL;
}
return head;
}

/* * * * * * * * * * * * * * * * * * * */
/* * * * * * * * * * 插入节点* * * * * * * * * */
struct node *insert(struct node *head, char *pstr, int n)
{
struct node *p1, *p2, *p3;
p1=(struct node*)malloc(sizeof(struct node));
strcpy(p1->str, pstr);
p1->num = n;
p2 = head;
if(head == NULL)
{
head = p1;
p1->next = NULL;
}
else
{
while ((n > p2->num) && (p2->next != NULL))
{
p3 = p2;
p2 = p2->next;
}
if (n <= p2->num)
if (head==p2)
{
head = p1 ;
p1->next = p2;
}
else
{
p3->next = p1;
p1->next = p2;
}
else
{
p2->next = p1;
p1->next = NULL;
}
}
return(head);
}
/* * * * * * * * * * * * * * * * * * * * * * * * */
/* * * * * 删除节点* * * * * * * * * * * * */
struct node *delet(struct node *head, char *pstr)
{
struct node *temp, *p;
temp = head;
if(head == NULL)
printf("\nList is null!\n");
else
{
temp = head;
while((strcmp(temp->str, pstr) != 0) && (temp->next != NULL))
{
p = temp;
temp = temp->next;
}
if(strcmp(temp->str, pstr) == 0)
{
if(temp == head)
{
head = head->next;
free(temp);
}
else
{
p->next = temp->next;
printf("delete string : %s\n", temp->str);
free(temp);
}
}
else printf("\nno find string!\n");
}
return(head);
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* * * * * * * * * * 链表各节点的输出* * * * * * * * * */
void print (struct node *head)
{
struct node *temp;
temp = head;
printf("\n output strings:\n");
while(temp!=NULL)
{
printf("\n%d----%s\n", temp->num, temp->str);
temp = temp->next;
}
return;
}

#include <stdio.h>
#define OK 1
#define ERROR 0
#define LINK_LIST_MAX 100
#define OVERFLOW -1
#define NULL 0
typedef int Entryelem;
typedef struct {
Entryelem *head;
int length;
} LINK_LIST ;

LINK_LIST *l;
int Initlist(LINK_LIST l)
{l.head=(Entryelem*)malloc(LINK_LIST_MAX*sizeof(Entryelem));
if(l.head==NULL) return(ERROR);
l.length=0;
return(OK);
}

void clearlist(LINK_LIST *l)
{
l->length=0;
}

int listlength(LINK_LIST *l){
return(l->length);
}

int listinsert(LINK_LIST *l,int i,Entryelem e)
{
int j;
if(l->length==LINK_LIST_MAX) return ERROR;
if(i<1||i>l->length+1) return(ERROR);
for(j=l->length-1;j>=i-1;j--)
l->head[j+1]=l->head[j];
l->head[i-1]=e;
l->length++;
return OK;
}
int isempty(LINK_LIST *l)
{if(l->length==0) return OK;
else return ERROR;
}

int listdelete(LINK_LIST *l, int i,Entryelem *e)
{
int j;
if(isempty(l)) return ERROR;
if(i<1||i>l->length) return ERROR;
*e=l->head[i-1];
for (j=i;j<l->length-1;j++)
l->head[j-1]=l->head[j];
l->length--;
return OK;
}

main()
{
int i;
Entryelem e;

if(Initlist(*l)==OK)
printf("success\n");
i=l->length;
printf("i=%d\n",i);
for(i=0;i<5;i++)
{scanf("%d",&e);
listinsert(l,i+1,e);
}
i=l->length;
printf("i=%d",i);
clearlist(l);
printf("%d",l->length);
}