利用数觉结构和C语言。实现单链表的创建 插入 删除 打印 查询

一定要C语言版的 调试过的 急需

#include <stdio.h>
#include <malloc.h>
#define N 8
typedef struct node
{int data;
struct node *next;
}node;

node * createsl()
{
node *p,*s,*h;
int j=1,x;
p=s=h=(node*)malloc(sizeof(node));
h->next=NULL;
printf("please input the data to create the list,end with -1 or %d nupmbers\n",N);
while(x!=-1&&j<=N)
{
printf("number %d:",j);
scanf("%d",&x);
s=(node*)malloc(sizeof(node));
s->data=x;
if(h->next==NULL)
h->next=s;
else
p->next=s;
p=s;
j++;
}
p->next=NULL;
return h;
}

int access(node *h,int i)
{
node *p;int j=1;
p=h->next;
while(p!=NULL)
{
if(p->data==i)
break;
p=p->next;
j++;
}
if(p!=NULL)
{
printf("find the number in position:%d\n",j);
return(p->data);
}
else
{
printf("can't find the number in the list!\n");
return -1;
}
}

void insertsl(node *h,int i)
{
node *p,*t;
int j=1;
p=h->next;;
while(p->next!=NULL)
{
p=p->next;
j++;
}

t=(node*)malloc(sizeof(node));
t->data=i;
t->next=p->next;
p->next=t;
printf("insert success in position %d\n",j+1);
}

void deletesl(node *h,int i)
{
node *p,*s,*q;
int j=1;
p=h;
while(p->next!=NULL)
{
q=p->next;
if(q->data==i)
break;
p=p->next;
j++;
}

if(p->next==NULL)
{
printf("Can't find the number you want to delete.\n");
return;
}
else
{
s=p->next;
p->next=s->next;
free(s);
printf("delete success in position %d\n",j+1);
}
}

void print(node *h)
{
printf("\nprint all the data in the list:") ;
node *s;
s=h->next;
if(s!=NULL)
{
while(s!=NULL)
{
printf(" %d ",s->data) ;
s=s->next;
}
}
else
printf("the list is empty!%d");
printf("\n");
}

int main()
{
node *p;
int a;
p=createsl() ;
printf("\nyou need find the number:\n");
scanf("%d",&a);
access(p,a);

printf("\nplease input the number you want to insert:\n");
scanf("%d",&a);
insertsl(p,a);

printf("\nplease input the number you want to delete:\n");
scanf("%d",&a);
deletesl(p,a);

print(p);
return 0;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2009-05-26
写了个不完全但能运行的版本,剩下的自己补齐吧

/*
* File: LInkedList.c
* Author: huyi
*
* Created on May 25, 2009, 9:17 PM
*/

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

#define null 0

typedef struct Node Node;
typedef struct LinkedList LinkedList;
typedef int ElementType;

struct Node {
ElementType data;
Node* next;
};

struct LinkedList {
Node* next;
};

LinkedList* createLinkedList() {
LinkedList* head = malloc(sizeof (LinkedList));
if (head) {
head->next = null;
return head;
} else {
return null;
}
}

void append(LinkedList* list, ElementType e) {
Node* newNode = malloc(sizeof (Node));
if (newNode) {
Node* next = list->next;
list->next = newNode;
newNode->data = e;
newNode->next = next;
}
}

void delete(LinkedList* list, ElementType e) {
Node* current = list->next;
while (current && current->next->data != e) {
current = current->next;
}

if (current) {
Node* found = current->next;
current->next = current->next->next;
free(found);
}
}

void print(LinkedList* list) {
Node* current = list->next;
while (current) {
printf("%d\t", current->data);
current = current->next;
}
puts("");
}

/*
*
*/
int main(int argc, char** argv) {
LinkedList* myList = createLinkedList();
append(myList, 1);
append(myList, 2);
append(myList, 3);
print(myList);
delete(myList, 2);
append(myList, 4);
print(myList);
return (EXIT_SUCCESS);
}
相似回答