满意答案
z99625622
2013.04.23
采纳率:54% 等级:13
已帮助:6625人
给你个简单的例子,虽然不是你想要的,你可以把这个看懂,然后改改就可以了
// 链表e.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include
#include
#include
#include
#include
/*---------------------结构体定义部分------------------------------*/
struct Node
{
char name[10];
int score;
struct Node *next;
};
typedef struct Node ListNode;
/*---------------------函数声明部分-----------------------------------*/
ListNode* CreateNode(int n);
void PrinfListNode(ListNode *h);
void InsertList(ListNode *h,int i,char name[],int e,int n);
void DeleteList(ListNode *h,int i,int n);
void DisplayNote(void);
/*---------------------函数实现部分-----------------------------------*/
int main(int argc, char* argv[])
{
ListNode *h;
int i=1,n,score;
char name[10];
int *m=0;
cout<
while(i)
{
DisplayNote();
scanf("%d",&i);
switch(i)
{
case 1:
printf("Please input the number of the list\n");
scanf("%d",&n);
h=CreateNode(n);
printf("List elements is:\n");
PrinfListNode(h);
break;
case 2:
printf("Please input the position of the element:");
scanf("%d",&i);
printf("Please input name of the student:");
scanf("%s",&name);
printf("Please input score of the student:");
scanf("%d",&score);
InsertList(h,i,name,score,n);
printf("List elements is:\n");
PrinfListNode(h);
break;
case 3:
printf("Please input the position of the element:");
scanf("%d",&i);
DeleteList(h,i,n);
printf("List elements is:\n");
PrinfListNode(h);
break;
case 4:
printf("List elements is:\n");
PrinfListNode(h);
break;
case 0:
return 0;
break;
default:
printf("ERROR! Please try again");
}
}
return 0;
}
/*-----------------建立链表函数实现部分-----------------------------------*/
ListNode *CreateNode(int n)
{
ListNode *head;
ListNode *pre,*p;
int i;
head=(ListNode*)malloc(sizeof(ListNode));
head->next=NULL;
pre=head;
for (i=1;i<=n;i++)
{
printf("Please input name of the %d student:",i);
p=(ListNode*)malloc(sizeof(ListNode));
scanf("%s",&p->name);
printf("Please input score of the %d student:",i);
scanf("%d",&p->score);
pre->next=p;
pre=p;
}
p->next=NULL;
return head;
}
/*-----------------输出链表函数实现部分-----------------------------------*/
void PrinfListNode(ListNode *h)
{
ListNode *p;
p=h->next;
while(p)
{
printf("name:%s\tscore:%d",p->name,p->score);
p=p->next;
printf("\n");
}
}
/*-----------------插入链表函数实现部分-----------------------------------*/
void InsertList(ListNode *h,int i,char name[],int e,int n)
{
ListNode *p,*q;
int j;
if (i<1||i>n+1)
{
printf("Error ! Please input again.\n");
}
else
{
j=0;p=h;
while(j
{
p=p->next;
j++;
}
q=(ListNode *)malloc(sizeof(ListNode));
strcpy(q->name,name);
q->score=e;
q->next=p->next;
p->next=q;
}
}
/*-----------------删除链表函数实现部分-----------------------------------*/
void DeleteList(ListNode *h,int i,int n)
{
ListNode *p,*q;
int j;
char name[10];
int score;
if (i<1||i>n)
{
printf("Error ! Please input again.\n");
}
else
{
j=0;p=h;
while(j
{
p=p->next;
j++;
}
q=p->next;
p->next=q->next;
strcpy(name,q->name);
score=q->score;
free(q);
printf("name=%s,score=%d",name,score);
}
}
void DisplayNote(void)
{
printf("1--建立新的链表\n");
printf("2--添加元素\n");
printf("3--删除元素\n");
printf("4--输出当前链表中的内容\n");
printf("0--建立新的链表\n");
}
01分享举报