软件技术基础上机实验报告

更新时间:2024-03-22 10:59:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

电子科技大学

软件技术基础上机实验报告

上机实验二

实验名称:ex2—1

#include \#include \typedef struct node { int data; struct node *next; }node_type;

node_type *create_sllist( ) {

int x; node_type *h, *newnode, *temp;

h= (node_type *) malloc(sizeof(node_type)); h->next = NULL; temp=h; printf(\ scanf(\ while(x!= -1) { newnode=(node_type *)malloc(sizeof(node_type)); if (newnode!=NULL) { newnode->data = x; newnode->next=NULL; temp->next = newnode; temp = newnode; scanf(\ } else { return (h); } } return(h); }

void insert_sllist(node_type *L,int i,int e) { node_type *s,*temp; int j;

temp=L; j=0; while(temp!=NULL&&jnext; j++; }

if(temp==NULL||i<1)

printf(\输入位置错误!\ else { s=(node_type*)malloc(sizeof(node_type)); s->data=e;

s->next=temp->next; temp->next=s; } }

node_type *print( node_type *head ) { node_type *temp; temp=head->next; while( temp!=NULL) { printf(\ \ temp=temp->next; } }

void delete_sllist(node_type *list, int i) {

int counter; node_type *temp, *s; temp = list; counter = 0; while(counternext != NULL) { counter++;

temp=temp->next; }

if (counter!=i-1) {

printf (\

return; } else

{ s = temp->next; temp->next = temp->next->next; free(s); } }

void main() {

int newdata,newp,p; node_type *L; L=create_sllist(); print(L);

printf(\ scanf(\ scanf(\

insert_sllist(L,newp,newdata); print(L);

printf(\ position of the number you want to delete:\\n\ scanf(\ delete_sllist(L,newp); print(L); }

典型数据输入(如图):

应输出(上机前自己分析的结果):1 3 6 8 2 7 上机时遇到的问题:

1、无法同时读取数据和插入位置。 解决办法:分开输入

实际运行结果(如图):

个人体会:

对链表的编写还应加强练习。

实验名称:ex2—2

#include \#include \typedef struct node { int data; struct node *next; }node_type;

node_type *create_sllist( ) {

int x; node_type *h, *newnode, *temp;

h= (node_type *) malloc(sizeof(node_type)); h->next = NULL; temp=h; printf(\ scanf(\ while(x!= 0) { newnode=(node_type *)malloc(sizeof(node_type)); if (newnode!=NULL) { newnode->data = x; newnode->next=NULL; temp->next = newnode; temp = newnode;

scanf(\ } else { return (h); } } return(h); }

void insert_sllist(node_type *L,int e) {

node_type *s,*temp; temp=L; while(temp->next!=NULL) { temp=temp->next; } s=(node_type*)malloc(sizeof(node_type)); s->data=e;

s->next=NULL; temp->next=s; }

node_type *print( node_type *head ) { node_type *temp; temp=head->next; while( temp!=NULL) { printf(\ \ temp=temp->next; } }

void delete_sllist(node_type *list, int i) { node_type *temp, *s; temp = list; while(temp->next->data!=i&& temp->next

{ temp=temp->next;

} s = temp->next; temp->next = temp->next->next; free(s); }

int visit(node_type *L,int n) {

int counter;

NULL) !=

node_type *temp, *s; temp = L; counter = 0; while(temp!= NULL) { if(temp->data!=n) {

counter++;

temp=temp->next;

}

else {

return(1); break; } }

if(temp == NULL) return(0); }

void main() {

int newdata,p; node_type *L; L=create_sllist(); print(L);

printf(\ data \\n\ scanf(\ p=visit(L,newdata); if(p==1) {

printf(\ delete_sllist(L,newdata); print(L); } else {

printf(\ insert_sllist(L,newdata); print(L); } }

典型数据输入(如图):

应输出(上机前自己分析的结果):2 17 63 72 302

2 17 63 5 72 302 7

上机时遇到的问题:

1、删除函数的调用不正确 解决办法:参考书本得以解决

实际运行结果(如图):

实验名称:ex2—3

#include \#include \typedef struct node

{ int data; struct node *next; }node_type;

node_type *create_sllist( ) {

int x; node_type *h, *newnode, *temp;

h= (node_type *) malloc(sizeof(node_type)); h->next = NULL; temp=h; printf(\ scanf(\ while(x!= -1) { newnode=(node_type *)malloc(sizeof(node_type)); if (newnode!=NULL) { newnode->data = x; newnode->next=NULL; temp->next = newnode; temp = newnode; scanf(\ } else { return (h); } } return(h); }

void insert_sllist(node_type *L,int i) {

node_type *s,*temp; temp=L; while(temp!=NULL) { if(i>temp->data&&inext->data) { s=(node_type*)malloc(sizeof(node_type)); s->data=i;

s->next=temp->next; temp->next=s; } else { temp=temp->next; } } }

node_type *print( node_type *head ) { node_type *temp; temp=head->next; while( temp!=NULL) { printf(\ \ temp=temp->next; } }

void main() {

int newdata,newp,p; node_type *L; L=create_sllist(); print(L);

printf(\ scanf(\ insert_sllist(L,newdata); print(L); }

典型数据输入(如图):

应输出(上机前自己分析的结果):5 8 13 22 47 92 103 实际运行结果(如图):

本文来源:https://www.bwwdw.com/article/mz88.html

Top